May-02-2019, 06:12 PM
You don't need to avoid negative figures turning into double operators. Let that happen, and then use replace on any double operators:
I would normally approach this with regular expressions. Assuming this is homework and you can't use regular expressions, I would do one loop over the input string. For each character, if it's a number character, store it in a sub-string and build that sub-string character by character. When you get to an operator, append the number and then the operator into your list, and reset the sub-string. If you get an operator after an operator (or an operator at the beginning) you know that's a negative number, and you can put that in the sub string.
text = text.replace(' - - ', ' - -')
. But that may get tricky with double spaces cropping up.I would normally approach this with regular expressions. Assuming this is homework and you can't use regular expressions, I would do one loop over the input string. For each character, if it's a number character, store it in a sub-string and build that sub-string character by character. When you get to an operator, append the number and then the operator into your list, and reset the sub-string. If you get an operator after an operator (or an operator at the beginning) you know that's a negative number, and you can put that in the sub string.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures