Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save scidam/29a69f390c0d865574317aacafade8a5 to your computer and use it in GitHub Desktop.
Get slowest race
# Source of data: https://www.arrs.run/
# This dataset has race times for women 10k runners from the Association of Road Racing Statisticians
# Assume a year has 365.25 days
from datetime import datetime
def prefil_globals():
global T_INDX, AT_INDX, RD_INDX, DB_INDX, LOC_INDX
with open('10k_racetimes.txt', 'rt') as file:
content = file.read()
races = get_data().split('\n')
header = races[0]
T_INDX = header.index("TIME")
AT_INDX = header.index("Athlete")
RD_INDX = header.index("Race date")
DB_INDX = header.index("Date of birth Location")
LOC_INDX = header.index("Location")
def get_data():
with open('10k_racetimes.txt', 'rt') as file:
content = file.read()
return content
prefil_globals()
def get_event_time(line):
"""Given a line with Jennifer Rhines' race times from 10k_racetimes.txt,
parse it and return a tuple of (age at event, race time).
Assume a year has 365.25 days"""
race_date = line[RD_INDX:DB_INDX].strip()
birth_date = line[DB_INDX:LOC_INDX].strip()
race_time = line[T_INDX:AT_INDX].strip()
if race_date and birth_date:
rd = datetime.strptime(race_date, '%d %b %Y')
bd = datetime.strptime(birth_date, '%d %b %Y')
return rd - bd, race_time
def convert_time_string_to_seconds(ts):
a, b = ts.split(':')
try:
a = float(a)
b = float(b)
except ValueError:
print(f"Illegal value: {a}, {b}; {ts}")
return a * 60 + b
def get_age_slowest_times():
'''Return a tuple (age, race_time) where:
age: AyBd is in this format where A and B are integers'''
races = [
get_event_time(line) for line in get_data().split('\n')[1:]
if 'Jennifer Rhines' in line
]
races = filter(lambda x: x is not None, races)
item = max(races, key=lambda x: convert_time_string_to_seconds(x[1]))
res = divmod(item[0].days, 365.25)
return f'{int(res[0])}y{int(res[1])}d', item[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment