Skip to content

Instantly share code, notes, and snippets.

@scidam
Created November 8, 2021 15:41
Show Gist options
  • Select an option

  • Save scidam/e646a07cbda7034acdc8c0f5bd02b998 to your computer and use it in GitHub Desktop.

Select an option

Save scidam/e646a07cbda7034acdc8c0f5bd02b998 to your computer and use it in GitHub Desktop.
My solution to Medal challenge
from collections import namedtuple
with open('olympics.txt', 'rt', encoding='utf-8') as file:
olympics = file.read()
medal = namedtuple('medal', ['City', 'Edition', 'Sport', 'Discipline', 'Athlete', 'NOC', 'Gender',
'Event', 'Event_gender', 'Medal'])
medals = [medal(*line.split(';')) for line in olympics.split('\n')[1:] if line]
def get_medals(**kwargs):
'''Return a list of medal namedtuples '''
result = []
for medal in medals:
if all(getattr(medal, att, False) == val for att, val in kwargs.items()):
result.append(medal)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment