Text Decompressor | Sololearn: Learn to code for FREE!

+2

Text Decompressor

You need to decompress text. The compressed version has a number next to each symbol/letter, representing the amount of time that symbol should appear. For example, a2b3 is the compressed version of aabbb Task: Write a program that takes the compressed text as input and outputs the decompressed version. Input Format: A single string with letters/symbols, each followed by a number. Output Format: A string, representing the decompressed text. Sample Input: k2&4b1 from itertools import * a = input() b = list(a) c = [] while len(a)/2 > len(c): c.append(2) Output = [b[x - y: x] for x, y in zip( accumulate(c), c)] n = 0 z = [] while len(z) < len(Output): for i in Output: if Output.index(i) == n: z.append(i[0] * int(i[1])) n += 1 df = ("".join(i for i in z)) print(df) Above my code isn't passing the 5th case and it's locked, hence not able to check it. Plz help in correcting me.

5/29/2021 1:14:17 PM

Swapnil Kamdi

10 Answers

New Answer

+6

Swapnil Kamdi , i found a way of doing it without itertools. (itertools has great functions, but in this case it can also be done like in the demonstrated code) inp = "k2&14b1" char = ''.join([x if not x.isdigit() else ' ' for x in inp]).split() dig = ''.join([x if x.isdigit() else ' ' for x in inp]).split() [print(c * int(d), end='') for c,d in zip(char, dig)] the code is doing: ▪︎a comprehension that takes all "non-digit" characters and puts them in a new list, that will be joined and split again (other chars will output as " ") ▪︎a comprehension that takes all "digit" characters and puts them in a new list, that will be joined and split again (other chars will output as " ") ▪︎a comprehension that uses zip with 2 generated lists and outputs the final result

+2

Vasiliy Plz FYI. https://www.sololearn.com/coach/85?ref=app

+2

n = input() print( "".join([i*int(x) for i,x in zip(n[0::2],n[1::2])]))

+1

or maybe that your code produce an error for input where numbers are greater than 9: k21&4b1

+1

visph Vasiliy Maybe need to take the separate integars first within list. Could you plz bother to help me with code.

+1

loop over the char of the string, store the first, got the number while it's a digit (accumulate as a string), when a no digit occur (or end of string), use the stored char and the number to add to your result string... then if not end of string store the new char occuring ;)

+1

Specify from which section this test

+1

This is not available to me, since I am not a pro. These tasks are given in accordance with the passed material. I need to know the topic of the chapter in order to correctly solve this task.

0

Maybe the problem is that you do not have a condition when writing a number at the beginning of a line? Example: 5k2&4b1

0

string_input = input() first = [] second = [] i: str for i in string_input: if not i.isdigit(): second.append(i) else: first.append(i) first = [int(i) for i in first] def multipl(list1, list2): first_second = [] # Create empty list for x in range(0, len(list1)): first_second.append(list1[x] * list2[x]) # Adds each element to the list return print(''.join(first_second)) multipl(first, second)