forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#452 from sarcodian/python_12
[Python] Challenge 12 (reviewed and merged)
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Two Fuctions: | ||
|
||
Compress: | ||
Make a new string and append all chars unless same consecutive char, | ||
then store those in a temp variable, if 5 or more then compress. | ||
|
||
Decompress: | ||
Make a new string and add N instances of y after encountering any instances | ||
of y#N |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
def compress(): | ||
''' | ||
Ask for a string input and return a compressed string with all | ||
consecutive repetitions for 4 or greater replaced with a representation | ||
in the form of y#N. | ||
aaaaa -> a#5 | ||
''' | ||
|
||
orig_string = input("Enter the list to compress: ") | ||
temp = '' | ||
comp_string = '' | ||
|
||
for i, j in enumerate(orig_string): | ||
try: | ||
if j != orig_string[i+1]: | ||
temp += j | ||
if len(temp) > 4: | ||
comp_string += '{}#{}'.format(temp[0], len(temp)) | ||
temp = '' | ||
else: | ||
comp_string += j | ||
temp = '' | ||
else: | ||
temp += j | ||
except IndexError: | ||
if j == orig_string[i-1]: | ||
temp += j | ||
if len(temp) > 4: | ||
comp_string += '{}#{}'.format(temp[0], len(temp)) | ||
temp = '' | ||
else: | ||
comp_string += temp | ||
temp = '' | ||
else: | ||
temp += j | ||
if len(temp) > 0: | ||
comp_string += temp | ||
|
||
return comp_string | ||
|
||
def decompress(): | ||
''' | ||
Ask for a string and expand all instances of #N with the char preceeding it | ||
repeated N times. | ||
a#5 -> aaaaa | ||
''' | ||
comp_string = input("Enter string to be decompressed: ") | ||
temp = '' | ||
orig_string = '' | ||
|
||
for i, j in enumerate(comp_string): | ||
if j in '0123456789': | ||
pass | ||
elif j == '#': | ||
num = i+1 | ||
while comp_string[num] in '0123456789': | ||
temp += comp_string[num] | ||
num += 1 | ||
orig_string += comp_string[i-1]*(int(temp)-1) | ||
temp = '' | ||
else: | ||
orig_string += j | ||
|
||
return orig_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# we start by importing the unittest module | ||
import unittest | ||
from unittest import mock | ||
|
||
# Next lets import our function that we intend to do testing on | ||
# | ||
# **We could also import all functions by using * or just import the module | ||
# itself but for now lets just import the function | ||
from challenge_12 import compress, decompress | ||
|
||
# lets define our suite of tests as a class and lets inherit from unittest.TestCase | ||
class TestBinaryMethods(unittest.TestCase): | ||
|
||
def test_compress(self): | ||
|
||
test_list = [ | ||
'aaaaabbbbbcccccdd', | ||
'abcdefghhhhhijk', | ||
'iasudhaiusdh', | ||
'abcccccccccccccccde' | ||
] | ||
|
||
with mock.patch('builtins.input', side_effect=test_list): | ||
self.assertEqual(compress(), 'a#5b#5c#5dd') | ||
self.assertEqual(compress(), 'abcdefgh#5ijk') | ||
self.assertEqual(compress(), 'iasudhaiusdh') | ||
self.assertEqual(compress(), 'abc#15de') | ||
|
||
def test_decompress(self): | ||
|
||
test_list = [ | ||
'a#5b#5c#5dd', | ||
'abcdefgh#5ijk', | ||
'iasudhaiusdh' | ||
] | ||
|
||
with mock.patch('builtins.input', side_effect=test_list): | ||
self.assertEqual(decompress(), 'aaaaabbbbbcccccdd') | ||
self.assertEqual(decompress(), 'abcdefghhhhhijk') | ||
self.assertEqual(decompress(), 'iasudhaiusdh') | ||
|
||
def test_decompress_with_n_digit_numbers(self): | ||
|
||
test_list = [ | ||
'abc#15de', | ||
'abc#150de' | ||
] | ||
|
||
with mock.patch('builtins.input', side_effect=test_list): | ||
self.assertEqual(decompress(), 'abcccccccccccccccde') | ||
self.assertEqual(decompress(), 'ab'+('c'*150)+'de') | ||
|
||
|
||
# if the python file is ran by itself run unittest | ||
# This allows us to import the members of the file without running main if we need to | ||
if __name__ == '__main__': | ||
unittest.main() |