Error creating postfix expression [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 9 years ago.
I am changing infix to postfix and then evaluating it.
#!/usr/bin/python
import sys
import fileinput
import operator
operator_functions = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.div,
'%': operator.mod
}
def tokenise( line ) :
'''generator, serves up one token at a time
input - a valid, openo file handle (object)
output - next token (as a string)
side-effects - input pointer is moved'''
for tok in line.split() :
yield tok
#Define types with corresponding ID numbers to distinguish them.
OPERATOR = 1
OPERAND = 2
LEFT_PARENTHESES = 3
RIGHT_PARENTHESES = 4
def precedence(s): #Returns the precedence of the operators
if s == '(':
return 3
elif s == '+' or s == '-':
return 2
elif s == '*' or s == '/' or s == '%':
return 1
else:
return 0
def typeof(s): #Returns the type of the symbol
if s == '(':
return LEFT_PARENTHESES
elif s == ')':
return RIGHT_PARENTHESES
elif s == '+' or s == '-' or s == '*' or s == '%' or s == '/':
return OPERATOR
else :
return OPERAND
def infix2postfix(infix):
postfix = []
stack = []
for i in infix:
symbol_type = typeof(i)
if symbol_type == LEFT_PARENTHESES :
#If it is a left paren, push it onto the stack
stack.append(i)
elif symbol_type == RIGHT_PARENTHESES :
#If it is a right paren, pop operators from the stack and append to the postfix expression,
#until a left paren is encountered on the stack. Remove and discard the left paren
next = stack.pop()
while next != '(':
postfix.append(next)
next = stack.pop()
elif symbol_type == OPERAND:
#If it is an operand, append it to the postfix expression
postfix.append(i)
elif symbol_type == OPERATOR:
#If it is an operator, then pop operators from the stack and append to the postfix expression
#while the operators have equal or higher precedence than the current token. Push current
#token (operator) onto the stack
p = precedence(i)
#print i
while len(stack) != 0 and p <= precedence(stack[-1]) :
print stack
postfix.append(stack.pop())
stack.append(i)
while len(stack) > 0 : #Add to postfix
postfix.append(stack.pop())
evalPostfix(postfix) #Call evalPostFix to get result
def evalPostfix(postfix_expression):
stack = []
for token in postfix_expression :
if token in operator_functions:
no1 = int(stack.pop()) #First Number
no2 = int(stack.pop()) #Second Number
stack.append(operator_functions[token](no2, no1))
else :
stack.append(token)
print ' '.join(postfix_expression),'=',stack.pop() #The Result
##########
#Main Code
##########
if len(sys.argv) == 2: #Read from file
f = open( sys.argv[1] )
elif len(sys.argv) == 1: #Read from stdin
f = sys.stdin
else:
print 'Invalid Number of arguments. Supply either no arguments or an input file.'
sys.exit(0)
lines = [line.strip() for line in f]
for i in lines:
arr=[] #Array containing all infix expressions
tokens = tokenise( i )
for t in tokens :
#print t
arr.append(t)
infix2postfix(arr) #Call infix2postfix
Input example:
1 + 2 - 3
Output example:
1 2 + 3 - = 0
That works as desired, but when I try this:
Input:
13 + 23 - 42 * 2
I get
Output:
13 23 + 42 - 2 * = -12
Instead of:
13 23 + 42 2 * - = -48
I am not sure what is going wrong. Any ideas?

Use == instead of is operator , there is difference between is and ==, is check returns True when both objects are same == returns true if value are equal.
additionally, Condition:
s is '+' or '-':
Should be:
s == '+' or s == '-':
Note an non-empty string is always true e.g.
>>> bool('')
False
>>> bool('+')
True

Related

How to understand a question about inserting occurrences? Python [duplicate]

