Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
input:
45zero 567one98twothreefour 678nine

output:
450 567198234 6789
Can anyone please help me to find the solution?

What I have tried:

string=input().split()
l=[]
for i in range(0,len(string)):
    if string[i]=='\0':
        break
    elif string[i].isdigit():
        l.append(string[i])
    else:
        if string[i:i+4]=='zero':
            l.append('0')
        elif string[i:i+3]=='one':
            l.append('1')
        elif string[i:i+3]=='two':
            l.append('2')
        elif string[i:i+5]=='three':
            l.append('3')
        elif string[i:i+4]=='four':
            l.append('4')
        elif string[i:i+4]=='five':
            l.append('5')
        elif string[i:i+3]=='six':
            l.append('6')
        elif string[i:i+5]=='seven':
            l.append('7')
        elif string[i:i+5]=='eight':
            l.append('8')
        elif string[i:i+4]=='nine':
            l.append('9')
print(*l)
Posted
Updated 4-Feb-22 7:37am

Why are you splitting the text?
Your output requires the spaces (and presumably other punctuation) and split will remove all whitespace - which includes the spaces.
Instead, think about processing the input character-by-character:
If it's a digit, add it to the output.
Otherwise, if it (and the following characters) match one of your "known words" add the digit for that to the output and skip the rest of the word.
Otherwise, add it to the output.

Think about how you would do it manually, and consider ways to "computerise" that. If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
sanjay surya 4-Feb-22 12:33pm    
can you please explain widely with code! since I am new to programming I don't understand. Very sorry!
OriginalGriff 4-Feb-22 12:47pm    
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
sanjay surya 4-Feb-22 12:55pm    
ok sorry i have posted the code what I have done. my aim is to first read all the characters in the string. then I checked for digit. if it is digit I appended to list. if the character is a string then I checked to which it matches. for example, zero I checked string[i:i+4]=='zero' then i appended 0 to list. but when next character reaches(i.e)z[e]ro i need to skip until another digit or string comes i dont know how to. at the same time i need to append to the list as a part. for example if the input is six789 eightnineeight8 then my output should be 6789 8988, the output should also separated by spaces. if ninenine is there, then i should not skip or leave because another nine should be appended. this is what my problem please help me!
Use Replace, below link has some samples on that

Replace strings in Python (replace, translate, re.sub, re.subn) | note.nkmk.me[^]
 
Share this answer
 
Comments
sanjay surya 4-Feb-22 12:44pm    
Again sorry, i cant get it. How can I process the input character by character?
thatraja 4-Feb-22 12:55pm    
Did you check that link atleast? You need patience with learning. You won't get instant or 100% perfect solutions always.

That link has many examples. Try those & take whatever you need and leave the rest.

For example multiple replace is one simple option.
Ex from that link : print(s.replace('one', 'XtwoX').replace('two', 'YYY'))
sanjay surya 4-Feb-22 13:16pm    
thank you very much! I got the solution! One thing I request you please don't be harsh or take them silly if they don't know anything! Since we are farmers, we know only to give food to people. We have not learned much about patience. Due to current technology development and poor economic background, I am forced to do learn these works. #tamizhan #india #tamilnadu #farmer
i feel sad to learn these, but i feel proud to be farmer
thatraja 4-Feb-22 14:31pm    
Trust me, my reply not even harsh.

All we're asking you is spend some more time with answers before follow-up questions.

You welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900