Skip to content

Instantly share code, notes, and snippets.

@HeroicEric
Forked from jli-hashrocket/game_data.rb
Last active December 28, 2015 22:09
Show Gist options
  • Select an option

  • Save HeroicEric/7569907 to your computer and use it in GitHub Desktop.

Select an option

Save HeroicEric/7569907 to your computer and use it in GitHub Desktop.
require 'pry'
games = [
{
home_team: "Patriots",
away_team: "Broncos",
home_score: 7,
away_score: 3
},
{
home_team: "Broncos",
away_team: "Colts",
home_score: 3,
away_score: 0
},
{
home_team: "Patriots",
away_team: "Colts",
home_score: 11,
away_score: 7
},
{
home_team: "Steelers",
away_team: "Patriots",
home_score: 7,
away_score: 21
}
]
# I would probably use a hash to store this data so that I could label the
# wins and the losses.
#
# scores = {
# "Patriots" => [0,0],
# "Broncos" => [0,0],
# "Colts" => [0,0],
# "Steelers" => [0,0]
# }
scores = {
"Patriots" => { wins: 0, losses: 0 },
"Broncos" => { wins: 0, losses: 0 },
"Colts" => { wins: 0, losses: 0 },
"Steelers" => { wins: 0, losses: 0 }
}
games.each do |game|
if game[:home_score] > game[:away_score]
scores[game[:home_team]][:wins] += 1
scores[game[:away_team]][:losses] += 1
else
scores[game[:away_team]][:wins] += 1
scores[game[:home_team]][:losses] += 1
end
end
scores = scores.sort_by { |key, val| -val[:wins] }
scores.each do |key, val|
puts "#{key} #{val[:wins]} wins, #{val[:losses]} losses"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment