Skip to content

Commit

Permalink
Released challenge 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Landau committed Jan 18, 2017
1 parent 908a86a commit 7261c03
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 14 deletions.
5 changes: 5 additions & 0 deletions challenge_12/python/slandau3/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.linting.pylintEnabled": true,
"python.pythonPath": "python3",
"python.formatting.provider": "autopep8"
}
33 changes: 21 additions & 12 deletions challenge_12/python/slandau3/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,34 @@ def compress(string_to_compress: str) -> str:
output = "" # The compressed string that will be outputted
character = "" # character the string_to_compress at blank
occurrences = 0 # the number of occurrences of this letter
for index, char in enumerate(string_to_compress): # goes through both the range and the characters at the same time
if char != character or index == len(string_to_compress)-1: # if we spot a different character,
# goes through both the range and the characters at the same time
for index, char in enumerate(string_to_compress):
# if we spot a different character,
if char != character or index == len(string_to_compress) - 1:
# or are at the end of the string_to_compress
# go here
if index == len(string_to_compress)-1 and character == char: # If we are at the end of
# If we are at the end of
if index == len(string_to_compress) - 1 and character == char:
# the string_to_compress
# but the last character is the same, add to the occurrences of the character
# but the last character is the same, add to the occurrences of
# the character
occurrences += 1

if occurrences > COMPRESSION_CUTOFF:
# If we have more than three occurrences, add the compress format to the output
# If we have more than three occurrences, add the compress
# format to the output
output += character + '#' + str(occurrences)
else:
# the next line puts 'occurrences' number of 'character' in the output
# the next line puts 'occurrences' number of 'character' in the
# output
output += character * occurrences

if index == len(string_to_compress)-1 and character != char:
if index == len(string_to_compress) - 1 and character != char:
# If we are at the end of the string_to_compress and the character
# is not the same as the last. Top it off
output += char

character = char # set char to character so we know the last character we looked at
character = char # set char to character so we know the last character we looked at
occurrences = 1

else:
Expand All @@ -41,15 +47,18 @@ def compress(string_to_compress: str) -> str:

def decompress(string_to_uncompress: str) -> str:
# Using regular expressions to parse the string_to_uncompress
matches = re.findall(r'(.#\d+)', string_to_uncompress) # Looking for [anything]#[at least one number]
decompressed = string_to_uncompress # decompressed is the new string that we will output
for match in matches: # Scan through the matches and uncompress each of them
# Looking for [anything]#[at least one number]
matches = re.findall(r'(.#\d+)', string_to_uncompress)
# decompressed is the new string that we will output
decompressed = string_to_uncompress
for match in matches: # Scan through the matches and uncompress each of them
# match the character so we know what character we need to expand
char = re.match(r'(.)#\d+', match)
# Determine the number of times it occurred
times = re.search(r'(\d+)', match)

replacement = char.group(1) * int(times.group(1)) # To get the matches specifically we need to access
# To get the matches specifically we need to access
replacement = char.group(1) * int(times.group(1))
# them at group 1
decompressed = decompressed.replace(match, replacement)

Expand Down
9 changes: 7 additions & 2 deletions challenge_12/python/slandau3/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from compression import compress, decompress

class Test(unittest.TestCase):
"""
hello
"""

def test1(self):
"""
Expand All @@ -17,7 +20,8 @@ def test2(self):
An arbitrarily long string
:return:
"""
self.assertEqual(compress('abadbadsakdlfjieafnealfjiasfjaaaaanadddddddkkkjj'), "abadbadsakdlfjieafnealfjiasfja#5nad#7kkkjj")
self.assertEqual(compress('abadbadsakdlfjieafnealfjiasfjaaaaanadddddddkkkjj'), \
"abadbadsakdlfjieafnealfjiasfja#5nad#7kkkjj")

def test3(self):
"""
Expand All @@ -38,7 +42,8 @@ def test5(self):
Decompress a fairly large string without too many compressions in it.
:return:
"""
self.assertEqual(decompress('abadbadsakdlfjieafnealfjiasfja#5nad#7kkkjj'), 'abadbadsakdlfjieafnealfjiasfjaaaaanadddddddkkkjj')
self.assertEqual(decompress('abadbadsakdlfjieafnealfjiasfja#5nad#7kkkjj'), \
'abadbadsakdlfjieafnealfjiasfjaaaaanadddddddkkkjj')

def test6(self):
"""
Expand Down
25 changes: 25 additions & 0 deletions challenge_14/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Reversing a linked list
======
Idea
----
Everyone should be somewhat familiar with the concept of a linked list by now. Today, you're job is to create a linked list and reverse it. Your program needs to be able to take strings from standard input, which will be used as each nodes data attribute. The strings in standard input will be space separated. Nodes are to be created and connected for these strings one after another (newest node is appended to the end of the linked list).

As for the "reverse" function. You need to implement a function to reverse the linked list. However, you may not touch the data on any of the nodes. The list must be reversed by manipulating the "next" attribute of each node.

The program should run in O(N) time.

You should have another function that will print the linked list (simply loop through it and print the data at each node).

Testing
------
In order to test the program, you may use the provided test files. Plug all strings from the test files into your program to create a node for each one. Print the list of nodes then reverse the list. After the list has been completely reversed output the list again (which should result in the reverse of what was previously printed).

The test inputs are also listed below.

1. r e d r u m

2. p a r k

3. b o o b y t r a p

4. l i v e
68 changes: 68 additions & 0 deletions challenge_14/java/slandau3/src/Reverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Created by slandau on 1/11/17.
*/
public class Reverse<T> {
public Node root = null;
public int size = 0;

private class Node {
public T data;
public Node next;

public Node(T data) {
this.data = data;
}
}

public void append(T data) {
if (size == 0) {
root = new Node(data);
} else {
Node temp = root;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Node(data);
}
size++;
}

public void reverse() {
if (size == 1) {
// do nothing
} else {
Node prev = null;
Node cur = root;
Node next = root.next;
while (next != null) {
cur.next = prev;
prev = cur;
cur = next;
next = next.next;
}
cur.next = prev;
root = cur;
}

}

public void print() {
Node temp = root;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}


public static void main(String[] args) {
Reverse<Integer> a = new Reverse<>();
a.append(1);
a.append(2);
a.append(3);
a.print();
a.reverse();
a.print();
}

}
1 change: 1 addition & 0 deletions challenge_14/tests/test1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
r e d r u m
1 change: 1 addition & 0 deletions challenge_14/tests/test2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p a r k
1 change: 1 addition & 0 deletions challenge_14/tests/test3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b o o b y t r a p
1 change: 1 addition & 0 deletions challenge_14/tests/test4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
l i v e

0 comments on commit 7261c03

Please sign in to comment.