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).

31st Jul 2023, 8:18 AM
Karri Emmanuel Watson
Karri Emmanuel Watson - avatar
17 odpowiedzi
+ 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.
1st Aug 2023, 8:40 PM
Lothar
Lothar - avatar
+ 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.
31st Jul 2023, 8:34 AM
Lisa
Lisa - avatar
+ 5
Karri Emmanuel Watson Share your code
31st Jul 2023, 8:37 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 5
btw: the task is a code coach exercise called "no numerals" and can be found in the *community section* at *code coach*.
31st Jul 2023, 6:15 PM
Lothar
Lothar - avatar
+ 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
1st Aug 2023, 8:00 PM
Lothar
Lothar - avatar
+ 3
Attempts?
31st Jul 2023, 8:19 AM
A͢J
A͢J - avatar
+ 3
Karri Emmanuel Watson I am asking for your attempts.
31st Jul 2023, 12:05 PM
A͢J
A͢J - avatar
+ 2
Ok for sure I had written th e code but my code failed in passing 2 test cases out of 6
31st Jul 2023, 8:36 AM
Karri Emmanuel Watson
Karri Emmanuel Watson - avatar
+ 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. 😁👍
31st Jul 2023, 10:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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)
31st Jul 2023, 8:43 AM
Karri Emmanuel Watson
Karri Emmanuel Watson - avatar
+ 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 🙂
1st Aug 2023, 7:20 PM
Jakku Shashank
Jakku Shashank - avatar
+ 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👈
1st Aug 2023, 8:18 PM
Jakku Shashank
Jakku Shashank - avatar
0
As many as u can
31st Jul 2023, 8:20 AM
Karri Emmanuel Watson
Karri Emmanuel Watson - avatar
0
Ok 1 sec
31st Jul 2023, 8:41 AM
Karri Emmanuel Watson
Karri Emmanuel Watson - avatar
0
ceate a dictionary with the numeral forms as keys and long values as strings and create a simple replace() function
31st Jul 2023, 10:08 PM
Zeusofyogurt
Zeusofyogurt - avatar
0
Ok but you need to post a detailed question 🤦‍♂️
1st Aug 2023, 8:02 PM
Jakku Shashank
Jakku Shashank - avatar
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.
2nd Aug 2023, 1:09 AM
Ahmed Rami
Ahmed Rami - avatar