CS261 Data Structures Assignment 1 v 1.12 (revised 6/20/2021) Python Fundamentals Review D T A C U R T S A S E R U T _ CS261 Data Structures Assignment 1: Python Fundamentals Review Page 2 of 23...

1 answer below »
Write python functions that answer these 14 questions using the skeleton code provided. Functions must pass usage tests with the code at the bottom.


CS261 Data Structures Assignment 1 v 1.12 (revised 6/20/2021) Python Fundamentals Review D T A C U R T S A S E R U T _ CS261 Data Structures Assignment 1: Python Fundamentals Review Page 2 of 23 Contents Assignment Summary........................................................... 3 General Instruction .............................................................. 4 Specific Instructions............................................................. 5 Problem 1 - Min Max ............................................................ 6 Problem 2 - Fizz Buzz ........................................................... 7 Problem 3 - Reverse ............................................................ 8 Problem 4 - Rotate .............................................................. 9 Problem 5 - Range ……………………………….................................... 11 Problem 6 - Is Sorted? ……………………………….............................. 12 Problem 7 - Sort ………………………………....................................... 13 Problem 8 - Remove Duplicates ……………………........................... 14 Problem 9 - Count Sort……………………....................................... 15 Problem 10 - Array Intersection……………………............................ 17 Problem 11 - Sorted Squares…………………................................. 18 Problem 12 - Add Numbers………………..….................................. 20 Problem 13 - Spiral Matrix…………….......................................... 21 Problem 14 - Transform String…………….……………........................ 23 CS261 Data Structures Assignment 1: Python Fundamentals Review Page 3 of 23 Summary For this assignment, you will write a few short Python functions. There are several main objectives: ● Ensure that you are familiar with basic Python syntax constructs. ● Your programming environment is set up correctly. ● You are familiar with submitting assignments through Gradescope and troubleshooting your solution based on Gradescope output. ● You know how to import and use classes that have been pre-written for you. For the CS261 class, we assume you are comfortable with: ● Iterating over a list of elements using for and while loops ● Accessing elements in the list using their indices ● Passing functions as parameters to other functions ● Using classes written for you (import into your code, create objects) ● Writing your own classes (including extending existing classes) ● Writing unit tests for your code ● Debugging your solutions None of the programs in this assignment or in CS261 in general will require Python knowledge beyond what was covered in CS161 and CS162. If you completed CS161 / CS162 classes in Python, you should be able to complete this assignment. In case you need help, please post questions on Ed Discussions / Teams or feel free to contact the instructors or the ULAs. CS261 Data Structures Assignment 1: Python Fundamentals Review Page 4 of 23 General Instructions 1. Programs in this assignment must be written in Python v3 and submitted to Gradescope before the due date specified in the syllabus. You may resubmit your code as many times as necessary. Gradescope allows you to choose which submission will be graded. 2. In Gradescope, your code will run through several tests. Any failed tests will provide a brief explanation of testing conditions to help you with troubleshooting. Your goal is to pass all tests. 3. We encourage you to create your own test programs and cases even though this work won’t have to be submitted and won’t be graded. Gradescope tests are limited in scope and may not cover all edge cases. Your submission must work on all valid inputs. We reserve the right to test your submission with more tests than Gradescope. 4. Your code must have an appropriate level of comments. At a minimum, each method should have a descriptive docstring. Additionally, put comments throughout the code to make it easy to follow and understand. 5. You will be provided with a starter “skeleton” code, on which you will build your implementation. Methods defined in skeleton code must retain their names and input / output parameters. Variables defined in skeleton code must also retain their names. We will only test your solution by making calls to methods defined in the skeleton code and by checking values of variables defined in the skeleton code. You can add more helper methods and variables, as needed. You also are allowed to add optional default parameters to method definitions. However, certain classes and methods can not be changed in any way. Please see comments in the skeleton code for guidance. In particular, content of any methods pre-written for you as part of the skeleton code must not be changed. 6. Both the skeleton code and code examples provided in this document are part of assignment requirements. Please read all of them very carefully. They have been carefully selected to demonstrate requirements for each method. Refer to them for the detailed description of expected method behavior, input / output parameters, and handling of edge cases. 7. For each method, you can choose to implement a recursive or iterative solution. When using a recursive solution, be aware of maximum recursion depths on large inputs. We will specify the maximum input size that your solution must handle. 8. Unless indicated otherwise, we will test your implementation with different types of objects, not just integers. We guarantee that all such objects will have correct implementation of methods __eq__, __lt__, __gt__, __ge__, __le__ and __str__. CS261 Data Structures Assignment 1: Python Fundamentals Review Page 5 of 23 Specific Instructions There are 14 separate problems in this assignment. For each problem you will have to write a Python function according to the provided specifications. A “skeleton” code and some basic test cases for each problem are provided in the file assignment1.py Most problems will take as input and sometimes return as output an object of the StaticArray class. StaticArray class has been pre-written for you and is located in the file static_array.py It is a very simple class that simulates the behavior of a fixed size array. It has only four methods, that allow you to: 1) Create a new static array that will store a fixed number of elements. Once the StaticArray is created, its size can not be changed. 2) Change the value of any element using its index 3) Read the value of any element using its index 4) Query the size of the array. Below is a small example of how to do the four operations described above: #create new StaticArray object to store 5 elements arr = StaticArray(5) # set the value of each element equal to its index multiplied by 10
Answered 1 days AfterJul 06, 2021

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
151 Votes
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):
"""
Cre
ate 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
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here