Question

Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive...

Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive repeated characters are replaced with one of the character and the number of times the character is repeated. This is also known as "Run length encoding". Examples:“aaabbc” -> “a3b2c”“abc” -> “abc”“aaqakkaccc” -> a2qak2a2c3

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def compressString(text):

    # Variable to hold the result
    # Add first character of given string to resultantString
    resultantString=text[0]

    # Counter variable to keep the track of how many times a character is repeated
    # Set count =1, because a character will always appears at least 1 time
    count=1

    # Iterate the loop till the length of given string - 1
    # because it will be compared in if statement
    for i in range(0,len(text)-1):

        # Check if adjacent characters are same
        # If yes, increment count
        # Keep incrementing count, till a character is repeated
        if text[i]==text[i+1] :
            count+=1
        else:
            # Else, check if count > 1
            # If yes, append value of count to resultant string
            if count>1 :
                resultantString+=str(count)

            # Append next to resultant string as well
            resultantString+=text[i+1]
            # Re-set count to 1
            count = 1
    # Now, the count for the last characters have been calculated
    # Check if it's > 1, if yes, append to resultant string
    if count> 1:
        resultantString+=str(count)

    # return resultant string
    return resultantString

if __name__ == '__main__':
    print ('aaabbc - ',compressString('aaabbc'))
    print('abc - ',compressString('abc'))
    print('aaqakkaccc - ',compressString('aaqakkaccc'))

SCREENSHOT-

汤compressString.py A def copressstring(text): # variable to hold the result # Add first character of given string to resultan32 if count 1 resultantStringtstr (count) 34 35 36 37 # return resultant string return resultantString 38if namemain : T: 39


OUTPUT-

compressString C:Users\mahes\AppData Local\Programsv aaabbca3b2c abcabc aaakkaccc a2qak2ac3 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data...

    Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of: the number of characters in...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • On Python 3.6, write a function char_table(s), where s is a string. The function should produce...

    On Python 3.6, write a function char_table(s), where s is a string. The function should produce a table that shows the characters in the string and the number of occurrences of each character. Make the table nicely formatted, using right justification for the numbers (check out the format command.). List the characters in order (the order in the character encoding). Give some examples of the output.

  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE...

    Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE MUST BE RECURSIVE. Do not use loops (while, do/while, or for). Do not declare any variables outside of a method. You may declare local variables inside a method. Complete the following method: public static String decompress(String compressedText): Decompress the input text, which has been compressed using the RLE algorithm (previous hw assignment): Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • In python Write a function called removeExtensión that is given the name of a file as...

    In python Write a function called removeExtensión that is given the name of a file as a string and returns the same string without the extension (the last "" and any following characters); if the given string has no extension, return the given string without changing it. For example, removeExtension"Chapter 3.9.txt") should return "Chapter 3.9" while removeExtension("Hello") should return "Hello"

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

  • Using C++, and <iostream> if possible, ​ Write a program that can compress a text file...

    Using C++, and <iostream> if possible, ​ Write a program that can compress a text file consisting of English language alphabet A-zl using run-length encoding scheme. (Weight 50%) Run-Length Encoding The key idea is to replace the repeating character by itself and the number of times that it has been repeated. Consider the following string HELLO WORLD This can be re-written as HEL20 WORLD Notice that since LL was repeated, it was repeated by L2. 2 here indicates that letter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT