Skip to content

Commit 130753a

Browse files
committed
Made PEP8 compatible
1 parent 324b623 commit 130753a

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

pygorithm/data_structures/stack.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,48 @@
33

44
# stack implementation
55
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-
156
def __init__(self, limit = 10):
167
'''
178
@param : limit: the stack size
189
'''
1910
self.stack = []
2011
self.limit = limit
2112

22-
# for printing the stack contents
2313
def __str__(self):
2414
return ' '.join([str(i) for i in self.stack])
2515

26-
# for pushing an element on to the stack
2716
def push(self, data):
17+
''' pushes an item into the stack '''
2818
if len(self.stack) >= self.limit:
2919
return -1 # indicates stack overflow
3020
else:
3121
self.stack.append(data)
3222

33-
# for popping the uppermost element
3423
def pop(self):
24+
''' pops the topmost item from the stack '''
3525
if len(self.stack) <= 0:
3626
return -1 # indicates stack underflow
3727
else:
3828
return self.stack.pop()
3929

40-
# for peeking the top-most element of the stack
4130
def peek(self):
31+
''' returns the topmost element of the stack '''
4232
if len(self.stack) <= 0:
4333
return -1 # stack underflow
4434
else:
4535
return self.stack[len(self.stack) - 1]
4636

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
5040

51-
# for checking the size of stack
5241
def size(self):
42+
''' returns the current size of the stack '''
5343
return len(self.stack)
5444

5545
# easily retrieve the source code of the Stack class
5646
def get_code(self):
47+
''' returns the code for current class '''
5748
import inspect
5849
return inspect.getsource(Stack)
5950

@@ -68,15 +59,15 @@ def __init__(self, expression = [], stack = None):
6859
@param: expression : the infix expression to be converted to postfix
6960
@param: stack : stack to perform infix to postfix operation
7061
'''
71-
self.myExp = list(expression)
72-
self.myStack = stack
62+
self.expression = list(expression)
63+
self.my_stack = stack
7364

74-
# function to check whether the given character of the expression is operand
7565
def _isOperand(self, char):
66+
''' utility function to find whether the given character is an operator '''
7667
return (ord(char) >= ord('a') and ord(char) <= ord('z')) or (ord(char) >= ord('A') and ord(char) <= ord('Z'))
7768

78-
# self defined precedence for each operator
7969
def _precedence(self, char):
70+
''' utility function to find precedence of the specified character '''
8071
if char == '+' or char == '-':
8172
return 1
8273
elif char == '*' or char == '/':
@@ -86,29 +77,30 @@ def _precedence(self, char):
8677
else:
8778
return -1
8879

89-
# function to convert infix to postfix
9080
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()
10293
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])
10697

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)
110101

111102
# easily retrieve the source code of the Stack class
112103
def get_code(self):
104+
''' returns the code of the current class '''
113105
import inspect
114-
return inspect.getsource(InfixToPostfix)
106+
return inspect.getsource(InfixTopostfix)

0 commit comments

Comments
 (0)