Question

In: Computer Science

You are to implement a string compression algorithm with the following specifications: If a character, ch,...

You are to implement a string compression algorithm with the following specifications:

  • If a character, ch, occurs n times in a row, then it will be represented by {ch}{n], where {n} is a value of n. For example is there is a substring “aaaa” it would be represented as “a4”.
  • If a character, ch occurs exactly one time in a row, then it will be simply represented as {ch}. For example, if the substring is “a”, then it will be represented as “a”.

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

Solutions

Expert Solution

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.


Related Solutions

Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
Analyze the algorithm experimentally. a)Implement the algorithm b)Let p be the string length at which your...
Analyze the algorithm experimentally. a)Implement the algorithm b)Let p be the string length at which your program takes 2 seconds to run, collect running times for your algorithm using the following string lengths: p/4, 2p/4, 3p/4, p, 5p/4, 6p/4, 7p/4, 2p. c)Generate your strings by reading the attached file, only reading as many characters as you need. d)Plot your results (x-axis is string length, y-axis should be time) e)Draw conclusions based on your graph. You may also need to plot...
Project Description: In groups of four students, implement (New Encryption Algorithm) character oriented and apply it...
Project Description: In groups of four students, implement (New Encryption Algorithm) character oriented and apply it using any programming Language that you are familiar with to encrypt and decrypt your name. Your work should cover: 1. Create the algorithm and explain (encryption-decryption) using block diagram. 2. Mention whether will you use the keys or not in your algorithm. 3- If you will use keys, explain how to generate the keys. 2. Display the generated keys 3. Encrypt your name 4....
QUESTION 1 The statement char ch = ‘A’ would store in ch A. The character A...
QUESTION 1 The statement char ch = ‘A’ would store in ch A. The character A B. ASCII value of A C. A along with the single inverted commas D. Both (a) and (b) 2 points    QUESTION 2 Which of the following scanf() statement will you use to accept following variables? float gravity_a; double gravity_b; A. scanf("%f %lf", &gravity_a, &gravity_b); B. scanf("%LF %f ", gravity_a, gravity_b); C. scanf("%LF %LF ", &gravity_a, &gravity_b); D. scanf("%f %LF ", gravity_a, gravity_b); 2...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
Calculate the expected life of N (# of cycles) in a compression spring with the specifications....
Calculate the expected life of N (# of cycles) in a compression spring with the specifications. Material: 302 Stainless Steel End type: closed Overall Length: 2" OD: 0.5" ID: 0.406" Wire Diameter: 0.047" Wire Shape: Round Compressed Lenght: 1.35" Maximum Load: 3.8 lbs Rate: 5.85 lbs/in RoHS: Compliant
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
how can I save a character stack to a string and then save that string into...
how can I save a character stack to a string and then save that string into a new string arraylist in java? so if the character stack is h, e, l, l, o i want it to save "hello" to a string and then put that string into an array list.
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in the "How it works" section), where you take some user-defined number of steps that randomly step in W and I and are scaled by a step_size. This means you don't want to take steps of a discrete size, but instead use a random distribution of step sizes that are scaled by your step_size variable. You'll want to take steps that have an equal chance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT