Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Challenge 12 (Unreviewed) #452

Merged
merged 1 commit into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 or more. *


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()