-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain
More file actions
70 lines (65 loc) · 1.52 KB
/
main
File metadata and controls
70 lines (65 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# your code goes here
import re
class stack(object):
#top = -1
#stack = []
#Now the initialize function to start a new stack
def __init__(self, lst):
self.top = -1
self.stack = []
self.push(lst)
"""for item in lst:
self.stack.append(item)
self.top += 1"""
#Push Function for the stack
def push(self,item):
if type(item) == list:
for rec in item:
self.stack.append(rec)
self.top += 1
else:
self.stack.append(item)
self.top += 1
#print "Item Pushed Successfully"
#Pop Function for the stack
def pop(self):
rec = 0
rec = self.stack[self.top]
del self.stack[self.top]
self.top -= 1
return rec
#Function to print the stack
def prnt(self):
for i in range(self.top,-1,-1):
print self.stack[i]
#Function to return the top
def get_top(self):
return self.top
def length(self):
return self.top + 1
def infix_to_postfix(expr):
operand = stack([])
operator = stack([])
result = stack([])
p_fix = ''
for letter in expr:
if re.search('[a-z]',letter) is not None:
operand.push(letter)
if letter == '+' or letter == '-' or letter == '/' or letter == '*' or letter == '^':
operator.push(letter)
if letter == ')':
operand.push(operator.pop())
for i in range(0, operand.length()):
result.push(operand.pop())
for i in range(0, result.length()):
p_fix = p_fix + result.pop()
print p_fix
def main():
count = int(raw_input())
expr_list = []
for i in range(1, count+1):
expr_list.append(raw_input())
i += 1
for expr in expr_list:
infix_to_postfix(expr)
main()