+ 3
#text decompressor
I wrote this program in an attempt to solve a code coach but everytime I run the code, I consistently get an error on test case #5; could any of you gurus have a look at it and help me pass all test cases? https://sololearn.com/compiler-playground/cewKXlAqz8sT/?ref=app
11 Antworten
+ 6
Test your code with something like
a2b3a2
Note that dictionary keys must be unique and may get overwritten when you try to add a key that already exists.
+ 5
Bob_Li ,
thanks for your comment and the suggestion for `non-digits`. you are right!
but remember that they also match all kinds of `white-spaces`, tabs, newlines,...
what also can be done to shorten the code is the combination of the *2 patterns in one call*:
r'([a-z]+)+([\d]+)+'
this results in a list of tuples so that we do not need to use zip() at all.
+ 3
Lisa thank you, seems like that's really the problem here, I've just tested it with b2c2b3c3
And the output was bbbccc, instead of the expected bbccbbbccc
+ 3
Kambi Alii Kalu ,
in general, you may simplify your code a bit.
> you are already using regex with pattern r"\d+" that is matching *one or more consecutive digits*.
using re.findall(...) returns a list with all sequences of digits from the input.
> we could make a second call of re.findall(...) with a pattern that matches *one or more consecutive letters*.
the pattern we need to accomplish this is: r'[a-z]+'
after this we have 2 lists with the required data.
> now we could combine them with zip(...) as already used from you.
> in the final step we can iterate through the zipped data and create the final decompressed string.
that is all, the code has a good readability with only a few lines ...
+ 3
🫡🫡Bob_Li you've made it look easier and simpler, I've just realized that I overestimated the problem
+ 2
Thank you Lothar ;your response has just made me figure out that I could simply remove the dict() from line 44 and items() from line 50 and now it works perfectly without overwriting the non_digits
+ 2
Lothar
perhaps r'[^\d]+' instead of r'[a-z]+' for non-digits so it can also pick up symbols and capital letters?
Kambi Alii Kalu
no need to convert the result of re.findall to list because it's already a list.
I added a comment in your code for a simpler version.
actually, it can be solved without regex and just using isdigit()
+ 2
Lothar
yes, r'[^\d]+' might be too generic as to include even whitespace characters. i didn't think of that... but within the input of the code coach, it should not matter much. a-z works, so it's assumed there is no \s.
combining the extraction in one operation is a nice idea. It might even be possible to do a one liner from this...
+ 1
Just remove "raise" from two if statements and it just works
0
import re
# Take the input string from user
user_input = input()
# Use regex to find pairs of non-digits followed by digits (e.g., 'a3', 'b12', '@2')
matches = re.findall(r"([^\d]+)(\d+)", user_input)
# Check if any pair is found, else raise an error
if not matches:
raise ValueError("Invalid input format. Ensure the string follows the correct pattern.")
# List to store the decompressed string items
decompressed_list = []
# Iterate through each matched pair and create the decompressed string
for char, num in matches:
# Multiply the character by the number (convert num to integer)
decompressed_list.append(char * int(num))
# Print the decompressed result as a joined string
print("".join(decompressed_list))
0
I read bt I am beginner