davron.sh.karimov
BAN USERHi Folks
Please, let me know if something was missed
import stack
def postfixEval(expression):
operands = stack.Stack()
for exp in expression:
if exp in '0123456789':
operands.push(exp)
else:
operator = exp
operand2 = int(operands.pop())
operand1 = int(operands.pop())
result = doMath(operator, operand1, operand2)
operands.push(result)
return operands.pop()
def doMath(operator, operand1, operand2):
operators = '*/+-'
if operators.index(operator) == 0:
return operand1 * operand2
elif operators.index(operator) == 1:
return operand1 // operand2
elif operators.index(operator) == 2:
return operand1 + operand2
elif operators.index(operator) == 3:
return operand1 - operand2
def check(text, sample):
pos = 0
for i in range(len(text)):
if text[i] == sample[pos]:
pos += 1
if pos == len(sample):
return True
return False
python
time O(m)
space O(1)
- davron.sh.karimov February 01, 2016