I came across this exercise of checking whether or not the simple brackets "(", ")" in a given string are matched evenly.
I have seen examples here using the stack command which I haven't encountered yet. So I attempted a different approach. Can anyone tell me where I am going wrong?
def matched(str):
ope = []
clo = []
for i in range(0,len(str)):
l = str[i]
if l == "(":
ope = ope + ["("]
else:
if l == ")":
clo = clo + [")"]
else:
return(ope, clo)
if len(ope)==len(clo):
return True
else:
return False
The idea is to pile up "(" and ")" into two separate lists and then compare the length of the lists. I also had another version where I had appended the lists ope and clo with the relevant I which held either ( or ) respectively.
A very slightly more elegant way to do this is below. It cleans up the for loop and replaces the lists with a simple counter variable. It also returns false if the counter drops below zero so that matched(")(") will return False.
def matched(str):
count = 0
for i in str:
if i == "(":
count += 1
elif i == ")":
count -= 1
if count < 0:
return False
return count == 0
This checks whether parentheses are properly matched, not just whether there is an equal number of opening and closing parentheses. We use a list as a stack and push onto it when we encounter opening parentheses and pop from it when we encounter closing parentheses.
The main problem with your solution is that it only counts the number of parentheses but does not match them. One way of keeping track of the current depth of nesting is by pushing opening parentheses onto a stack and popping them from the stack when we encounter a closing parenthesis.
def do_parentheses_match(input_string):
s = []
balanced = True
index = 0
while index < len(input_string) and balanced:
token = input_string[index]
if token == "(":
s.append(token)
elif token == ")":
if len(s) == 0:
balanced = False
else:
s.pop()
index += 1
return balanced and len(s) == 0
My solution here works for brackets, parentheses & braces
openList = ["[", "{", "("]
closeList = ["]", "}", ")"]
def balance(myStr):
stack = []
for i in myStr:
if i in openList:
stack.append(i)
elif i in closeList:
pos = closeList.index(i)
if stack and (openList[pos] == stack[-1]):
stack.pop()
else:
return "Unbalanced"
if len(stack) == 0:
return "Balanced"
print(balance("{[()](){}}"))
Most blatant error done by you is:
if l == ")":
clo = clo + [")"]
else:
return(ope, clo) # here
By using return, you exit from function when first char not equal to "(" or ")" is encountered. Also some indentation is off.
Minimal change which allows your code to run (although it won't give correct answers for all possible input strings) is:
def matched(str):
ope = []
clo = []
for i in range(0,len(str)):
l = str[i]
if l == "(":
ope = ope + ["("]
elif l == ")":
clo = clo + [")"]
if len(ope)==len(clo):
return True
else:
return False
The problem with your approach is that you don't consider the order. Following line would pass: ))) (((.
I'd suggest to keep the count of open and closed parenthesis:
counter starts from 0
every ( symbol increments counter
every ) symbol decrements counter
if at any moment counter is negative it is an error
if at the end of the line counter is 0 - string has matching parenthesis
a = "((a+b)*c)+(b*a))"
li = list(a)
result = []
for i in range(0, len(a)):
if a[i] == "(":
result.append(i)
elif a[i] == ")":
if len(result) > 0:
result.pop()
else:
li.pop(i)
for i in range(0, len(result)):
li.pop(result[i])
print("".join(li))
this code works fine
def matched(s):
p_list=[]
for i in range(0,len(s)):
if s[i] =='(':
p_list.append('(')
elif s[i] ==')' :
if not p_list:
return False
else:
p_list.pop()
if not p_list:
return True
else:
return False
You can do this in a couple of lines using accumulate (from itertools). The idea is to compute a cumulative parenthesis level going through the string with opening parentheses counting as level+1 and closing parentheses counting as level-1. If, at any point, the accumulated level falls below zero then there is an extra closing parenthesis. If the final level is not zero, then there is a missing closing parenthesis:
from itertools import accumulate
def matched(s):
levels = list(accumulate((c=="(")-(c==")") for c in s))
return all( level >= 0 for level in levels) and levels[-1] == 0
An alternative to check for balanced nested parentheses:
def is_balanced(query: str) -> bool:
# Alternative: re.sub(r"[^()]", "", query)
query = "".join(i for i in query if i in {"(", ")"})
while "()" in query:
query = query.replace("()", "")
return not query
for stmt in [
"(()()()())", # True
"(((())))", # True
"(()((())()))", # True
"((((((())", # False
"()))", # False
"(()()))(()", # False
"foo", # True
"a or (b and (c or d)", # False
"a or (b and (c or d))" # True
"a or (b and (c or (d and e)))", # True
]:
print(stmt)
print("Balanced:", is_balanced(stmt))
print()
It works by:
Removing everything but parentheses
Recursively remove innermost parentheses pairs
If you're left with anything besides the empty string, the statement is not balanced. Otherwise, it is.
if the parenthesis sequence is not an issue (strings like )( ) this code is faster :
def matched_parenthesis(s):
return s.count('(') == s.count(')')
Tested with 15KB string, it is ~20μs v.s. 1ms iterating over the whole string.
And for me the order is not an issue as the underlying protocol guaranties that the string is well-formed.
In case u also need to find the position of the first mismatching bracket from left u can use the below code which also cover certain edge cases:
def isBalanced(expr):
opening=set('([{')
new=set(')]}{[(')
match=set([ ('(',')'), ('[',']'), ('{','}') ])
stack=[]
stackcount=[]
for i,char in enumerate(expr,1):
if char not in new:
continue
elif char in opening:
stack.append(char)
stackcount.append(i)
else:
if len(stack)==0:
print(i)
return False
lastOpen=stack.pop()
lastindex=stackcount.pop()
if (lastOpen, char) not in match:
print (i)
return False
length=len(stack)
if length!=0:
elem=stackcount[0]
print (elem)
return length==0
string =input()
ans=isBalanced(string)
if ans==True:
print("Success")
if "(" ,")" these two characters are not present then we don't want to return true or false just return no matching found. if matching found i just checking the count of both characters are same then return true, else return false
def matched(str):
count1=0
count2=1
for i in str:
if i =="(":
count1+=1:
elif i==")":
count2+=1:
else:
print "no matching found for (,)"
if count1==count2:
return True
else:
return False
Simplest of all , though all of you guys have done good:
def wellbracketed(s):
left=[]
right=[]
for i in range(0,len(s)):``
if s[i]=='(':
left=left+['(']
elif s[i]==')':
if len(left)!=0:
right=right+[')']
else:
return False
return(len(left)==len(right))
here's another way to solve it by having a counter that tracks how many open parentheses that are difference at this very moment.
this should take care all of the cases.
def matched(str):
diffCounter = 0
length = len(str)
for i in range(length):
if str[i] == '(':
diffCounter += 1
elif str[i] == ')':
diffCounter -= 1
if diffCounter == 0:
return True
else:
return False
input_str = "{[()](){}}"
strblance=""
for i in input_str:
if not strblance:
strblance = strblance+i
elif (i is '}' and strblance[len(strblance)-1] is '{') \
or ( i is']'and strblance[len(strblance)-1] is '[') \
or ( i is ')'and strblance[len(strblance)-1] is '('):
strblance = strblance[:len(strblance)-1]
else:
strblance = strblance+i
if not strblance:
print ("balanced")
else:
print ("Not balanced")
More advanced example in which you additionally need to check a matching of square brackets '[]' and braces '{}' pars.
string = '([]{})'
def group_match(string):
d = {
')':'(',
']':'[',
'}':'{'
}
list_ = []
for index, item in enumerate(string):
if item in d.values():
list_.append(item)
elif (item in d.keys()) and (d.get(item) in list_):
list_.pop()
return len(list_) == 0
The simplest code ever!!
def checkpar(x):
while len(''.join([e for e in x if e in "()"]).split('()'))>1: x=''.join(x.split('()'))
return not x
you can check this code.
This code don't use stack operations.
def matched(s):
count = 0
for i in s:
if i is "(":
count += 1
elif i is ")":
if count != 0:
count -= 1
else:
return (False)
if count == 0:
return (True)
else:
return (False)
#function to check if number of closing brackets is equal to the number of opening brackets
#this function also checks if the closing bracket appears after the opening bracket
def matched(str1):
if str1.count(")")== str1.count("("):
p1=str1.find("(")
p2=str1.find(")")
if p2 >= p1:
str1=str1[p1+1:p2]+ str1[p2+1:]
if str1.count(")")>0 and str1.count("(")>0:
matched(str1)
return True
else:
return False
else:
return False
matched(str1)
parenthesis_String = input("Enter your parenthesis string")
parenthesis_List = []
for p in parenthesis_String:
parenthesis_List.append(p)
print(parenthesis_List)
if len(parenthesis_List)%2 != 0:
print("Not Balanced Wrong number of input")
for p1 in parenthesis_List:
last_parenthesis = parenthesis_List.pop()
print(last_parenthesis)
if (p1 == '{' and last_parenthesis == '}' or p1 == '[' and last_parenthesis == ']' or p1 == '(' and last_parenthesis == ')'):
print("Balanced")
else:
print("Not balanced")
A little different one.
expression = '{(){({)}}'
brackets = '[](){}'
stack = []
balanced = False
for e in expression:
if e in brackets and stack: # Popping from the stack if it is closing bracket
if stack [-1] == brackets[brackets.index(e)-1]:
stack.pop()
balanced = True
continue # it will go to the new iteration skipping the next if below
if e in brackets: # Push to stack if new bracket in the expression
stack .append(e)
balanced = False
balanced = 'Balanced' if balanced and not stack else 'Unbalanced'
print(balanced, stack)
just modified Henry Prickett-Morgan's code a little bit to handle it more sensibly, namely taking into account that the number of "(" matches that of ")" but string starts with ")" or ends with "(" which are apparently not right.
def ValidParenthesis(s):
count = 0
if s[0] == ')' or s[-1] == '(':
return False
else:
for c in s:
if c == '(':
count += 1
elif c == ')':
count -= 1
else:
continue
return count == 0
The best way to understand this snippet is to follow along with all kind of scenarios.
in_data = ['{','[','(']
out_data = ['}',']',')']
def check_match(statements):
stack = []
for ch in statements:
if ch in in_data:
stack.append(ch)
if ch in out_data:
last = None
if stack:
last = stack.pop()
if last is '{' and ch is '}':
continue
elif last is '[' and ch is ']':
continue
elif last is '(' and ch is ')':
continue
else:
return False
if len(stack) > 0:
return False
else:
return True
print(check_match("{www[eee}ee)eee"))
print(check_match("(ee)(eee[eeew]www)"))
print(check_match("(ss(ss[{ss}]zs)zss)"))
print(check_match("([{[[]]}])"))
def matched(str):
braces = {"{": "}", "(": ")", "[": "]"}
stack = []
for c in str:
if c in braces.keys():
stack.append(c)
elif c in braces.values():
if not stack:
return False
last_brace = stack.pop()
if braces[last_brace] != c:
return False
if stack:
return False
return True
print(matched("()"))
>> True
print(matched("(}"))
>> False
print(matched("}{"))
>> False
print(matched("}"))
>> False
print(matched("{"))
>> False
print(matched("(ff{fgg} [gg]h)"))
>> True
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
def isValid(s):
stack = []
for i in s:
if i in open_list:
stack.append(i)
elif i in close_list:
pos = close_list.index(i)
if open_list[pos] == stack[len(stack)-1]:
stack.pop()
else:
return False
if len(stack) == 0:
return True
else:
return False
print(isValid("{[(){}]}"))
s='{[]{()}}}{'
t=list(s)
cntc=0
cnts=0
cntp=0
cntc=min(t.count("{"),t.count("}"))
cnts=min(t.count("["),t.count("]"))
cntp=min(t.count("("),t.count(")"))
print(cntc+cnts+cntp)
for a balanced string, we can find an opening brace followed by it closing brace. if you do this basic check you could remove the checked substring and check the remaining string. At the end, if the string is not empty then it is not balanced.
def is_balanced(s: str) -> bool:
while any([x in s for x in ["", "", ""]]):
s=s.replace("{}", "").replace("[]","").replace("()","")
return s==""
def parenthesis_check(parenthesis):
chars = []
matches = {')':'(',']':'[','}':'{'}
for i in parenthesis:
if i in matches:
if chars.pop() != matches[i]:
return False
else:
chars.append(i)
return chars == []
foo1="()()())("
def bracket(foo1):
count = 0
for i in foo1:
if i == "(":
count += 1
else:
if count==0 and i ==")":
return False
count -= 1
if count == 0:
return True
else:
return False
bracket(foo1)
Although I'm not proposing a fix to your implementation, I suggest a cleaner and more pythonic version of the #kreld solution:
def check_parentheses(expr):
s = []
for c in expr:
if c in '(':
s.append(c)
elif c in ')':
if not len(s):
break
else:
s.pop()
else:
return not len(s)
return False
# test -----------------------------------------------------------------
test_expr = [')(', '(()', '())', '(', ')', '((', '))', '(()())', '(())',
'()', '()(())']
for i, t in enumerate(test_expr, 1):
print '%i\t%s\t%s' % (i, t, check_parentheses(t))
# output ---------------------------------------------------------------
1 )( False
2 (() False
3 ()) False
4 ( False
5 ) False
6 (( False
7 )) False
8 (()()) True
9 (()) True
10 () True
11 ()(()) True

Balanced String Recursion Returns Improperly

I'm currently working on a problem to write a recursive program to remove all the balanced bracket operators from a string or return False if the string is not balanced. I can get the program to remove all the brackets but, according to the debugger, when the program does its final base case check to verify that the string is empty, the program jumps from return True in line 3 to isBalanced recursive call in line 10. I don't understand why this is happening. Code is the following:
def isBalanced(string):
if not string: # Base Case. If the string is empty then return True
return True
else:
j = 0
for i in string: # Iterate thru the str looking for (), {}. and [] pairs, looking for closed bracket first
if (i == ')') or (i == ']') or (i == '}'):
if (i == ')') and (string[j-1] == '('):
new_string = string[:j-1] + string[j+1:] # Remove ()
isBalanced(new_string)
elif (i == ']') and (string[j-1] == '['):
new_string = string[:j-1] + string[j+1:] # Remove []
isBalanced(new_string)
elif (i == '}') and (string[j-1] == '{'):
new_string = string[:j-1] + string[j+1:] # Remove {}
isBalanced(new_string)
else: # Did not find an open bracket to match a closed bracket operator
print('Program failed at:', string)
return False
else:
j += 1 # Index counter
test_str = "({[]()})"
print(isBalanced(test_str))

Why can't I push multiple digit number in stack to calculate prefix?

I'm writing a program where I'm converting a function to prefix and calculate.
from pythonds.basic import Stack
def doMath(op, op1, op2):
if op == "*":
return int(op1) * int(op2)
elif op == "/":
return int(op1) / int(op2)
elif op == "+":
return int(op1) + int(op2)
elif op == "-":
return int(op1) + int(op2)
def postfixEval(postfixExpr):
operandStack = Stack()
tokenList = postfixExpr.split()
for token in tokenList:
print(tokenList)
print("this is token: ", token)
if token in "0123456789":
operandStack.push(token)
print("pop: ",operandStack.peek())
elif not operandStack.isEmpty():
operand2 = operandStack.pop()
operand1 = operandStack.pop()
result = doMath(token, operand1, operand2)
print (result)
operandStack.push(result)
return operandStack.pop()
print(postfixEval('7 8 + 3 2 + /'))
print(postfixEval("17 10 + 3 * 9 /"))
So when I run the first postfixEval it return 3.0,
but at the second print it returns IndexError: pop from empty list
Apparently it's bc of the 2 digit numbers, how could I fix that?
Thanks
When you try:
if token in "0123456789":
operandStack.push(token)
for token = 17, this shall fail since 17 is not in 0123456789.
So change it to this:
try:
if float(token):
operandStack.push(token)
except:
#your code here
HOW THIS WORKS:
When a type of str containing digits and numbers are passed, float() tries to convert it into float. This is only possible only if it is a number.
Replace if token in "0123456789" (checks if token is a substring of "0123456789") with if token.isdigit() (checks if token consists of decimal digits).

Evaluate mathematical expression from String - Python

I need to evaluate an expression from a string given such as "(19 + ((91 -96)-13))" but I have to make te algorithm myself, which I can't use eval() or something similar.
I've been tried alot with this code but it gives me problems with negative numbers:
while counter < 1:
chunks = []
counter2 = 1
for character in operation:
if character.isdigit():
if chunks[-1].isdigit(): # If the last chunk is already a number
chunks[-1] += character # Add onto that number
else:
chunks.append(character) # Start a new number chunk
elif character in '+-/*()':
chunks.append(character) # This doesn't account for `1 ++ 2`.
for e in reversed(chunks):
if e == '(':
counter2 = len(chunks) - counter2
break
else:
counter2 = counter2 + 1
if chunks[counter2+2] == '+':
result2 = int (chunks[counter2+1]) + int (chunks[counter2+3])
elif chunks[counter2+2] == '-':
result2 = int (chunks[counter2+1]) - int (chunks[counter2+3])
elif chunks[counter2+2] == '*':
result2 = int (chunks[counter2+1]) * int (chunks[counter2+3])
elif chunks[counter2+2] == '/':
result2 = int (chunks[counter2+1]) / int (chunks[counter2+3])
chunks[counter2] = ''
chunks[counter2 + 1] = ''
chunks[counter2 + 2] = str (result2)
chunks[counter2 + 3] = ''
chunks[counter2 + 4] = ''
operation = ''.join(chunks)
Don't pay attention to the while condition, I'm updating it as I need just to check
You should try to debug code more on your own. The problem you have is the int conversion is being done on "-" and not "-5". You need to combine "-" and integer next in the sequence and convert that to int and use.
temp = str(chunks[counter2+3]) + str(chunks[counter2+4])

converting infix to prefix in python

I am trying to write an Infix to Prefix Converter where e.g. I would like to convert this:
1 + ((C + A ) * (B - F))
to something like:
add(1, multiply(add(C, A), subtract(B, F)))
but I get this instead :
multiply(add(1, add(C, A), subtract(B, F)))
This is the code I have so far
postfix = []
temp = []
newTemp = []
def textOperator(s):
if s is '+':
return 'add('
elif s is '-':
return 'subtract('
elif s is '*':
return 'multiply('
else:
return ""
def typeof(s):
if s is '(':
return leftparentheses
elif s is ')':
return rightparentheses
elif s is '+' or s is '-' or s is '*' or s is '%' or s is '/':
return operator
elif s is ' ':
return empty
else :
return operand
infix = "1 + ((C + A ) * (B - F))"
for i in infix :
type = typeof(i)
if type is operand:
newTemp.append(i)
elif type is operator:
postfix.append(textOperator(i))
postfix.append(newTemp.pop())
postfix.append(', ')
elif type is leftparentheses :
newTemp.append(i)
elif type is rightparentheses :
next = newTemp.pop()
while next is not '(':
postfix.append(next)
next = newTemp.pop()
postfix.append(')')
newTemp.append(''.join(postfix))
while len(postfix) > 0 :
postfix.pop()
elif type is empty:
continue
print("newTemp = ", newTemp)
print("postfix = ", postfix)
while len(newTemp) > 0 :
postfix.append(newTemp.pop())
postfix.append(')')
print(''.join(postfix))
Can someone please help me figure out how I would fix this.
What I see, with the parenthetical clauses, is a recursive problem crying out for a recursive solution. The following is a rethink of your program that might give you some ideas of how to restructure it, even if you don't buy into my recursion argument:
import sys
from enum import Enum
class Type(Enum): # This could also be done with individual classes
leftparentheses = 0
rightparentheses = 1
operator = 2
empty = 3
operand = 4
OPERATORS = { # get your data out of your code...
"+": "add",
"-": "subtract",
"*": "multiply",
"%": "modulus",
"/": "divide",
}
def textOperator(string):
if string not in OPERATORS:
sys.exit("Unknown operator: " + string)
return OPERATORS[string]
def typeof(string):
if string == '(':
return Type.leftparentheses
elif string == ')':
return Type.rightparentheses
elif string in OPERATORS:
return Type.operator
elif string == ' ':
return Type.empty
else:
return Type.operand
def process(tokens):
stack = []
while tokens:
token = tokens.pop()
category = typeof(token)
print("token = ", token, " (" + str(category) + ")")
if category == Type.operand:
stack.append(token)
elif category == Type.operator:
stack.append((textOperator(token), stack.pop(), process(tokens)))
elif category == Type.leftparentheses:
stack.append(process(tokens))
elif category == Type.rightparentheses:
return stack.pop()
elif category == Type.empty:
continue
print("stack = ", stack)
return stack.pop()
INFIX = "1 + ((C + A ) * (B - F))"
# pop/append work from right, so reverse, and require a real list
postfix = process(list(INFIX[::-1]))
print(postfix)
The result of this program is a structure like:
('add', '1', ('multiply', ('add', 'C', 'A'), ('subtract', 'B', 'F')))
Which you should be able to post process into the string form you desire (again, recursively...)
PS: type and next are Python built-ins and/or reserved words, don't use them for variable names.
PPS: replace INFIX[::-1] with sys.argv[1][::-1] and you can pass test cases into the program to see what it does with them.
PPPS: like your original, this only handles single digit numbers (or single letter variables), you'll need to provide a better tokenizer than list() to get that working right.

Categories

Resources