Answer To: CS261 Data Structures Assignment 1 v 1.12 (revised 6/20/2021) Python Fundamentals Review D T A C U R...
Shashank answered on Jul 07 2021
static_array.py# Course: CS261 - Data Structures
# Description: 'Helper' data structures for assignments 1, 2, 3
class StaticArrayException(Exception):
"""
Custom exception for Static Array class
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
pass
class StaticArray:
"""
Class that implements Static Array Data Structure
Implemented methods: get, set, size
DO NOT CHANGE THIS CLASS IN ANY WAY
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
"""
def __init__(self, size=10):
"""
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')
self._size = size
self._data = [None] * size
def __iter__(self):
"""
Disable iterator capability for StaticArray class
This means loops and aggregate functions like
those shown below won't work:
arr = StaticArray()
for value in arr: # will not work
min(arr) # will not work
max(arr) # will not work
sort(arr) # will not work
"""
return None
def __str__(self):
"""
Return content of static array in human-readable form
"""
out = "STAT_ARR Size: "
out += str(self._size)
out += " " + str(self._data)
return out
def get(self, index: int):
"""
Return value from given index position
Invalid index raises StaticArrayException
"""
if index < 0 or index >= self.size():
raise StaticArrayException('Index out of bounds')
return self._data[index]
def set(self, index: int, value: object) -> None:
"""
Store value at given index in the array
Invalid index raises StaticArrayException
"""
if index < 0 or index >= self.size():
raise StaticArrayException('Index out of bounds')
self._data[index] = value
def __getitem__(self, index):
"""
Same functionality as get() method above, but called differently
These snippets of code are equivalent:
arr = StaticArray()
arr.set(0, 'hello')
print(arr.get(0))
arr = StaticArray()
arr[0] = 'hello'
print(arr[0])
"""
return self.get(index)
def __setitem__(self, index, value) -> None:
"""
Same functionality as set() method above, but called differently
These snippets of code are equivalent:
arr = StaticArray()
arr.set(0, 'hello')
print(arr.get(0))
arr = StaticArray()
arr[0] = 'hello'
print(arr[0])
"""
self.set(index, value)
def size(self) -> int:
""" Return size of the array (number of elements) """
return self._size
assignment.py# Course: CS261 - Data Structures
# Student Name:
# Assignment:
# Description:
import random
import string
from static_array import *
# ------------------- PROBLEM 1 - MIN_MAX -----------------------------------
def min_max(arr: StaticArray):
"""
TODO: Write this implementation
"""
# print(StaticArray)
min_val = arr[0]
max_val = arr[0]
n=0
while n if arr[n]>max_val:
max_val = arr[n]
if arr[n] min_val = arr[n]
n+=1
return (min_val,max_val)
# ------------------- PROBLEM 2 - FIZZ_BUZZ ---------------------------------
def fizz_buzz(arr: StaticArray) -> StaticArray:
"""
TODO: Write this implementation
"""
newlist=[]
n=0
while n if arr[n]%3 == 0 and arr[n]%5 == 0:
newlist.append('fizzbuzz')
elif arr[n]%3 == 0:
newlist.append('fizz')
elif arr[n]%5 == 0:
newlist.append('buzz')
else:
newlist.append(arr[n])
n+=1
return 'STAT_ARR Size: {} {}'.format(StaticArray.size(arr),newlist)
# ------------------- PROBLEM 3 - REVERSE -----------------------------------
def reverse(arr: StaticArray) -> None:
"""
TODO: Write this implementation
"""
new_list = []
n=StaticArray.size(arr)
while n>0 :
new_list.append(arr[n-1])
n-=1
for i, value in enumerate(new_list):
arr.set(i, value)
return arr
# ------------------- PROBLEM 4 - ROTATE ------------------------------------
def rotate(arr: StaticArray, steps: int) -> StaticArray:
"""
TODO: Write this implementation
"""
output_list = []
n=0
steps = steps % StaticArray.size(arr)
if steps !=0:
if steps > 0:
for item in range(StaticArray.size(arr) - steps, StaticArray.size(arr)):
output_list.append(arr[item])
for item in range(0,StaticArray.size(arr) - steps):
output_list.append(arr[item])
else:
for item in range(abs(steps), StaticArray.size(arr)):
output_list.append(arr[item])
for item in range(0,abs(steps)):
output_list.append(arr[item])
return 'STAT_ARR Size: {} {}'.format(StaticArray.size(arr) ,output_list)
else:
return arr
# ------------------- PROBLEM 5 - SA_RANGE ----------------------------------
def sa_range(start: int, end: int) -> StaticArray:
"""
TODO: Write this implementation
"""
n=0
...