From 7261c0371a15df723d62bbcf4144a2c71940a0c4 Mon Sep 17 00:00:00 2001 From: Steven Landau Date: Wed, 18 Jan 2017 17:26:48 -0500 Subject: [PATCH] Released challenge 14 --- .../python/slandau3/.vscode/settings.json | 5 ++ challenge_12/python/slandau3/compression.py | 33 +++++---- challenge_12/python/slandau3/tests.py | 9 ++- challenge_14/README.md | 25 +++++++ challenge_14/java/slandau3/src/Reverse.java | 68 +++++++++++++++++++ challenge_14/tests/test1 | 1 + challenge_14/tests/test2 | 1 + challenge_14/tests/test3 | 1 + challenge_14/tests/test4 | 1 + 9 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 challenge_12/python/slandau3/.vscode/settings.json create mode 100644 challenge_14/README.md create mode 100644 challenge_14/java/slandau3/src/Reverse.java create mode 100644 challenge_14/tests/test1 create mode 100644 challenge_14/tests/test2 create mode 100644 challenge_14/tests/test3 create mode 100644 challenge_14/tests/test4 diff --git a/challenge_12/python/slandau3/.vscode/settings.json b/challenge_12/python/slandau3/.vscode/settings.json new file mode 100644 index 000000000..9612230c7 --- /dev/null +++ b/challenge_12/python/slandau3/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.linting.pylintEnabled": true, + "python.pythonPath": "python3", + "python.formatting.provider": "autopep8" +} \ No newline at end of file diff --git a/challenge_12/python/slandau3/compression.py b/challenge_12/python/slandau3/compression.py index 4253498f9..c5e470bcf 100644 --- a/challenge_12/python/slandau3/compression.py +++ b/challenge_12/python/slandau3/compression.py @@ -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: @@ -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) diff --git a/challenge_12/python/slandau3/tests.py b/challenge_12/python/slandau3/tests.py index 5168ccf79..fc38db22b 100644 --- a/challenge_12/python/slandau3/tests.py +++ b/challenge_12/python/slandau3/tests.py @@ -4,6 +4,9 @@ from compression import compress, decompress class Test(unittest.TestCase): + """ + hello + """ def test1(self): """ @@ -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): """ @@ -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): """ diff --git a/challenge_14/README.md b/challenge_14/README.md new file mode 100644 index 000000000..8de8eec6d --- /dev/null +++ b/challenge_14/README.md @@ -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 \ No newline at end of file diff --git a/challenge_14/java/slandau3/src/Reverse.java b/challenge_14/java/slandau3/src/Reverse.java new file mode 100644 index 000000000..19a4106d5 --- /dev/null +++ b/challenge_14/java/slandau3/src/Reverse.java @@ -0,0 +1,68 @@ +/** + * Created by slandau on 1/11/17. + */ +public class Reverse { + 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 a = new Reverse<>(); + a.append(1); + a.append(2); + a.append(3); + a.print(); + a.reverse(); + a.print(); + } + +} diff --git a/challenge_14/tests/test1 b/challenge_14/tests/test1 new file mode 100644 index 000000000..1895c812a --- /dev/null +++ b/challenge_14/tests/test1 @@ -0,0 +1 @@ +r e d r u m diff --git a/challenge_14/tests/test2 b/challenge_14/tests/test2 new file mode 100644 index 000000000..e523bfa61 --- /dev/null +++ b/challenge_14/tests/test2 @@ -0,0 +1 @@ +p a r k diff --git a/challenge_14/tests/test3 b/challenge_14/tests/test3 new file mode 100644 index 000000000..73500607c --- /dev/null +++ b/challenge_14/tests/test3 @@ -0,0 +1 @@ +b o o b y t r a p diff --git a/challenge_14/tests/test4 b/challenge_14/tests/test4 new file mode 100644 index 000000000..c80403e12 --- /dev/null +++ b/challenge_14/tests/test4 @@ -0,0 +1 @@ +l i v e