Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please contain docstrings explaining purposes use the given outline below for array_stack.py and array_stack_tests.py #array_stack.py from __future__ import annotations from typing import Any class ArrayStack:

image text in transcribed

please contain docstrings explaining purposes

use the given outline below for array_stack.py and array_stack_tests.py

#array_stack.py

from __future__ import annotations

from typing import Any

class ArrayStack: def __init__(self) -> None: self.capacity = 4 self.array: list[Any] = [None] * self.capacity self.size = 0

def empty_stack() -> ArrayStack: ...

def push(stack: ArrayStack, value: Any) -> None: ...

def pop(stack: ArrayStack) -> Any: ...

def peek(stack: ArrayStack) -> Any: ...

def is_empty(stack: ArrayStack) -> bool: ...

def size(stack: ArrayStack) -> int: ...

#array_stack_tests.py

from __future__ import annotations

import unittest

from array_stack import empty_stack, is_empty, peek, pop, push, size

class Tests(unittest.TestCase): def test_push_one_value(self) -> None: my_stack = empty_stack() push(my_stack, 10)

self.assertEqual(my_stack.capacity, 4) self.assertEqual(my_stack.array[0], 10) self.assertEqual(my_stack.size, 1)

# TODO: add more tests!

if __name__ == "__main__": unittest.main()

Notes: - To break up a string into "tokens", consider using the str.split method. - To convert a token (string) into a number (float), consider using try/except blocks along with the float function. - Your functions will support the mathematical operations of +,,,/,//, and . - That last one, , means exponentiation. It has higher precedence than the other operators (e.g., 2 43==264==128) and is right-associative (e.g., 232==2(32)==29== 512). (Note that is not how Python does exponentiation. That's OK! This is our calculator, we can do what we want!) - All the other operators are left-associative (e.g., 23//4==(23)//4==6//4==1 ). - At no point should you ever be using the Python builtin function eval. 2 Array Stack The first thing we're going to have to do is implement a stack. In class, we talked about how we could use either an array-based structure, or we could use a link-based structure. While they both do work, for consistency in grading, I'm going to have you all implement an array-based stack. In a file called array_stack.py, you will implement these functions (stubs are included in the starting file): - empty_stack This function takes no arguments and returns an empty stack. - push This function takes a stack and a value as arguments and places the value on the "top" of the stack. For this project, because we're only using the array-based approach, we should be mutating the given stack and thus our function won't return anything. - pop This function takes a stack as an argument and removes (and returns) the element at the "top" of the stack. If the stack is empty, then this operation should raise an IndexError. For this project, because we're mutating the stack directly, we only need return the value being popped. - peek This function takes a stack as an argument and returns (without removing) the value on the "top" of the stack. If the stack is empty, then this operation should raise an IndexError. - is_empty This function takes a stack as an argument and returns whether or not the stack is empty. - size This function takes a stack as an argument and returns the number of items in the stack. You should find that you can reuse a lot of your code from your ArrayList implementation, but it should be a fair bit simpler. Similar to ArrayList, you have the exact same restrictions about which list operations are not allowed. These should all be O(1) (i.e., constant time) operations. Place your test cases in a files named array_stack_tests.py

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions