Skip to content

Commit e5a7ef8

Browse files
author
IanDoarn
committed
stack.py now passes unittests. added missing parentheses in conditional in method __precedence()
Signed-off-by: IanDoarn <[email protected]>
1 parent e0e9505 commit e5a7ef8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

pygorithm/data_structures/stack.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ def __init__(self, expression=None, stack=None):
8888
self.my_stack = stack
8989

9090
@staticmethod
91-
def __is_operand(char):
91+
def _is_operand(char):
9292
"""
9393
utility function to find whether the given character is an operator
9494
"""
9595
# OLD VERSION
9696
# return ord(char) >= ord('a') and ord(char) <= ord('z') \
9797
# or ord(char) >= ord('A') and ord(char) <= ord('Z')
98-
return True if ord(char) in [ord(c) for c in list(ascii_letters)] else False
98+
return True if ord(char) in [ord(c) for c in ascii_letters] else False
9999

100100
@staticmethod
101-
def __precedence(char):
101+
def _precedence(char):
102102
"""
103103
utility function to find precedence of the specified character
104104
"""
@@ -117,7 +117,7 @@ def infix_to_postfix(self):
117117
"""
118118
postfix = []
119119
for i in range(len(self.expression)):
120-
if self.__is_operand(self.expression[i]):
120+
if self._is_operand(self.expression[i]):
121121
postfix.append(self.expression[i])
122122
elif self.expression[i] == '(':
123123
self.my_stack.push(self.expression[i])
@@ -127,7 +127,8 @@ def infix_to_postfix(self):
127127
postfix.append(top_operator)
128128
top_operator = self.my_stack.pop()
129129
else:
130-
while not self.my_stack.is_empty() and self.__precedence(self.expression[i] <= self.__precedence(self.my_stack.peek())):
130+
while not self.my_stack.is_empty() and self._precedence(self.expression[i]) <= self._precedence(
131+
self.my_stack.peek()):
131132
postfix.append(self.my_stack.pop())
132133
self.my_stack.push(self.expression[i])
133134

0 commit comments

Comments
 (0)