Skip to content

Instantly share code, notes, and snippets.

@rosiehoyem
Created October 13, 2013 17:07
Show Gist options
  • Save rosiehoyem/6964630 to your computer and use it in GitHub Desktop.
Save rosiehoyem/6964630 to your computer and use it in GitHub Desktop.
Hashketball homework
# Using Nested Hashes, define a game, with two teams, their players, and the players stats:
# The game has two teams.
# A team has:
# A name
# Two colors
# Each team should have at least 5 players
# Each player should have a:
# name
# number (like their jersey number)
# shoe size
# Each player should have the following stats:
# points
# rebounds
# assists
# steals
# blocks
# slam dunks
game = {
:team_mn => {
:colors => ["blue" ,"white"],
:players => {
:player_1 => {
:name => "Kenny",
:number => 1,
:shoe_size => 13,
:stats => {
:points => 2,
:rebounds => 1,
:assists => 1,
:steals => 1,
:blocks => 1,
:slam_dunks => 1
}
},
:player_2 => {
:name => "Ben",
:number => 2,
:shoe_size => 13,
:stats => {
:points => 4,
:rebounds => 2,
:assists => 2,
:steals => 2,
:blocks => 2,
:slam_dunks => 2
}
},
:player_3 => {
:name => "Tony",
:number => 3,
:shoe_size => 13,
:stats => {
:points => 6,
:rebounds => 3,
:assists => 3,
:steals => 3,
:blocks => 3,
:slam_dunks => 3
}
},
:player_4 => {
:name => "Andy",
:number => 4,
:shoe_size => 13,
:stats => {
:points => 8,
:rebounds => 4,
:assists => 4,
:steals => 4,
:blocks => 4,
:slam_dunks => 4
}
},
:player_5 => {
:name => "Tony",
:number => 5,
:shoe_size => 13,
:stats => {
:points => 10,
:rebounds => 5,
:assists => 5,
:steals => 5,
:blocks => 5,
:slam_dunks => 5
}
}
}
},
:team_ny => {
:colors => ["blue" ,"white"],
:players => {
:player_1 => {
:name => "Sam",
:number => 11,
:shoe_size => 13,
:stats => {
:points => 3,
:rebounds => 1,
:assists => 1,
:steals => 1,
:blocks => 1,
:slam_dunks => 1,
}
},
:player_2 => {
:name => "Randy",
:number => 12,
:shoe_size => 13,
:stats => {
:points => 5,
:rebounds => 2,
:assists => 2,
:steals => 2,
:blocks => 2,
:slam_dunks => 2
}
},
:player_3 => {
:name => "Lenny",
:number => 13,
:shoe_size => 13,
:stats => {
:points => 7,
:rebounds => 3,
:assists => 3,
:steals => 3,
:blocks => 3,
:slam_dunks => 3
}
},
:player_4 => {
:name => "Randy",
:number => 14,
:shoe_size => 13,
:stats => {
:points => 9,
:rebounds => 4,
:assists => 4,
:steals => 4,
:blocks => 4,
:slam_dunks => 4
}
},
:player_5 => {
:name => "Ping",
:number => 15,
:shoe_size => 13,
:stats => {
:points => 11,
:rebounds => 5,
:assists => 5,
:steals => 5,
:blocks => 5,
:slam_dunks => 5
}
}
}
}
}
# Using the power of Ruby, and the Hashes you created above, answer the following questions:
# Return the number of points scored for any player.
def points_of_player(player_name, game)
game.each do |team, team_hash|
team_hash[:players].each do |player, player_hash|
if player_hash[:name] == player_name
return player_hash[:stats][:points]
end
end
end
end
puts points_of_player("Ping", game)
# # Return the shoe size for any player.
def shoesize_of_player(player_name, game)
game.each do |team, team_hash|
team_hash[:players].each do |player, player_hash|
if player_hash[:name] == player_name
return player_hash[:shoe_size]
end
end
end
end
puts shoesize_of_player("Ping", game)
# # Return both colors for any team.
# puts game[:team_mn][:colors]
# # Return both teams names.
# ##DOESN'T WORK##
# game[1]
# # Return all the player numbers for a team.
# roster = []
# game[:team_mn][players].each do |x|
# roster << x[:player_name]
# end
# # Return all the stats for a player.
# # Return the rebounds for the player with the largest shoe size.
# def player_largest_show_size
# game.each do |team, team_hash|
# player_shoesize = team_hash[:player].collect do |player_hash|
# player_hash
# end
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment