HW3
.py
keyboard_arrow_up
School
California State University, Fullerton *
*We aren’t endorsed by this school
Course
220
Subject
Industrial Engineering
Date
Apr 3, 2024
Type
py
Pages
8
Uploaded by MasterDiscovery7392 on coursehero.com
# HW3
# REMINDER: The work in this assignment must be your own original work and must be completed alone.
class Node:
def __init__(self, value):
self.value = value self.next = None def __str__(self):
return f"Node({self.value})"
__repr__ = __str__
#=============================================== Part I ==============================================
class Stack:
'''
>>> x=Stack()
>>> x.pop()
>>> x.push(2)
>>> x.push(4)
>>> x.push(6)
>>> x
Top:Node(6)
Stack:
6
4
2
>>> x.pop()
6
>>> x
Top:Node(4)
Stack:
4
2
>>> len(x)
2
>>> x.peek()
4
'''
def __init__(self):
self.top = None
def __str__(self):
temp = self.top
out = []
while temp:
out.append(str(temp.value))
temp = temp.next
stack_str = '\n'.join(out)
return f"Top:{self.top}\nStack:\n{stack_str}"
def __repr__(self):
return str(self)
def isEmpty(self):
return self.top is None
def __len__(self):
temp = self.top
count = 0
while temp:
count += 1
temp = temp.next
return count
def push(self, value):
new_node = Node(value)
new_node.next = self.top
self.top = new_node
def pop(self):
if self.isEmpty():
return None
popped_value = self.top.value
self.top = self.top.next
return popped_value
def peek(self):
if self.isEmpty():
return None
return self.top.value
#=============================================== Part II ==============================================
class Calculator:
def __init__(self):
self.__expr = None
def setExpr(self, new_expr):
self.__expr = new_expr
def getExpr(self):
return self.__expr
def _isNumber(self, txt):
'''
>>> x=Calculator()
>>> x._isNumber(' 2.560 ')
True
>>> x._isNumber('7 56')
False
>>> x._isNumber('2.56p')
False
'''
try:
float(txt)
return True
except ValueError:
return False
def getPriority(self, op):
if op in ['+', '-']:
return 1
elif op in ['*', '/']:
return 2
elif op == '^':
return 3
else:
return 0
def _getPostfix(self, txt):
'''
Required: _getPostfix must create and use a Stack object for expression
processing
>>> x=Calculator()
>>> x._getPostfix('2 ^ 4')
'2.0 4.0 ^'
>>> x._getPostfix('2')
'2.0'
>>> x._getPostfix('2.1 * 5 + 3 ^ 2 + 1 + 4.45')
'2.1 5.0 * 3.0 2.0 ^ + 1.0 + 4.45 +'
>>> x._getPostfix('2 * 5.34 + 3 ^ 2 + 1 + 4')
'2.0 5.34 * 3.0 2.0 ^ + 1.0 + 4.0 +'
>>> x._getPostfix('2.1 * 5 + 3 ^ 2 + 1 + 4')
'2.1 5.0 * 3.0 2.0 ^ + 1.0 + 4.0 +'
>>> x._getPostfix('( 2.5 )')
'2.5'
>>> x._getPostfix('( 2 { 5.0 } )')
'2.0 5.0 *'
>>> x._getPostfix(' 5 ( 2 + { 5 + 3.5 } )')
'5.0 2.0 5.0 3.5 + + *'
>>> x._getPostfix ('( { 2 } )')
'2.0'
>>> x._getPostfix ('2 * ( [ 5 + -3 ] ^ 2 + { 1 + 4 } )')
'2.0 5.0 -3.0 + 2.0 ^ 1.0 4.0 + + *'
>>> x._getPostfix ('[ 2 * ( ( 5 + 3 ) ^ 2 + ( 1 + 4 ) ) ]')
'2.0 5.0 3.0 + 2.0 ^ 1.0 4.0 + + *'
>>> x._getPostfix ('( { 2 * { { 5 + 3 } ^ 2 + ( 1 + 4 ) } } )')
'2.0 5.0 3.0 + 2.0 ^ 1.0 4.0 + + *'
>>> x._getPostfix('2 * ( -5 + 3 ) ^ 2 + [ 1 + 4 ]')
'2.0 -5.0 3.0 + 2.0 ^ * 1.0 4.0 + +'
# In invalid expressions, you might print an error message, but code must return None, adjust doctest accordingly
# If you are veryfing the expression in calculate before passing to postfix, this cases are not necessary
>>> x._getPostfix('2 * 5 + 3 ^ + -2 + 1 + 4')
>>> x._getPostfix('2 * 5 + 3 ^ - 2 + 1 + 4')
>>> x._getPostfix('2 5')
>>> x._getPostfix('25 +')
>>> x._getPostfix(' 2 * ( 5 + 3 ) ^ 2 + ( 1 + 4 ')
>>> x._getPostfix(' 2 * ( 5 + 3 ) ^ 2 + ( 1 + 4 ]')
>>> x._getPostfix(' ( 2 * { 5 + 3 ) ^ 2 + ( 1 + 4 ] }')
>>> x._getPostfix(' 2 * ( 5 + 3 ) ^ 2 + ) 1 + 4 (')
>>> x._getPostfix('2 * 5% + 3 ^ + -2 + 1 + 4')
'''
# YOUR CODE STARTS HERE
precedence = {'(': 0, ')': 0, '[': 0, ']': 0, '{': 0, '}': 0, '<': 0, '>':
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help