Skip to content

Instantly share code, notes, and snippets.

@scidam
Created November 8, 2021 07:06
Show Gist options
  • Select an option

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

Select an option

Save scidam/0c7fd4e751a07a2b7ffdc6fa38b74b43 to your computer and use it in GitHub Desktop.
Average race time (my solution)
# Source of data: https://www.arrs.run/
# This dataset has race times for women 10k runners from the Association of Road Racing Statisticians
import re
import datetime
def get_data():
"""Return content from the 10k_racetimes.txt file"""
with open('10k_racetimes.txt', 'rt') as file:
content = file.read()
return content
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_rhines_times():
"""Return a list of Jennifer Rhines' race times"""
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")
times = []
for rline in races[1:]:
if 'jennifer' in rline[AT_INDX:RD_INDX].lower() and 'rhines' in rline[AT_INDX:RD_INDX].lower():
times.append(rline[T_INDX:AT_INDX].strip())
return times
def get_average():
"""Return Jennifer Rhines' average race time in the format:
mm:ss:M where :
m corresponds to a minutes digit
s corresponds to a seconds digit
M corresponds to a milliseconds digit (no rounding, just the single digit)"""
racetimes = get_rhines_times()
rts = list(map(convert_time_string_to_seconds, racetimes))
avg_rts = sum(rts) / len(rts)
return f"{avg_rts // 60:2.0f}:{avg_rts % 60:2.1f}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment