Created
April 6, 2026 09:21
-
-
Save MaqSaid/c59167054569859ee2960c5854c63194 to your computer and use it in GitHub Desktop.
Solution for average race time calculation from LinkedIn Learning Python challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 get_rhines_times(): | |
| """Return a list of Jennifer Rhines' race times""" | |
| races = get_data() | |
| rhines_times = [] | |
| def get_time(line): | |
| return re.findall(r'\d{2}:\S+', line)[0] | |
| for line in races.splitlines(): | |
| if 'Jennifer Rhines' in line: | |
| rhines_times.append(get_time(line)) | |
| return rhines_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() | |
| total = datetime.timedelta() | |
| for racetime in racetimes: | |
| try: | |
| mins, secs, ms = re.split(r'[:.]', racetime) | |
| total += datetime.timedelta(minutes=int(mins), seconds=int(secs), milliseconds=int(ms)) | |
| except ValueError: | |
| mins, secs = re.split(r'[:.]', racetime) | |
| total += datetime.timedelta(minutes=int(mins), seconds=int(secs)) | |
| avg = total / len(racetimes) | |
| minutes = avg.seconds // 60 | |
| seconds = avg.seconds % 60 + avg.microseconds / 1e6 | |
| return f"{minutes:02d}:{seconds:04.1f}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| ITALICS = re.compile(r'<em>(.+?)</em>') | |
| SPACES = re.compile(r'\s+') | |
| PARAGRAPHS = re.compile(r'<p>(.+?)</p>') | |
| URLS = re.compile(r'<a href="(.+?)">(.+?)</a>') | |
| def html2markdown(html): | |
| '''Take in html text as input and return markdown''' | |
| markdown = ITALICS.sub(r'*\1*', html) | |
| markdown = SPACES.sub(r' ', markdown) | |
| markdown = PARAGRAPHS.sub(r'\1\n\n', markdown) | |
| markdown = URLS.sub(r'[\2](\1)', markdown) | |
| return markdown.strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment