+ 7

No numerals, easier way to solve it on Python?

I solved the No numerals problem, but i know that there must be a better way to solve it: I solved it replacing each number in one line each one... f=input() ften=f.replace("10","ten") f1=ften.replace("1","one") f2=f1.replace("2","two") f3=f2.replace("3","three") f4=f3.replace("4","four") f5=f4.replace("5","five") f6=f5.replace("6","six") f7=f6.replace("7","seven") f8=f7.replace("8","eight") f9=f8.replace("9","nine") f0=f9.replace("0","zero") print(f0) but how i can do it better?

19th Dec 2019, 4:14 PM
David Martínez
David Martínez - avatar
24 odpowiedzi
+ 21
x = input() dic = { "10": "ten", "0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", } for key, value in dic.items(): x = x.replace(key, value) print(x)
31st Dec 2019, 10:33 AM
MohammadSaleh Kamyab
MohammadSaleh Kamyab - avatar
+ 5
Thanks to say I have problem with that
31st Dec 2019, 10:38 AM
MohammadSaleh Kamyab
MohammadSaleh Kamyab - avatar
+ 4
Funny. I did it about like that and passed anyway, although Kitten must be right.
19th Dec 2019, 6:43 PM
HonFu
HonFu - avatar
+ 3
How? That is the question
31st Dec 2019, 11:02 AM
David Martínez
David Martínez - avatar
+ 2
For what is required it's a good solution. For a slightly neater way, I first defined a dictionary with all numbers in it as keys and all names of numbers in it as values. I also defined a string '0123456789' Then iterated through the input finding elements that are in the string of numbers and replacing them with their corresponding value in the dictionary. Make sure to replace '10' with 'ten' before this for loop though. Side note, I first used: for i in range(len(input)): and iterated over with input[i], but this didn't finish the job as the length of the string was longer by the end so it never reached the last digit/s Make use of: for i in input:...
30th Dec 2019, 3:09 PM
Liam Rogers
Liam Rogers - avatar
+ 2
But 10 is onezero... or not?
31st Dec 2019, 10:35 AM
David Martínez
David Martínez - avatar
+ 1
All numbers? They are infinite :P And what happens with the 10, 11, ...?
30th Dec 2019, 3:15 PM
David Martínez
David Martínez - avatar
+ 1
David Martínez I only did 0-10 as the question asked, I’m sure there would be a way to extend this. Maybe check: if input[i+1] in string and then change to the tens word for that number. And if you go from the end of the string backwards I think is would be easier to implement
30th Dec 2019, 11:10 PM
Liam Rogers
Liam Rogers - avatar
+ 1
David Martínez, if you make sure that 10 is replaced *first*, after that transformation only genuine 1s remain.
31st Dec 2019, 10:59 AM
HonFu
HonFu - avatar
+ 1
You can replace the 10 separately and then write a loop for the rest. This was my version: inp=input().replace('10', 'ten') for a, b in zip( (str(i) for i in range(10)), 'zero one two three four five six seven eight nine'.split()): inp = inp.replace(a, b) print(inp)
31st Dec 2019, 6:17 PM
HonFu
HonFu - avatar
+ 1
And the same thing for 11, 12, 13...
31st Dec 2019, 6:26 PM
David Martínez
David Martínez - avatar
+ 1
No, the task description says that we aren't supposed to touch everything beyond ten.
31st Dec 2019, 6:33 PM
HonFu
HonFu - avatar
+ 1
Hi Guys. Nice debate.... Why bother with a for loop when you have a dict ready to call the values. A string input was the request. Dict = {'1': 'one', '2': 'two', Etc, '10': 'ten', } If the input refers directly to the key of the dictionary, you can print out the value
1st Jan 2020, 6:36 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
y = input().split() nums=list('123456789')+['10'] for x in range(len(y)): if y[x] in nums: y[x] = y[x].replace('1','one') y[x] = y[x].replace('2','two') y[x] = y[x].replace('3','three') y[x] = y[x].replace('4','four') y[x] = y[x].replace('5','five') y[x] = y[x].replace('6','six') y[x] = y[x].replace('7','seven') y[x] = y[x].replace('8','eight') y[x] = y[x].replace('9','nine') print(' '.join(y))
23rd May 2020, 2:11 AM
Hust
+ 1
Guys why don't u make a translator I made this it had one problem replacing 10 but thanks to Honfu Suggestion it is solved check this code import re numwords = ["zero","one","two","three","four","five","six","seven","eight","nine","ten"] le = input().replace("10",numwords[10]) def convertor(phrase): translated = "" for letter in phrase: if letter in "0123456789" : translated += numwords[int(letter)] else: translated += letter else: translated.replace("onezero",str(numwords[10])) return translated print(convertor(le))
21st Oct 2020, 6:00 AM
Eon
Eon - avatar
+ 1
My solution: sentence = input() import re if re.search("10", sentence): sentence = re.sub(r"\b10\b","ten", sentence) if re.search("1", sentence): sentence = re.sub(r"\b1\b","one", sentence) if re.search("2", sentence): sentence = re.sub(r"\b2\b","two", sentence) if re.search("3", sentence): sentence = re.sub(r"\b3\b","three", sentence) if re.search("4", sentence): sentence = re.sub(r"\b(4)\b","four", sentence) if re.search("5", sentence): sentence = re.sub(r"\b(5)\b","five", sentence) if re.search("6", sentence): sentence = re.sub(r"\b(6)\b","six", sentence) if re.search("7", sentence): sentence = re.sub(r"\b(7)\b","seven", sentence) if re.search("8", sentence): sentence = re.sub(r"\b(8)\b","eight", sentence) if re.search("9", sentence): sentence = re.sub(r"\b(9)\b","nine", sentence) print(sentence) Please tell me what are thinking about my solution.
27th Feb 2022, 9:20 PM
Przemysław Komański
Przemysław Komański - avatar
+ 1
I think to replace 0 to 10 we should do it with list arr = ['zero','one','two','three','four','five','six','seven','eight','nine','ten'] st = input() for i in range(11)[::-1]: st = st.replace(str(i),arr[i]) print(st)
3rd Aug 2022, 10:01 AM
Jeet Solanki
+ 1
What's wrong with my code I can't pass the 3rd test case string_1 = input() string = string_1.split() no = "1234567890" extract_index = [] act_no = [] for i in string: if i in no: act_no += i no_index = string.index(i) extract_index.append(no_index) dict = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six", '7': "seven", '8': "eight", '9':"nine", '0': "zero",'10':"ten"} for i in range(len(act_no)): string[extract_index[i]] = dict.get(act_no[i]) for i in string: print(i,end=" ")
10th Sep 2022, 2:56 PM
Joel Justin
Joel Justin - avatar
+ 1
phrase=input() list=phrase.split(' ') dict= { '0':"zero", '1':"one", '2':"two", '3':"three", '4':"four", '5':"five", '6':"six", '7':"seven", '8':"eight", '9':"nine", '10':"ten" } output=[] for i in list: if i in dict: x=dict[i] output.append(x) else: output.append(i) result=" ".join(output) print(result)
2nd Nov 2022, 12:47 PM
Yadhu Krishna P R
Yadhu Krishna P R - avatar
+ 1
I made this option txt=list(input().replace("10","ten")) new_txt="" num_dict={ 0:"Zero", 1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine" } for i in txt: try: new_txt+=num_dict.get(int(i)) except ValueError: new_txt+=i print(new_txt) I don't know why I did int in dictionary
20th Mar 2023, 1:52 PM
Danil Ryaboshtanov