3
3
4
4
# stack implementation
5
5
class Stack (object ):
6
- '''
7
- size : return the current size of the stack
8
- push : push an item into the stack
9
- pop : pop the topmost item from the stack
10
- peek : return the topmost element of the stack
11
- isEmpty : check if the stack is empty
12
-
13
- '''
14
-
15
6
def __init__ (self , limit = 10 ):
16
7
'''
17
8
@param : limit: the stack size
18
9
'''
19
10
self .stack = []
20
11
self .limit = limit
21
12
22
- # for printing the stack contents
23
13
def __str__ (self ):
24
14
return ' ' .join ([str (i ) for i in self .stack ])
25
15
26
- # for pushing an element on to the stack
27
16
def push (self , data ):
17
+ ''' pushes an item into the stack '''
28
18
if len (self .stack ) >= self .limit :
29
19
return - 1 # indicates stack overflow
30
20
else :
31
21
self .stack .append (data )
32
22
33
- # for popping the uppermost element
34
23
def pop (self ):
24
+ ''' pops the topmost item from the stack '''
35
25
if len (self .stack ) <= 0 :
36
26
return - 1 # indicates stack underflow
37
27
else :
38
28
return self .stack .pop ()
39
29
40
- # for peeking the top-most element of the stack
41
30
def peek (self ):
31
+ ''' returns the topmost element of the stack '''
42
32
if len (self .stack ) <= 0 :
43
33
return - 1 # stack underflow
44
34
else :
45
35
return self .stack [len (self .stack ) - 1 ]
46
36
47
- # to check if stack is empty
48
- def isEmpty ( self ):
49
- return self .stack == []
37
+ def is_empty ( self ):
38
+ ''' checks if the stack is empty '''
39
+ return self .size () == 0
50
40
51
- # for checking the size of stack
52
41
def size (self ):
42
+ ''' returns the current size of the stack '''
53
43
return len (self .stack )
54
44
55
45
# easily retrieve the source code of the Stack class
56
46
def get_code (self ):
47
+ ''' returns the code for current class '''
57
48
import inspect
58
49
return inspect .getsource (Stack )
59
50
@@ -68,15 +59,15 @@ def __init__(self, expression = [], stack = None):
68
59
@param: expression : the infix expression to be converted to postfix
69
60
@param: stack : stack to perform infix to postfix operation
70
61
'''
71
- self .myExp = list (expression )
72
- self .myStack = stack
62
+ self .expression = list (expression )
63
+ self .my_stack = stack
73
64
74
- # function to check whether the given character of the expression is operand
75
65
def _isOperand (self , char ):
66
+ ''' utility function to find whether the given character is an operator '''
76
67
return (ord (char ) >= ord ('a' ) and ord (char ) <= ord ('z' )) or (ord (char ) >= ord ('A' ) and ord (char ) <= ord ('Z' ))
77
68
78
- # self defined precedence for each operator
79
69
def _precedence (self , char ):
70
+ ''' utility function to find precedence of the specified character '''
80
71
if char == '+' or char == '-' :
81
72
return 1
82
73
elif char == '*' or char == '/' :
@@ -86,29 +77,30 @@ def _precedence(self, char):
86
77
else :
87
78
return - 1
88
79
89
- # function to convert infix to postfix
90
80
def infix_to_postfix (self ):
91
- postFix = []
92
- for i in range (len (self .myExp )):
93
- if (self ._isOperand (self .myExp [i ])):
94
- postFix .append (self .myExp [i ])
95
- elif (self .myExp [i ] == '(' ):
96
- self .myStack .push (self .myExp [i ])
97
- elif (self .myExp [i ] == ')' ):
98
- topOperator = self .myStack .pop ()
99
- while (not self .myStack .isEmpty () and topOperator != '(' ):
100
- postFix .append (topOperator )
101
- topOperator = self .myStack .pop ()
81
+ ''' function to generate postfix expression from infix expression '''
82
+ postfix = []
83
+ for i in range (len (self .expression )):
84
+ if (self ._isOperand (self .expression [i ])):
85
+ postfix .append (self .expression [i ])
86
+ elif (self .expression [i ] == '(' ):
87
+ self .my_stack .push (self .expression [i ])
88
+ elif (self .expression [i ] == ')' ):
89
+ topOperator = self .my_stack .pop ()
90
+ while (not self .my_stack .is_empty () and topOperator != '(' ):
91
+ postfix .append (topOperator )
92
+ topOperator = self .my_stack .pop ()
102
93
else :
103
- while (not self .myStack . isEmpty ()) and (self ._precedence (self .myExp [i ]) <= self ._precedence (self .myStack .peek ())):
104
- postFix .append (self .myStack .pop ())
105
- self .myStack .push (self .myExp [i ])
94
+ while (not self .my_stack . is_empty ()) and (self ._precedence (self .expression [i ]) <= self ._precedence (self .my_stack .peek ())):
95
+ postfix .append (self .my_stack .pop ())
96
+ self .my_stack .push (self .expression [i ])
106
97
107
- while (not self .myStack . isEmpty ()):
108
- postFix .append (self .myStack .pop ())
109
- return ' ' .join (postFix )
98
+ while (not self .my_stack . is_empty ()):
99
+ postfix .append (self .my_stack .pop ())
100
+ return ' ' .join (postfix )
110
101
111
102
# easily retrieve the source code of the Stack class
112
103
def get_code (self ):
104
+ ''' returns the code of the current class '''
113
105
import inspect
114
- return inspect .getsource (InfixToPostfix )
106
+ return inspect .getsource (InfixTopostfix )
0 commit comments