Click here to Skip to main content
15,891,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose a string is "1+2-3/4*5"
If I give the position as 2. Then how do I return the value 2 for the leftmost number and - for the leftmost operator

What I have tried:

I have found out for operator. But I need help for string.
op=['+','-','*','/']
    
    for i in range(len(txt)):
        if txt[i] in op:
            return(i)
            break
    else:
        return(-1)
Posted
Updated 31-May-18 1:46am
Comments
Member 13852296 31-May-18 7:07am    
Sorry forgot to add this.
If it is a 2 digit number it should return the 2 digit number adn not just the first digit of the whole string

1 solution

You need to parse the string into tokens, numbers and operators and then do the calculations from the results. See string to polish notation - Google Search[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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