I need to write a function sort_gradebook(gradebook)
, which has next arguments: [first_name, last_name, grade_1, grade_2, ..., grade_n, final_grade]
. Function must sort by:
- Final grade
- If final grades are equal - by first grade
- If first grades are equal - by second grade, etc
- If all grades are equal - by second name
- If second names are equal - by name.
Everything I could do:
from operator import itemgetter
def sort_gradebook(*gradebook):
length = len([str(i) for i in gradebook[0]])
a = [i for i in range(length)]
for i in a:
s = sorted(gradebook, key = itemgetter(i))
return s
For test:
from itertools import permutations
def test_sort(inp, outp):
for i in permutations(inp):
assert sort_gradebook(list(i)) == outp
test_sort([['Alice', 'Smith', 2, 3, 4],
['John', 'Smith', 2, 3, 5]], [['John', 'Smith', 2, 3, 5],
['Alice', 'Smith', 2, 3, 4]
])