+ 5
No numeral
Hi, i cannot solve this problem in code coach My first try is: a=str(input()) a=a.replace("0", "zero") a=a.replace("1", "one") a=a.replace("2", "two") a=a.replace("3", "three") a=a.replace("4", "four") a=a.replace("5", "five") a=a.replace("6", "six") a=a.replace("7", "seven") a=a.replace("8", "eight") a=a.replace("9", "nine") a=a.replace("10", "ten") print(a) But the hidden test case 3 is false, so i thought the problem is numer bigger than 10 (f.e: 12 will become onetwo) So here is my second try: https://code.sololearn.com/c1DVm102H33m/?ref=app It take care of all number from 0-10 and will print 12 as 12, but the hidden test case 3 is still false 😭 Can someone please help me or explain the problem with my code?
7 Réponses
+ 7
Hieu Le Chi ,
have a look at the code for a special test case:
https://code.sololearn.com/cA76jNJY7s2T/?ref=app
+ 11
Hieu Le Chi ,
even if you correct your code according to some recommendations here, the result will fail. the reason is, that you use the input string for iteration and processing. this will lead to a result of '.. oneone ...' if the string contains the number 11 and do on...
you should try to follow this:
▪︎the input string has to be split to individual words (this will create automatically a list)
▪︎use this list and iterate through it by using a for loop
- if word in the loop variable does contain a number (using isdigit()), replace the element in the list (number) with the 'word number'. to do so, you need an index number to access the respective element
- in each other case do nothing
▪︎after iteration through list is finished, the list should contain the correct words
▪︎for doing the output of the list, use print(" ".join(... your_list_name))
+ 7
If input is 10 cats output should be ten cats not one zero cats.
You can start with 10 to correct it.
a=str(input())
a=a.replace("10", "ten")
...
+ 6
Thank you guys so much, I have finally solved the problems. What a great community we have here 😊
P.S: here is my finished code
https://code.sololearn.com/c2t3O09Dstz0/?ref=app
+ 5
Your code doesn't works for input like "1,0" and also it is " 10 ":" ten ". (watch the Space before ten)
+ 2
[EDITED]
Hieu Le Chi Here are two solutions:
print(*map(lambda x: ("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")[int(x)] if x.isdigit() and 0 <= int(x) < 11 else x, input().split()))
# Hope this helps
+ 1
Lothar,
Thank you much for improving my code 🙏🤝, you are amazing 😊