Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help with the following Python functions: ------------------------------------------------------------------------------------------------- import random from static_array import * # ------------------- PROBLEM 1 - MIN_MAX ----------------------------------- def min_max(arr: StaticArray) ->
Please help with the following Python functions:
-------------------------------------------------------------------------------------------------
import random from static_array import * # ------------------- PROBLEM 1 - MIN_MAX ----------------------------------- def min_max(arr: StaticArray) -> (int, int): """ Write function receives a one-dimensional array of integers and returns a Python tuple with two values being min and max of the array """ pass # ------------------- PROBLEM 2 - FIZZ_BUZZ --------------------------------- def fizz_buzz(arr: StaticArray) -> StaticArray: """ Function that receives a StaticArray of integers, returns a new StaticArray object and modifies it by If number is divisable by 3 element in new array is fizz If number is divisable by 5 element in new array is buzz If number is a multiple and 3 and 5, element in new array is fizzbuzz All other cases element in new array has same value as original array """ pass # ------------------- PROBLEM 3 - REVERSE ----------------------------------- def reverse(arr: StaticArray) -> None: """ Function receives StaticArray reverses order of elements in the array """ pass # ------------------- PROBLEM 4 - ROTATE ------------------------------------ def rotate(arr: StaticArray, steps: int) -> StaticArray: """ Function that receives two parameters being StaticArray and integer called steps Creates new array containing original elements shifted left or right by steps If steps positive rotate right, left if negative """ pass # ------------------- PROBLEM 5 - SA_RANGE ---------------------------------- def sa_range(start: int, end: int) -> StaticArray: """ Function that receives the two integers start and end and returns StaticArray containing all consecutive integers start and end 0(N) complexity """ pass # ------------------- PROBLEM 6 - IS_SORTED --------------------------------- def is_sorted(arr: StaticArray) -> int: """ Function receives StaticArray and returns integer returns array that describes if array is sorted: 1 if ascending, -1 descending and 0 otherwise """ pass # ------------------- PROBLEM 7 - FIND_MODE ----------------------------------- def find_mode(arr: StaticArray) -> (int, int): """ Receives StaticArray sorted in order either non-descending or non-ascending Returns mode of array and its frequency in that order """ pass # ------------------- PROBLEM 8 - REMOVE_DUPLICATES ------------------------- def remove_duplicates(arr: StaticArray) -> StaticArray: """ Function receives a StaticArray already sorted, non-descending or non-ascending Returns a new static array with all duplicate values removed """ pass # ------------------- PROBLEM 9 - COUNT_SORT -------------------------------- def count_sort(arr: StaticArray) -> StaticArray: """ Function recieves a StaticArray and returns a new StaticArray with same content sorted in non-ascending order using count sort algorithm """ pass # ------------------- PROBLEM 10 - SORTED SQUARES --------------------------- def sorted_squares(arr: StaticArray) -> StaticArray: """ Receives a StaticArray where elements sorted in order and returns a new StaticArray with squares of values from original array sorted in non-descending order, original array must not be modified""" pass
----------------------------------------------------------------------------------------------------------------------------------------------
#static_array.py
class StaticArrayException(Exception): """ Custom exception for Static Array class. Any changes to this class are forbidden. """ pass class StaticArray: """ Implementation of Static Array Data Structure. Implemented methods: get(), set(), length() """ def __init__(self, size: int = 10) -> None: """ Create array of given size. Initialize all elements with values of None. If requested size is not a positive number, raise StaticArray Exception. """ if size < 1: raise StaticArrayException('Array size must be a positive integer') # The underscore denotes this as a private variable and # private variables should not be accessed directly. # Use the length() method to get the size of a StaticArray. self._size = size self._data = [None] * size def __iter__(self) -> None: """ Disable iterator capability for StaticArray class. """ return None def __str__(self) -> str: """Override string method to provide more readable output.""" return f"STAT_ARR Size: {self._size} {self._data}" def get(self, index: int): """ Return value from given index position. Invalid index raises StaticArrayException. """ if index < 0 or index >= self.length(): raise StaticArrayException('Index out of bounds') return self._data[index] def set(self, index: int, value) -> None: """ Store value at given index in the array. Invalid index raises StaticArrayException. """ if index < 0 or index >= self.length(): raise StaticArrayException('Index out of bounds') self._data[index] = value def __getitem__(self, index: int): """Enable bracketed indexing.""" return self.get(index) def __setitem__(self, index: int, value: object) -> None: """Enable bracketed indexing.""" self.set(index, value) def length(self) -> int: """Return length of the array (number of elements).""" return self._size if __name__ == "__main__": # Create a new StaticArray object - is the default size arr = StaticArray() # These two statements are equivalent arr.set(0, 'hello') arr[0] = 'hello' # These two statements are equivalent print(arr.get(0)) print(arr[0]) # Print the number of elements stored in the array print(arr.length()) # Create a new StaticArray object to store 5 elements arr = StaticArray(5) # Set the value of each element equal to its index multiplied by 10 for index in range(arr.length()): arr[index] = index * 10 # Print the values of all elements in reverse order for index in range(arr.length() - 1, -1, -1): print(arr[index]) forbidden_list = [None] * 10 print(type(arr)) print(type(forbidden_list))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started