Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help fix my code i cant get it to run correctly. ill lleave the code and error below # - - - - -

please help fix my code i cant get it to run correctly. ill lleave the code and error below # ------------------- 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
"""
arr = StaticArray(end - start +1)
for i, j in enumerate(range(start, end +1)):
arr.set(i, j)
return arr
# ------------------- 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
"""
ascending = True
descending = True
for i in range(1, arr.length()):
if arr.get(i) arr.get(i -1):
ascending = False
if arr.get(i)> arr.get(i -1):
descending = False
if not ascending and not descending:
return 0
return 1 if ascending else -1
# ------------------ 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
"""
unique = StaticArray(1)
unique.set(0, arr.get(0))
for i in range(1, arr.length()):
if arr.get(i)!= arr.get(i-1):
unique.set(unique.length(), arr.get(i))
return unique
# ------------------- PROBLEM 9- COUNT_SORT --------------------------------
def count_sort(arr: StaticArray)-> StaticArray:
"""
Function receives a StaticArray and returns a new StaticArray with same content
sorted in non-ascending order using count sort algorithm
"""
max_value = arr.get(0)
for i in range(1, arr.length()):
if arr.get(i)> max_value:
max_value = arr.get(i)
count =[0]*(max_value +1)
for i in range(arr.length()):
count[arr.get(i)]+=1
for i in range(1, max_value +1):
count[i]+= count[i-1]
sorted_array = StaticArray(arr.length())
for i in range(arr.length()-1,-1,-1):
sorted_array.set(count[arr.get(i)]-1, arr.get(i))
count[arr.get(i)]-=1
return sorted_array
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions