-
Notifications
You must be signed in to change notification settings - Fork 162
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 or more. *