Question
PYTHON LANGUAGE. Please ONLY USE if else statements, and recursion. NO FOR/WHILE loops. Write a function called compress(S), whose argument is a binary string S
PYTHON LANGUAGE. Please ONLY USE if else statements, and recursion. NO FOR/WHILE loops.
Write a function called compress(S), whose argument is a binary string S of length less than or equal to 64, and and which returns another binary string as its result. The resulting binary string should be a run-length encoding of the original.
You may need a helper function or two... You may design and name them as you like! In class, we imagined frontNum(S), which you're welcome to use In addition, numToBinary will be helpful! Feel free to grab that from lab (or use numToBaseB).
Examples of compress in action:
In [1]: compress('11111')
Out[1]: '10000101'
In [2]: compress('101010')
Out[2]: '100000010000000110000001000000011000000100000001'
In [3]: compress(42*'0')
Out[3]: '00101010'
Helper functions:
def frontNum(s):
if len(s) <= 1:
return len(S)
elif S[0] == S[1]:
return 1 + frontNum(S[1:])
else:
return 1
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) 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