ARITHMETIC CALCULATOR IN PYTHON Requirements: Your implementation must use Array
ID: 3592925 • Letter: A
Question
ARITHMETIC CALCULATOR IN PYTHON
Requirements: Your implementation must use ArrayStack class and follow general guidelines of the algorithm described in slides #16-20 of Stacks lecture. Your function must be able to evaluate operations +, (both binary and unary forms), *, and / (using integer division). Your function must be able to evaluate correctly an expression with single-level parentheses. To test your code you can use several hard-coded arithmetic expressions Assumptions: One operation per set of parentheses, for simplicity. Only addition and subtraction operations could be in parentheses; multiplication and division, having highest priority (in the absence of exponent operation) do not require parentheses, nor will be placed in such. Dealing with more complex expressions (including the case of nested parentheses) will count as extra credit (up to 6 points) for this assignment. Dealing with interactive user input (i.e., "prompt the user to provide an arithmetic expression; make sure that it is parsed correctly whether or not the user types the expression with extra whitespace; use a loop to allow for multiple expressions to be evaluated") will also count as extra credit (up to 4 points) for this assignment. ArrayStack.py f
rom exceptions import Empty class ArrayStack: """LIFO Stack implementation using a Python list as underlying storage."""
def __init__(self): """Create an empty stack."""
self._data = [] # nonpublic list instance
def __len__(self): """Return the number of elements in the stack."""
return len(self._data)
def is_empty(self): """Return True if the stack is empty."""
return len(self._data) == 0
def push(self, e): """Add element e to the top of the stack."""
self._data.append(e) # new item stored at end of list
def top(self): """Return (but do not remove) the element at the top of the stack. Raise Empty exception if the stack is empty."""
if self.is_empty():
raise Empty('Stack is empty')
return self._data[-1] # the last item in the list
def pop(self): """Remove and return the element from the top of the stack (i.e., LIFO). Raise Empty exception if the stack is empty. """
if self.is_empty(): raise Empty('Stack is empty') return self._data.pop()
Explanation / Answer
CODE
If any query regarding code please get back to me
thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.