Text Decompression. One of my test cases failed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Text Decompression. One of my test cases failed

This is the first medium difficulty challenge I have done and although I have probably done it in the most convoluted and messy way possible, I feel somewhat accomplished in that I have at least passed most of the test cases. My questions are, how could my code be tidied up, and how could I make sure the final test case also passed? txt=input() def split(txt): return[char for char in txt] def num(lst): nums=[] letts=[] for x in lst: if x.isdigit()==True: nums.append(x) else: letts.append(x) for i in range(0, len(nums)): nums[i]=int(nums[i]) zip1=zip(letts, nums) dict1=dict(zip1) return(dict1) def decom(y): fin="" for key in y: fin+=(key*y[key]) return(fin) print(decom(num(split(txt))))

27th Apr 2022, 11:00 PM
Dan Hewes
6 Answers
+ 3
The one testcase that failed probably has double or higher digit numbers. You only split off single digits.
28th Apr 2022, 5:50 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
If it helps, itertools has a groupby function. For example, import itertools as it iter(''.join(x) for (_, x) in it.groupby(s, str.isdigit)) This will give you an iterator over all strings and numbers in a string s separated. For example, s = '1a33b54q2a' will be split into ['1', 'a', '33', 'b', '54', 'q', '2', 'a']
28th Apr 2022, 7:57 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Good luck 🍀👍
28th Apr 2022, 7:50 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Ah that makes sense, back to the drawing board then
28th Apr 2022, 7:31 AM
Dan Hewes
+ 1
Ah thank you, that sounds ideal, I think I'm going to need to go back to the lessons on lambda
28th Apr 2022, 8:10 AM
Dan Hewes
+ 1
here is a try, that is following a different approach. i am sure that it will not win the first price in pythonocness. 😁 i have included some comments, also some print-outs, thst can be removed. so finally it is just 4 lines of code. https://code.sololearn.com/cTDldqyVByXJ/?ref=app
28th Apr 2022, 2:35 PM
Lothar
Lothar - avatar