+ 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

4th Jan 2025, 4:25 PM
Kambi Alii Kalu
Kambi Alii Kalu - avatar
11 Respuestas
+ 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.
4th Jan 2025, 5:08 PM
Lisa
Lisa - avatar
+ 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.
5th Jan 2025, 7:29 AM
Lothar
Lothar - avatar
+ 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
4th Jan 2025, 6:40 PM
Kambi Alii Kalu
Kambi Alii Kalu - avatar
+ 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 ...
4th Jan 2025, 8:47 PM
Lothar
Lothar - avatar
+ 3
🫡🫡Bob_Li you've made it look easier and simpler, I've just realized that I overestimated the problem
5th Jan 2025, 6:56 AM
Kambi Alii Kalu
Kambi Alii Kalu - avatar
+ 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
4th Jan 2025, 9:01 PM
Kambi Alii Kalu
Kambi Alii Kalu - avatar
+ 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()
5th Jan 2025, 4:04 AM
Bob_Li
Bob_Li - avatar
+ 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...
5th Jan 2025, 7:32 AM
Bob_Li
Bob_Li - avatar
+ 1
Just remove "raise" from two if statements and it just works
5th Jan 2025, 11:47 PM
Abdo
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))
5th Jan 2025, 1:12 PM
Soumyadip Das
0
I read bt I am beginner
5th Jan 2025, 6:12 PM
Ayuub Kk
Ayuub Kk - avatar