Skip to content

Instantly share code, notes, and snippets.

@mojalil
Created July 5, 2023 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojalil/d513191abe1e4920819dc480b506f5d9 to your computer and use it in GitHub Desktop.
Save mojalil/d513191abe1e4920819dc480b506f5d9 to your computer and use it in GitHub Desktop.
nounsrc split times converter
# -*- coding: utf-8 -*-
"""NounsRC Time Trail Split Calculator.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1MGXChaKd65X9nnhwAoZ8N8U62oMCU4M1
"""
from datetime import datetime, timedelta
def convert_to_timedelta(split_time):
split_time = datetime.strptime(split_time, "%M:%S.%f")
return timedelta(minutes=split_time.minute, seconds=split_time.second, microseconds=split_time.microsecond)
def calculate_cumulative_split_times(split_times):
cumulative_times = []
cumulative_time = timedelta()
for split_time in split_times:
cumulative_time += split_time
cumulative_times.append(cumulative_time)
return cumulative_times
def print_cumulative_split_times(names, cumulative_times):
sorted_data = sorted(zip(names, cumulative_times), key=lambda x: x[1])
print("Name\t\tCumulative Time")
print("-----------------------------")
for name, time in sorted_data:
print(f"{name}\t{str(time)}")
def write_csv_file(names, cumulative_times, filename):
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Name", "Cumulative Time"])
for name, time in zip(names, cumulative_times):
writer.writerow([name, str(time)])
# Split times as deltas (reverse the order of the array)
split_times = [
"00:01.78",
"03:09.26",
"00:00.59",
"01:22.88",
"00:02.00",
"00:27.66",
"06:23.23"
][::-1]
# Names of the runners (reverse the order of the array)
names = [
"Yen",
"Skipper",
"Sim",
"Amanda",
"Maggie",
"Yizhen",
"Brian"
][::-1]
# Convert split times to timedelta objects
split_times = [convert_to_timedelta(time) for time in split_times]
# Calculate cumulative split times
cumulative_times = calculate_cumulative_split_times(split_times)
# Print the results
print_cumulative_split_times(names, cumulative_times)
# Write results to a CSV file
write_csv_file(names, cumulative_times, 'split_times.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment