Skip to content

Instantly share code, notes, and snippets.

@enderahmetyurt
Created September 15, 2025 09:24
Show Gist options
  • Select an option

  • Save enderahmetyurt/0779d753bbbdb3885c06fca0675d49ae to your computer and use it in GitHub Desktop.

Select an option

Save enderahmetyurt/0779d753bbbdb3885c06fca0675d49ae to your computer and use it in GitHub Desktop.
You are given an array of arrays, where each inner array represents the runs scored by each team in an inning of a baseball game: [[home1, away1], [home2, away2], ...]. Write a function that returns an object with the total runs for each team, which innings each team led, and who won the game.
# You are given an array of arrays, where each inner array represents the runs scored by each team in an inning of a baseball game: [[home1, away1], [home2, away2], ...]. Write a function that returns an object with the total runs for each team, which innings each team led, and who won the game.
# Example:
# const innings = [[1, 0], [2, 2], [0, 3], [4, 1]];
# > analyzeBaseballGame(innings)
# > {
# homeTotal: 7,
# awayTotal: 6,
# homeLedInnings: [1, 2, 4],
# awayLedInnings: [3],
# winner: "home"
# }
#
def analyze_baseball_game(innings)
home_total = 0
away_total = 0
home_led_innings = []
away_led_innings = []
innings.each_with_index do |(home, away), index|
home_total += home
away_total += away
if home_total > away_total
home_led_innings << (index + 1)
elsif away_total > home_total
away_led_innings << (index + 1)
end
end
winner = if home_total > away_total
"home"
elsif away_total > home_total
"away"
else
"draw"
end
{
homeTotal: home_total,
awayTotal: away_total,
homeLedInnings: home_led_innings,
awayLedInnings: away_led_innings,
winner: winner
}
end
innings = [[1, 0], [2, 2], [0, 3], [4, 1]]
puts analyze_baseball_game(innings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment