💾 Archived View for gemlog.blue › users › georgf › 1614689783.gmi captured on 2024-03-21 at 18:34:36. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-12-04)
-=-=-=-=-=-=-
Sometimes you might want to rank people or things: your class, vacation destinations, food, US-presidents etc.
Simply use the sort-routine of your favorite programming language. The clue: YOU are acting as the comparison operator. It's a good idea to shuffle the list of items before starting. Otherwise your decisions might get biased by the original order of the list.
Try:
from functools import cmp_to_key from random import shuffle def better(x,y): print("Who is better? {0} (1) or {1} (2)".format(x,y)) while True: a=input() if a=="1": r=1 break elif a=="2": r=-1 break return r liste=["Nixon","Ford","Carter","Reagan","Bush","Clinton","W. Bush","Obama","Trump","Biden"] shuffle(liste) liste.sort(key=cmp_to_key(better)) print("*** Result (ascending order) ***") for n in liste: print(n)
Who is the first and last in your list?