Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 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))))
6 Answers
+ 3
The one testcase that failed probably has double or higher digit numbers. You only split off single digits.
+ 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']
+ 2
Good luck 🍀👍
+ 1
Ah that makes sense, back to the drawing board then
+ 1
Ah thank you, that sounds ideal, I think I'm going to need to go back to the lessons on lambda
+ 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