I need help solving a problem! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I need help solving a problem!

You write a phrase and include a lot of number characters (0-9), but you decide that for numbers 10 and under you would rather write the word out instead. Can you go in and edit your phrase to write out the name of each number instead of using the numeral? 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. Input Format: A string of the phrase in its original form (lowercase). Output Format: A string of the updated phrase that has changed the numerals to words. https://code.sololearn.com/cQ5N1Oo1ioWb/?ref=app

31st Jul 2022, 12:31 AM
Camillo
Camillo - avatar
13 Answers
+ 1
It's a start, but it needs work. There is a problem when it comes to looking for '10'. Checking one character at a time won't work. Nor will it work to expand the check to two characters, because then what happens when it encounters '100'? Or '1st'? A full solution requires comparing whole words. The split() string method comes in handy to break up the input by spaces.
31st Jul 2022, 5:06 AM
Brian
Brian - avatar
+ 1
Sobola Gabriel think further about what happens with the global replacement approach. Input: 2010 Output should be 2010. But instead, after the blind replacements the output is: -> 20ten -> two0ten -> twozeroten It doesn't work.
1st Aug 2022, 5:46 PM
Brian
Brian - avatar
+ 1
Brian what you tells the program to do for you is to replace numbers from 10 downward with their equivalents words so 2010 would be seen as twozeroten and not a single no as two thousand and ten( 2010)
1st Aug 2022, 9:29 PM
Sobola Gabriel
Sobola Gabriel - avatar
+ 1
Sobola Gabriel I tested your sentence, "This is machie3456nd to have34 yo2r to 10 pla2ce", with my Code Coach solutions in five languages and got correct output from each one: "This is machie3456nd to have34 yo2r to ten pla2ce" Like most things, the solution is easy when you know how. A simple solution in Python takes only five lines of code. Use a dictionary, the input function, the split function, a while loop, one if statement and a print statement.
2nd Aug 2022, 7:28 AM
Brian
Brian - avatar
0
Thank you in advance!
31st Jul 2022, 12:31 AM
Camillo
Camillo - avatar
0
Janne using the simple .replace on the whole string also fails when the input includes "100" or "1st".
1st Aug 2022, 7:43 AM
Brian
Brian - avatar
0
I think accounting for the "10" in the phrase is a good way to start by first replacing all 10s with "ten" then others follow
1st Aug 2022, 3:38 PM
Sobola Gabriel
Sobola Gabriel - avatar
0
Sobola Gabriel the task is to replace only numbers that are "10 and under", since 2010 > 10 it is not to be replaced at all.
2nd Aug 2022, 12:14 AM
Brian
Brian - avatar
0
Brian for files for with nos greater than 10 that are few say like (10 - 20) of them, one might need to first replace those nos by other characters (say "#', "*", "?", etc) thereafter proceeds to first replacing 10, then others, and finally replace the "#", "*", "?", etc by the large nos equivalent. But this won't be possible on large files. If you considering your solution up there(breaking the input by spaces) then some of the elements of the input (large nos) will have to stand alone, otherwise if you have something like "This is machie3456nd to have34 yo2r to 10 pla2ce", you know it might not be possible.
2nd Aug 2022, 6:53 AM
Sobola Gabriel
Sobola Gabriel - avatar
0
Brian definitely your code is going to work perfectly with that sentence, what if 3456 is a single number standing alone. Won't your code change it to threefourfivesix?
2nd Aug 2022, 4:19 PM
Sobola Gabriel
Sobola Gabriel - avatar
0
Sobola Gabriel "3456" will work just as well because when it converts the whole word to a numeric value, it is greater than 10 (more specifically, the number is not found in the dictionary keys) so the word passes through without being replaced.
2nd Aug 2022, 5:40 PM
Brian
Brian - avatar
0
Brian great work by you 👍 then
2nd Aug 2022, 6:19 PM
Sobola Gabriel
Sobola Gabriel - avatar
- 1
Take the string as an input and do string = string.replace("1", "one") etc… But replace at first because otherwise the program will replace 1 and 0 to one zero instead of ten
1st Aug 2022, 3:39 AM
Janne
Janne - avatar