Note that in my example, both approaches are merged so they draw upon the exact same data in the loop. In real code, we would choose one approach or the other.
When it can be proven that both methods give the exact same results, every time, then preference may be a determining factor, but other factors come into play, as well.
1. readability
2. simplicity
3. efficiency (not a concern for a beginner)
There are an hundred ways to cook an egg. A Red Seal Chef knows them all, but then it is the circumstances and desired outcome that dictate which will be used in any particular instance. No egg cooking class starts with a soufflé. Boiling, poaching, frying, abd scrambling usually come first, followed by omelettes, then reaching the challenges.
Update
Here is the test code as a stand alone. We still need both, for the comparisons that are made at the end.
Dictionary
# This is a Python Dictionary that contains all of the students in Kenny's class as well as their grades.
student_grades = {"Jeremy" : 87, "Kyla" : 82, "Ayesha" : 90, "Aleida" : 94, "Todd" : 79, "Maxwell" : 98, "Yolonda" : 81, "Kiyoko" : 71, "Dagmar" : 73, "Laura" : 91, "Shimeah" : 81, "Songqiao" : 92, "Frankie" : 87, "Natalia" : 95, "Gonzalo" : 82, "Pavel" : 78}
Kenny's Algorithm
# This is a function that determines the student with the highest grade given a dictionary
def highest_grade(grades):
top_of_class = list(grades.keys())[0]
for student_a in grades:
for student_b in grades:
if (grades[student_a] > grades[student_b]) and (grades[student_a] > grades[top_of_class]):
top_of_class = student_a
elif (grades[student_b] > grades[student_a]) and (grades[student_b] > grades[top_of_class]):
top_of_class = student_b
return top_of_class
# This is a function that determines the student with the lowest grade given a dictionary
def lowest_grade(grades):
bottom_of_class = list(grades.keys())[0]
for student_a in grades:
for student_b in grades:
if (grades[student_a] < grades[student_b]) and (grades[student_a] < grades[bottom_of_class]):
bottom_of_class = student_a
elif (grades[student_b] < grades[student_a]) and (grades[student_b] < grades[bottom_of_class]):
bottom_of_class = student_b
return bottom_of_class
# This is Kenny's Algorithm! It sorts the students into pairs based on their grades.
def kennys_algorithm(grade_dict):
student_pairs = []
while len(grade_dict) > 0:
student_pairs.append([highest_grade(grade_dict), lowest_grade(grade_dict)])
grade_dict.pop(highest_grade(grade_dict))
grade_dict.pop(lowest_grade(grade_dict))
print(student_pairs)
return student_pairs
Test Algorithm
# test functions
def top_grade(grades):
top_of_class = list(grades.keys())[0]
for student_a in grades:
if (grades[student_a] > grades[top_of_class]):
top_of_class = student_a
return top_of_class
def bottom_grade(grades):
bottom_of_class = list(grades.keys())[0]
for student_a in grades:
if (grades[student_a] < grades[bottom_of_class]):
bottom_of_class = student_a
return bottom_of_class
def test_algorithm(grade_dict):
test_pairs = []
while len(grade_dict) > 0:
test_pairs.append([top_grade(grade_dict), bottom_grade(grade_dict)])
grade_dict.pop(top_grade(grade_dict))
grade_dict.pop(bottom_grade(grade_dict))
print(test_pairs)
return test_pairs
Run both and compare the outcomes…
student_pairs = kennys_algorithm(student_grades.copy())
test_pairs = test_algorithm(student_grades.copy())
print ([x for x in student_pairs if x not in test_pairs])
print ([x for x in test_pairs if x not in student_pairs])