Skip to content

Instantly share code, notes, and snippets.

@NamrataSitlani
Created July 19, 2020 19:56
Show Gist options
  • Save NamrataSitlani/be79b5bfdc137b0d065249182b593fbb to your computer and use it in GitHub Desktop.
Save NamrataSitlani/be79b5bfdc137b0d065249182b593fbb to your computer and use it in GitHub Desktop.
Given an unsorted array arr of integers and an index n, return a list of medians of the subarrays in arr (where the first subarray is from index 0 to index 1, the next from 0 to index 2… index 0 to index n)
def median(list1):
length = len(list1)
list1.sort()
if length %2 == 0:
median1 = list1[length//2]
median2 = list1[length//2 - 1]
median = (median1 + median2)/2
else:
median = list1[length//2]
return(median)
print(median)
def findmedian(list1, index):
sub = []
for j in range(1, index+1):
y = list1[0:j+1]
print("The subarrays are", y)
sub.append(median(y))
return sub
x = [2, 1, 3, 10, 5]
print(x)
print(findmedian(x, 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment