Code coach question | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grátis
+ 8

Code coach question

How do we replace a number in a sentence with the relevant name of that number? (eg:- I have 3 pens-> I have three pens)

26th Jan 2021, 2:08 PM
Kalindu Wanasinghe
Kalindu Wanasinghe - avatar
5 Respostas
+ 8
Thank you very much
26th Jan 2021, 3:17 PM
Kalindu Wanasinghe
Kalindu Wanasinghe - avatar
+ 7
[Edited *] Kalindu Wanasinghe(Not Yet) , you could do it with string replace() but there is also an other way to do it. (python) ▪︎ put all your numerals in a dictinary as key : value pair like {1: "one", ...} ▪︎ split the input sentence to a list ▪︎ iterate over that list by using a for loop and check if the current word is in the dictionary ▪︎ if YES, use this word to find the keyword and use the value of it to replace the current element of the list ▪︎ in NO, continue in looping ▪︎*if looping is finished, the elements of the list can be put together with join() to a string for output
26th Jan 2021, 3:17 PM
Lothar
Lothar - avatar
+ 4
make an array of that number range names . a=["zero","one","two"] and when looping over string you encounter an integer ,use it as an index to get relevant name from that array a .
26th Jan 2021, 2:11 PM
Abhay
Abhay - avatar
+ 1
That is correct
28th Jan 2021, 9:21 AM
Dj Kali
0
Create array of numbers like Num[]={"no","one",... "ten"};
28th Jan 2021, 12:17 PM
Nurmuhammet Tajimyradow
Nurmuhammet Tajimyradow - avatar