Question
PYTHON LANGUAGE please ONLY USE if else statements & recursion. DO NOT use for/while loops. Write a function called uncompress(C) that inverts or undoes the
PYTHON LANGUAGE please ONLY USE if else statements & recursion. DO NOT use for/while loops.
Write a function called uncompress(C) that "inverts" or "undoes" the compressing in your compress function.
That is, uncompress(compress(S)) should give back S.
- Here's a test that illustrates that point: assert uncompress(compress(64*'0')) == 64*'0'
- Again, helper functions are OK, as is using this week's previous problems or lab code you've written.
- be sure to test - here we provide several:
Examples of compress and uncompress in action:
In [1]: compress(64*'0') Out[1]: '01000000' In [2]: compress('11111') Out[2]: '10000101' In [3]: compress('101010') Out[3]: '100000010000000110000001000000011000000100000001' In [4]: uncompress('01000000') # 64 0's (see above) Out[4]: '0000000000000000000000000000000000000000000000000000000000000000' In [5]: uncompress('10000101') # 5 1's (see above) Out[5]: '11111' In [6]: uncompress('100000010000000110000001000000011000000100000001') # see above Out[6]: '101010' In [7]: Stripes = '0'*16 + '1'*16 + '0'*16 + '1'*16 In [8]: compress(Stripes) Out[8]: '00010000100100000001000010010000' In [9]: uncompress('00010000100100000001000010010000') Out[9]: 0000000000000000111111111111111100000000000000001111111111111111
Helper functions & compress function:
#helper function
def numToBaseB(N, B):
"""returns a string representing the number N in base B.
Argument N: a non-negative integer N
Argument B: a base B (between 2 and 10 inclusive)
"""
if N == 0:
return ''
else:
return numToBaseB(N//B, B) + str(N%B)
#helper function
def frontNum(S):
"""
"""
if len(S) <= 1:
return len(S)
elif S[0] == S[1]:
return 1 + frontNum(S[1:])
else:
return 1
#helper function
def addzeros(S):
"""addzeros makes strings of length 7 by adding 0's
Argument S: a string S
"""
if(len(S) == 7):
return S
else:
return addzeros("0"+S)
#compress function
def compress(S):
"""returns another binary string that is a run-length encoding
of the original.
Argument S: a binary string S (length <= 64)
"""
if len(S) == 0:
return ""
else:
return S[0]+addzeros(numToBaseB(frontNum(S), 2))+compress(S[frontNum(S):])
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started