+ 1
Help
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. Sample Input: i need 2 pumpkins and 3 apples Sample Output: i need two pumpkins and three apples https://code.sololearn.com/cEupo8ml8XTi/?ref=app [Solved]
7 odpowiedzi
+ 3
Raghunadh Narne that's because you are scaning the string character by character so 10 will be treated as 1 and 0 interested of 10 together
+ 1
Raghunadh Narne phrase=phrase.replace(i,link[int(i)])
link[int(i)] not link[i] ,i is a string ,input() function returns a string and you need to cast it to int if you are looking for a integer key in link dictionary ,
Your solution will still not pass for input containing number like "10" ,
0
Just take string keys as '0' instead of Integer 0.
I think all works..
Edit :
Oh. Yes. The replace function replacing all occurences of argument that first 0 is replacing by zero in 0,10 both. So first replace 10 by taking 10 as first key..
Or use other approach of splitting and replacing then use join.
Edit :
replace working like replaceAll of java...
Is may be there other method for replace Fisrt..! Like in java!
0
Raghunadh Narne yes
0
Jayakrishna🇮🇳 No, it's still failing for 10.
It's printing onezero insted of ten
0
Raghunadh Narne you can simply use split() method of string ,that stores the strings in a sentence seperated by spaces or the argument type you provide to split()
Then you will be iterating over "10" instead of "1" and "0" seperately
for i in phrase.split():
the default argument for it is space
0
Raghunadh Narne
Oh. Yes. The replace function replacing all occurences of argument that first 0 is replacing by zero in 0,10 both. So first replace 10 by taking 10 as first key..
Or use other approach of splitting and replacing then use join.
Or
Just replace all keys with values in input string by iterating dictionary instead of input string..