From fcd51488569447e6f32407089f994570ab461859 Mon Sep 17 00:00:00 2001 From: Sarcodian Date: Mon, 23 Jan 2017 13:19:22 +0300 Subject: [PATCH] Challenge 12 in python --- challenge_12/python/sarcodian/READ.me | 9 +++ .../python/sarcodian/src/challenge_12.py | 64 +++++++++++++++++++ .../sarcodian/src/challenge_12_tests.py | 57 +++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 challenge_12/python/sarcodian/READ.me create mode 100644 challenge_12/python/sarcodian/src/challenge_12.py create mode 100644 challenge_12/python/sarcodian/src/challenge_12_tests.py diff --git a/challenge_12/python/sarcodian/READ.me b/challenge_12/python/sarcodian/READ.me new file mode 100644 index 000000000..4281931d7 --- /dev/null +++ b/challenge_12/python/sarcodian/READ.me @@ -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 \ No newline at end of file diff --git a/challenge_12/python/sarcodian/src/challenge_12.py b/challenge_12/python/sarcodian/src/challenge_12.py new file mode 100644 index 000000000..12b531415 --- /dev/null +++ b/challenge_12/python/sarcodian/src/challenge_12.py @@ -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 diff --git a/challenge_12/python/sarcodian/src/challenge_12_tests.py b/challenge_12/python/sarcodian/src/challenge_12_tests.py new file mode 100644 index 000000000..15eb85ed6 --- /dev/null +++ b/challenge_12/python/sarcodian/src/challenge_12_tests.py @@ -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() \ No newline at end of file