In: Computer Science
You are to implement a string compression algorithm with the following specifications:
Create a function RLE() to implement this algorithm recursively, this function should take in a string message and return the compressed string as an output.
Python code
CODE:
#function to implement recursive string compression
def RLE(a):
#base condition if string is empty or only one letter
if len(a)<=1:
return a
m=0#loop variable
#loop to count the occurence of letter.
while(a[0]==a[m]):
m+=1#incrementing the count.
k=""#string to store the count
#if count is more than 1 we store thecount as string in k.
if m > 1:
k=str(m)#storing count.
#recursive we send the remaining string to compress.
return a[0]+k+RLE(a[m:])
#taking input from user
string = input("Enter string to compress : ")
#calling and printing the function
print("The compressed string is : "+RLE(string))
CODE ATTACHMENTS:

OUTPUT:

We just take the base condition and then the recursive call for the re,maining string.
Please do comment for any queries.
PLease like it.
Thank You.