I have a program that needs to be labeled. Meaning, write out what each command
ID: 3556210 • Letter: I
Question
I have a program that needs to be labeled. Meaning, write out what each command is doing. Also, label all variables.
Names = ['Turner', 'Philips', 'Stevenson', 'Jones', 'Gonzalez', 'Whitaker', 'Bruner', 'Webster', 'Foster', 'Anderson', 'Klein', 'Connors', 'Rivers', 'Wilson', 'Duncan']
Grades = [94, 82, 87, 78, 65, 90, 85, 97, 70, 100, 57, 88, 73, 92, 84]
def average(g_list):
sum = 0
n = len(g_list)
for i in range(n):
sum = sum + g_list[i]
return sum/float(n)
avg = average(Grades)
print (avg)
def median(g_list):
#lst = g_list
#even = (0 if len(lst) % 2 else 1) + 1
#half = (len(lst) - 1) / 2
#return sum(sorted(lst)[half:half + even]) / float(even)
l = sorted(g_list)
if len(l) % 2 == 0:
n = len(l)//2
return (l[n]+l[n-1])/2
else:
return l[len(l)//2]
median1 = median(Grades)
print (median1)
def sort(n_list, g_list):
length = len(n_list) - 1
sorted = False
while not sorted:
sorted = True
for i in range(length):
if n_list[i] > n_list[i+1]:
sorted = False
n_list[i], n_list[i+1] = n_list[i+1], n_list[i]
g_list[i], g_list[i+1] = g_list[i+1], g_list[i]
sort(Names, Grades)
print (Names)
print (Grades)
def binarySearch(n_list, g_list, name):
min = 0
max = len(n_list) - 1
while True:
if max < min:
return 'Student not found'
m = (min + max) // 2
if n_list[m] < name:
min = m + 1
elif n_list[m] > name:
max = m - 1
else:
return g_list[m]
g = binarySearch(Names, Grades, 'Philips')
print (g)
g = binarySearch(Names, Grades, 'BlackJack')
print (g)
Explanation / Answer
#create an array of names
Names = ['Turner', 'Philips', 'Stevenson', 'Jones', 'Gonzalez', 'Whitaker', 'Bruner', 'Webster', 'Foster', 'Anderson', 'Klein', 'Connors', 'Rivers', 'Wilson', 'Duncan']
#create an array with integer type
Grades = [94, 82, 87, 78, 65, 90, 85, 97, 70, 100, 57, 88, 73, 92, 84]
#function which computes the average for the given list of items
def average(g_list):
sum = 0
n = len(g_list) #returns length of the list
for i in range(n):
sum = sum + g_list[i] #adding each element
return sum/float(n) #computing the average
avg = average(Grades) #computing the average of the grades
print (avg) #prints the average
def median(g_list): #Function to compute the median of the given list
#lst = g_list
#even = (0 if len(lst) % 2 else 1) + 1
#half = (len(lst) - 1) / 2
#return sum(sorted(lst)[half:half + even]) / float(even)
l = sorted(g_list)
if len(l) % 2 == 0:
n = len(l)//2 #if length is even the return the mean mid and prev to the mid element
return (l[n]+l[n-1])/2
else:
return l[len(l)//2]
median1 = median(Grades) #gets the median
print (median1)
def sort(n_list, g_list): #function to sorth the given list
length = len(n_list) - 1
sorted = False
while not sorted: #using the bubble sort algorithm to sort the array maximmum element is pushed to the #last
sorted = True
for i in range(length):
if n_list[i] > n_list[i+1]:
sorted = False
n_list[i], n_list[i+1] = n_list[i+1], n_list[i]
g_list[i], g_list[i+1] = g_list[i+1], g_list[i]
sort(Names, Grades)
print (Names)
print (Grades)
def binarySearch(n_list, g_list, name): #Function to search for a given grade in a sorted list
#if the mid element is bigger then search in the left subarray or search in right sub array
min = 0
max = len(n_list) - 1
while True:
if max < min:
return 'Student not found'
m = (min + max) // 2
if n_list[m] < name:
min = m + 1
elif n_list[m] > name:
max = m - 1
else:
return g_list[m]
g = binarySearch(Names, Grades, 'Philips') #Searches for the name and outputs the grade of Philips
print (g)
g = binarySearch(Names, Grades, 'BlackJack') #Searches for the name and outputs the grade of BlackJack
print (g)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.