[9, 8, 7, 8]
We’ve worked with lists of lists, but here’s a review:
for
loops with lists of listsflatten
numbers
.append()
get_highest_average
lists
, which is a 2D list of numbers (floats or integers)def get_highest_average(numbers):
means = []
for i in range(len(numbers)):
if len(numbers[i]) > 0:
total_sublist = 0
for j in range(len(numbers[i])):
total_sublist += numbers[i][j]
mean_sublist = total_sublist / len(numbers[i])
means.append(mean_sublist)
if len(means) > 0:
highest_mean = means[0]
for i in range(len(means)):
if means[i] > highest_mean:
highest_mean = means[i]
return highest_mean
def main():
assert get_highest_average([[1, 1, 1], [2, 2]]) == 2
assert get_highest_average([[1, 1, 1], []]) == 1
assert get_highest_average([[], []]) == None
assert get_highest_average([]) == None
weeks = [ [100, 100, 105, 105, 102, 102, 100],
[70, 71, 75, 72, 81, 80, 74],
[78, 72, 74, 71, 70, 70, 70],
[30, 32, 40, 38, 31, 32, 30] ]
average = get_highest_average(weeks)
assert average == 102
main()
Name your solution two_d_highest_average.py
and submit it to gradescope