Created
August 20, 2024 15:54
-
-
Save dpjanes/3d06a53f57b6f42e9e1ed8fc93b86372 to your computer and use it in GitHub Desktop.
Compute the odds of winning the game when ahead in the 6th inning
This file contains 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
# Code by GPT-4o | |
# data from: | |
# https://www.retrosheet.org/gamelogs/index.html | |
def parse_line_score(line_score): | |
# Initialize scores | |
visitor_score = 0 | |
home_score = 0 | |
# Split the line score by inning | |
visitor_innings = list(line_score[0]) | |
home_innings = list(line_score[1]) | |
# Parse each inning up to the 6th | |
for i in range(min(6, len(visitor_innings))): | |
if visitor_innings[i].isdigit(): | |
visitor_score += int(visitor_innings[i]) | |
for i in range(min(6, len(home_innings))): | |
if home_innings[i].isdigit(): | |
home_score += int(home_innings[i]) | |
return visitor_score, home_score | |
def process_game_data(file_path): | |
games = 0 | |
ahead_in_6th_won = 0 | |
with open(file_path, 'r') as file: | |
for line in file: | |
games += 1 | |
fields = line.strip().split(',') | |
# Extract line scores for visiting and home teams (fields 20 and 21) | |
visitor_line_score = fields[19] | |
home_line_score = fields[20] | |
# Extract final scores (fields 10 and 11) | |
final_visitor_score = int(fields[9]) | |
final_home_score = int(fields[10]) | |
# Get the score at the end of the 6th inning | |
visitor_score_6th, home_score_6th = parse_line_score((visitor_line_score, home_line_score)) | |
# Determine which team was ahead in the 6th inning and if they won | |
if visitor_score_6th > home_score_6th: | |
if final_visitor_score > final_home_score: | |
ahead_in_6th_won += 1 | |
elif home_score_6th > visitor_score_6th: | |
if final_home_score > final_visitor_score: | |
ahead_in_6th_won += 1 | |
# Calculate the overall percentage | |
percentage = (ahead_in_6th_won / games) * 100 | |
return percentage | |
# Example usage | |
file_path = 'gl2023.txt' # replace with your file path | |
percentage = process_game_data(file_path) | |
print(f"Percentage of games where the team leading after the 6th inning won: {percentage:.2f}%") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment