Question

Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined above and returns the expanded (un-compressed) string. You may assume that the first value of count_tuples has a count that is greater than or equal to zero and all other counts are greater than zero.

Comments to show how to figure this out would be greatly appreciated

A data file in which particular characters often occur multiple times in a row can be compressed by a technique called run-le

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def decompressed(count_tuples):
    result = ''  # create empty string as result
    for count, char in count_tuples:  # go through each char and it's count from list
        result += char * count  # add the char count number of times to result
    return result  # return the result


# Testing the function here. ignore/remove the code below if not required
print(decompressed([(2, '0'), (4, '1'), (6, '0'), (2, '1'), (1, '0'), (5, '1')]))

00111100000011011111 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined...
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 is a relatively simple technique for compressing data; if a particular value (or substring)...

    Run-length encoding is a relatively simple technique for compressing data; if a particular value (or substring) appears multiple times in a row, it is only represented once, followed by an integer indicating the number of times it should be repeated. For example, the string "AAAAA" can be compressed to "A5" (5 occurrences of 'A'), saving three characters in the process. Define a Java method named expand() that takes a single String argument. You may assume that this String is run-length...

  • PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list....

    PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list. For example, count([x,[y,[a,2],[a,3]],[a,4]], 2) should return true. I only want to count occurrences of x,y,z not a. the base case is that any [a,int] list will have a count of 0. So any sublist with 'a' does not count. another example of what will return true is: count([x,[a,1],[a,5]], 1) The predicate must have the form count(X,Y) where X is the list structure passed and...

  • Write a function to find the smallest odd number in a list. The function takes a...

    Write a function to find the smallest odd number in a list. The function takes a list of numbers (each number is greater than or equal to 0) as an input. The function outputs a tuple, where the first number in the tuple is the smallest odd number and the second number in the tuple is the number of occurrences of that number. For example, if the input is [1, 4, 7, 3, 5, 2, 1, 3, 6] then the...

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

    Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list) bounds. if the insert_position is greater than the length of the list, insert the value at the end of the list. if the insert_position is less than or equal...

  • Write a function count_vowels(s) that takes a string as an argument and returns the number of...

    Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find...

  • Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.

    #In Coral  CHALLENGE ACTIVITY 7.1.1: Convert to binary.  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than e Output x % 2 (remainder is either e or 1) x= x / 2 Note: The above algorithm outputs the O's and 1's in reverse order. Ex: If the input is 6, the output is: 011 (6 in binary is 110; the...

  • Using Racket, write a bridgely? function that takes a list of at least 3 numbers and...

    Using Racket, write a bridgely? function that takes a list of at least 3 numbers and returns #true if it is bridgely, otherwise #false. A list of numbers is called "bridgely" if it contains at least 3 numbers, and every number in the list, except for the first and the last, is greater than both the first and the last number. Thus, these lists are bridgely: (list 1 2 3 4 5 6 5 4 3 2 1) (list 0...

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • ANSWER ONLY QUESTION #3!!!!! 2) (10 points) A moore FSM has a single infinitely long binary string r as input and a single output. The output is a logic 1 if the input changes from 0 to 1 or 1 to 0...

    ANSWER ONLY QUESTION #3!!!!! 2) (10 points) A moore FSM has a single infinitely long binary string r as input and a single output. The output is a logic 1 if the input changes from 0 to 1 or 1 to 0 For example, output is r-00101110 001110001 Design the FSM. Use full encoding. Construct a timing diagram for the input sequence shown above. Be sure and do an implication table check 3) (5 points) Show the schematic of a...

  • Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a...

    Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: The number parameter must be a default argument. -      If the default argument for number is given in the function call, only the first number of items are reversed. -      If the default argument for number is not provided in the function call, then the...

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