0
Solve it in python
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).
17 Antworten
+ 5
Jakku Shashank ,
the code is working now properly and passes all test cases. nevertheless, i would recommend revising the code.
> a common way is using a dictionary that holds key / value pairs like already shown in a previous post. then run a loop to perform the changes.
+ 7
Q&A is for getting help with your own code. It is not a code writing service.
If you need help, show your own code attempt so we can make suggestions how to fix it.
+ 5
Karri Emmanuel Watson Share your code
+ 5
btw:
the task is a code coach exercise called "no numerals" and can be found in the *community section* at *code coach*.
+ 4
Jakku Shashank ,
> sorry to say, but your solution does not solve the task. replace is modifying all occurrences of a character (digit) and not followjng the task description.
input sample:
hello 1 2 9 10 11 1234 end
gives this result:
hello one two nine onezero oneone onetwothreefour end
the correct result should be:
hello one two nine ten 11 1234 end
+ 3
Attempts?
+ 3
Karri Emmanuel Watson
I am asking for your attempts.
+ 2
Ok for sure I had written th e code but my code failed in passing 2 test cases out of 6
+ 2
Karri Emmanuel Watson
The .lower() is causing your code to fail the 2 tests.
You wrote a good code to meet the description, but the description was misleading.
I'm pretty sure they intended that only the numbers being converted should be lower case, but other words would keep their original format.
😁👍
+ 1
def replace_numbers_with_words(phrase):
num_to_word = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
'10':"ten"
}
words = phrase.split()
for i, word in enumerate(words):
if word.isdigit() or word.isspace() and int(word)<= 10:
words[i] = num_to_word[word]
new_phrase = ' '.join(words)
return new_phrase
original_phrase = input()
new_phrase = replace_numbers_with_words(original_phrase.lower())
print(new_phrase)
+ 1
X=input()
X=X.replace("0", "zero")
X=X.replace("1", "one")
X=X.replace("2", "two")
X=X.replace("3", "three")
X=X.replace("4", "four")
X=X.replace("5", "five")
X=X.replace("6", "six")
X=X.replace("7", "seven")
X=X.replace("8", "eight")
X=X.replace("9", "nine")
X=X.replace("10", "ten")
print(x)
For more Code solutions check out my profile 🙂
+ 1
str=input()
lis=str.split()
lis2=[]
for i in lis:
if(i=='1'):
lis2.append('one')
elif(i=='2'):
lis2.append('two')
elif(i=='3'):
lis2.append('three')
elif(i=='4'):
lis2.append('four')
elif(i=='5'):
lis2.append('five')
elif(i=='6'):
lis2.append('six')
elif(i=='7'):
lis2.append('seven')
elif(i=='8'):
lis2.append('eight')
elif(i=='9'):
lis2.append('nine')
elif(i=='0'):
lis2.append('zero')
elif(i=='10'):
lis2.append('ten')
else:
lis2.append(i)
for i in lis2:
print(i,end=' ')
Check this out👈
0
As many as u can
0
Ok 1 sec
0
ceate a dictionary with the numeral forms as keys and long values as strings and create a simple replace() function
0
Ok but you need to post a detailed question 🤦♂️
0
https://www.sololearn.com/compiler-playground/c1Tjqz7aYpPF
Here it is, it's a simplified code that will do as you want, until I upgrade it and correct errors.