+ 7

[Solved] No Numerals

I tried to solve this question: Task: Take a phrase and replace any instances of an integer from 0-10 and replace it with the English word that corresponds to that integer. Sample Input: i need 2 pumpkins and 3 apples Sample Output: i need two pumpkins and three apples This is my code: original_text = input().lower() arr = ["zero","one","two","three","four","five","six","seven","eight","nine","ten"] lst = original_text.split(" ") for i in lst: try: if i.isdigit() & int(i) <= 10: original_text = original_text.replace(i,arr[int(i)]) except: pass print(original_text) It worked for test cases 1,2,3 and 6 But it failed in test cases 4 and 5. I don't know what is the problem and if I could write a better code. Thank you for your help.

23rd Mar 2022, 3:32 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
25 Réponses
+ 11
Simba You're right. My code was right at the beginning just needed to delete the lower() method. I put that because in the description explicitly said that do the lower case I think. By the way thank you all guys for participating. ❤❤❤
23rd Mar 2022, 9:59 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
+ 6
I believe you didn't try this yet original_text = input()
23rd Mar 2022, 5:56 PM
Simba
Simba - avatar
+ 2
Hello Siavash Kardar Tehran Your code is not working for this case: 0 cats and 10 dogs output: zero cats and 1zero dogs
23rd Mar 2022, 3:57 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Brian I don't think that's the case, because both issigit() and <= return True or False.
23rd Mar 2022, 5:23 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
+ 2
rodwynnejones using replace in this way has the problem that it does not discriminate what it replaces. "2nd" becomes "twond". "1000" becomes "tenzerozero". You should replace word by word rather than the whole line at once.
25th Mar 2022, 2:35 AM
Brian
Brian - avatar
+ 2
Korkunç TheTerrible because there are some test cases have uppercase letters. For example, Cat 10 should return Cat ten not cat ten
25th Mar 2022, 11:50 AM
Simba
Simba - avatar
+ 1
i would do it this way: arr = { '0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten' } original_text = input().lower().split() new_text = [] for i in original_text: if i in arr.keys(): new_text.append(arr[i]) else: new_text.append(i) print(' '.join(new_text))
23rd Mar 2022, 4:25 PM
Murodilla Karimov
+ 1
The problem I see right away is that you are mistakenly using bitwise & in the test for single-digit numbers. It will fail for even numbers because their bit0 will be zero. Try changing to logical and. Edit: On closer inspection I see that the fix won't solve all the problems. I think 0 and 10 will yet fail.
23rd Mar 2022, 5:13 PM
Brian
Brian - avatar
+ 1
Siavash Kardar Tehran I realized that the if statement makes no difference in this code. Really it is the exception handling that makes it work. With or without the if statement, it works the same! The reason it failed the tests is that the replace function replaces all occurrances. So it would replace a 1 as one, and also replace a later occurrance of 100 as one00. Here is my fix: original_text = input() #.lower() arr = ["zero","one","two","three","four","five","six","seven","eight","nine","ten"] lst = original_text.split(" ") for i in range(len(lst)): try: lst[i] = arr[int(lst[i])] except: pass print(' '.join(lst)) Edit: One more tweak is needed to pass the Code Coach tests. Remove the conversion of the input into lowercase.
23rd Mar 2022, 5:56 PM
Brian
Brian - avatar
+ 1
I agree...for the code you wrote...You used string.split()...I didn't, I used string.replace() method.
24th Mar 2022, 11:04 PM
rodwynnejones
rodwynnejones - avatar
+ 1
mydict = { "10": "ten", "9": "nine", "8": "eight", "7": "seven", "6": "six", "5": "five", "4": "four", "3": "three", "2": "two", "1": "one", "0": "zero" } # user input not used, just a simple string to test 1, 0, and 10 # are correctly replaced. mystring = "i need 1 pumpkins and 10 apples 0 oranges" for x in mydict.keys(): mystring = mystring.replace(x, mydict[x]) print(mystring)
24th Mar 2022, 11:31 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Ah, ok, I hadn't considered that, thank you.
25th Mar 2022, 7:04 AM
rodwynnejones
rodwynnejones - avatar
+ 1
@Simba Oh. Darn. lol. Thank you :-)
25th Mar 2022, 12:04 PM
Korkunç el Gato
Korkunç el Gato - avatar
0
Murodilla Karimov When I input: Hello 2. It fails to do the number before the dot. How to solve this?
23rd Mar 2022, 5:07 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
0
Any punctuation after numbers fails. Should I use regex?
23rd Mar 2022, 5:14 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
0
Siavash Kardar Tehran you are right about that. Even numbers in the bitwise & evaluate to 0, which is <=10. Now I am puzzled. I will look further.
23rd Mar 2022, 5:34 PM
Brian
Brian - avatar
0
Try this: import re arr = { '0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten' } original_text = input() pattern_num = r'[0-9]+' # include all numbers for i in original_text.split(): for n in re.findall(pattern_num, i): # this option for extracting all numbers from text. returns list object if n in arr.keys(): original_text = original_text.replace(n, arr[n]) print(original_text)
23rd Mar 2022, 7:48 PM
Murodilla Karimov
0
I don't recall, do the test cases include punctuation at the end of the sentences?
23rd Mar 2022, 8:58 PM
Paul K Sadler
Paul K Sadler - avatar
0
I dont recall either
24th Mar 2022, 6:17 PM
Youssef Mohamed