Skip to content

Instantly share code, notes, and snippets.

@rahul8590
Created December 30, 2014 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahul8590/c12c045b6024b170587f to your computer and use it in GitHub Desktop.
Save rahul8590/c12c045b6024b170587f to your computer and use it in GitHub Desktop.
CodeEval Challenge 7 (prefex solving )
#!/bin/python
from __future__ import division
import operator
class Slack:
def __init__(self):
self.items = []
def isempty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def peek(self):
print self.items
d = {'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'%': operator.mod
}
if __name__ == '__main__':
op = Slack()
val = Slack()
s = '- + * 2 3 * 5 4 9'
s = s.strip(' \t\r\n')
l = [i for i in s.split(' ')]
l.reverse()
print l
sum = 0
for i in l:
if not d.has_key(i):
val.push(int(i))
print "pushed i =>", i
else:
op.push(str(i))
if val.size() >= 2:
try:
opt = str(op.pop())
print "opt is =>", opt
op1 = val.pop()
op2 = val.pop()
sum = d[opt](op1, op2)
except ZeroDivisionError:
sum = 0
val.push(sum)
#print "sum is =>", sum
# print "current op is =>",op.peek()
# print "current val is =>",val.peek()
print "sum =>", sum
@rahul8590
Copy link
Author

The above code is not passing all the test cases. Need to fix something .. not sure what is it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment