+ 3

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.

29th May 2021, 1:14 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
18 odpowiedzi
+ 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
29th May 2021, 3:02 PM
Lothar
Lothar - avatar
+ 6
n = input() print( "".join([i*int(x) for i,x in zip(n[0::2],n[1::2])]))
15th Jul 2021, 6:16 PM
Almantas Seikis
Almantas Seikis - avatar
+ 4
This one work too: txt = input() tab = list(txt) for i in range(0,len(tab),2): print(tab[i]*int(tab[i+1]),end="")
1st Feb 2022, 7:35 AM
Moussa Diallo
Moussa Diallo - avatar
+ 2
or maybe that your code produce an error for input where numbers are greater than 9: k21&4b1
29th May 2021, 1:35 PM
visph
visph - avatar
29th May 2021, 2:57 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
+ 2
Shortest and easiest way to get it done: compressed_string = input() decompressed_string = " " for i in compressed_string: if not i.isdigit(): decompressed_string += i * int(compressed_string[(compressed_string.index(i)+1)]) print(decompressed_string)
18th Nov 2021, 5:02 PM
Mas'ud Saleh
Mas'ud Saleh - avatar
+ 2
def decompress(x): symbol = x[::2] mult = x[1::2] decompress = [i*int(j) for i,j in zip(symbol,mult)] print(''.join(decompress)) decompress(input())
13th Dec 2021, 4:51 AM
Mateo González Bufi
Mateo González Bufi - avatar
+ 2
That works and is short -> str = list(input()) for i in range(0,len(str),2): print(str[i]*int(str[i+1]),end="") Hope it helps!
22nd Sep 2022, 2:38 PM
Simon Ludwig
+ 2
word=str(input()) l=[w for w in word] n=l[1::2] c=l[::2] d=[int(v) for v in n] result=[x*y for x,y in zip(c,d)] print("".join(result[0::]))
24th May 2023, 12:39 PM
Nkeh Bebey
Nkeh Bebey - avatar
+ 1
visph Vasiliy Maybe need to take the separate integars first within list. Could you plz bother to help me with code.
29th May 2021, 1:42 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
+ 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 ;)
29th May 2021, 2:07 PM
visph
visph - avatar
+ 1
Specify from which section this test
29th May 2021, 2:39 PM
Solo
Solo - avatar
+ 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.
29th May 2021, 3:02 PM
Solo
Solo - avatar
+ 1
This works great too 😊 i = list(input()) ns = "" for x in range(len(i))[::2]: ns += (i[x] * int(i[x+1])) print(ns)
27th Jun 2022, 2:28 PM
Martin Valdés Mallaug
+ 1
decompressor = input() decomp = list(decompressor) x=[] y=[] for i in decomp: if i.isnumeric(): x.append(int(i)) else: y.append(i) text='' for n in range(0,len(x)): text += (x[n]*y[n]) print(text) An alternative to some of the solutions above.
5th Jan 2023, 9:57 AM
ken baumli
ken baumli - avatar
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
29th May 2021, 1:34 PM
Solo
Solo - avatar
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)
6th Sep 2021, 8:41 PM
Johny Bracker
Johny Bracker - avatar
0
This the code I figured out
24th May 2023, 12:40 PM
Nkeh Bebey
Nkeh Bebey - avatar