+ 2
Only test case 3 is the problem
https://sololearn.com/coach/85/?ref=app Here’s my code: a=input() b="" c="1234567890" for i in a: if i not in c: continue else: j=a.index(i) b+=a[j-1]*int(i) print(b)
8 odpowiedzi
+ 4
This is only the link to the task. Please show **your code**
+ 7
chess doctor ,
there is an issue in the code since it uses the index() function to find the position of the number. as long as numbers are unique, there is no problem.
if a number repeats in the input string, the result will be incorrect. the reason is that index() allways finds the *first* occurrence of the value.
let us take this input *k2&4b1a2* and run it with the current code. the result will be:
*kk&&&&bkk* but it should be: *kk&&&&baa*
> so you should rework the algorithm how you access the *pair* of *CharNum*. it can be done with a range object that creates index nunbers with a
step-width of 2.
> the following code has to run in a loop.
we can use a slice that creates the pairs. we can multiply char * number. the result can be concatenated to an output string.
+ 6
Ali M ,
for this code coach the pattern of *CharNumCharNum...* is using only 1 × char and 1 × number. so we don't have to caer about *CharCharNum...*
+ 2
Got it…. Thanks lisa and luther
+ 1
Suppose the input was "a2b2". Your code would output "aaaa" as index() only gets the first occurrence of the character in the string.
0
I didn't do that code yet, but what iif the input was "kk2", do you think that could be the problem?🤔
0
i see, thanks for the explanation, Lothar
0
Here’s a different approach, less code - same output:
c = input()
for i, v in enumerate(c):
if (v.isdigit()):
print(c[i-1] * int(v))
You can use isdigit() instead of creating a string of digits and checking against 👍