Skip to content

Commit

Permalink
Merge pull request YearOfProgramming#452 from sarcodian/python_12
Browse files Browse the repository at this point in the history
[Python] Challenge 12 (reviewed and merged)
  • Loading branch information
Steven Landau committed Jan 23, 2017
2 parents 69725c0 + fcd5148 commit 61043f1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
9 changes: 9 additions & 0 deletions challenge_12/python/sarcodian/READ.me
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
64 changes: 64 additions & 0 deletions challenge_12/python/sarcodian/src/challenge_12.py
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
57 changes: 57 additions & 0 deletions challenge_12/python/sarcodian/src/challenge_12_tests.py
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()

0 comments on commit 61043f1

Please sign in to comment.