Skip to content

Instantly share code, notes, and snippets.

@sarony
Created October 1, 2013 13:09
Show Gist options
  • Select an option

  • Save sarony/6778182 to your computer and use it in GitHub Desktop.

Select an option

Save sarony/6778182 to your computer and use it in GitHub Desktop.
# Hashketball Nests
#
# Great news! You're going to an NBA game! The only catch is that you've been
# volunteered to keep stats at the game.
#
# Using Nested Hashes, define a game, with two teams, their players, and the players stats:
#
# The game has two teams.
game = {
:team1=> {
:name=>"TEAM 1",
:colors=> ["black", "white"],
:players=> [
{:name=>"Adam",
:jersey_number=>"10",
:shoe_size=>"19",
:stats=> {
:points=>"100",
:rebounds=>"10",
:assists=>"9",
:steams=>"8",
:blocks=>"7",
:slam_dunks=>"6"
}
},
{
:name=>"Bob",
:number=>"11",
:shoe_size=>"12",
:stats=> {
:points=>"110",
:rebounds=>"11",
:assists=>"10",
:steams=>"9",
:blocks=>"8",
:slam_dunks=>"7"
}
},
{
:name=>"Carl",
:number=>"12",
:shoe_size=>"13",
:stats=> {
:points=>"120",
:rebounds=>"12",
:assists=>"11",
:steams=>"10",
:blocks=>"9",
:slam_dunks=>"8"
}
},
{
:name=>"David",
:number=>"13",
:shoe_size=>"14",
:stats=> {
:points=>"130",
:rebounds=>"13",
:assists=>"12",
:steams=>"11",
:blocks=>"10",
:slam_dunks=>"9"
}
},
{
:name=>"Ethan",
:number=>"14",
:shoe_size=>"15",
:stats=> {:points=>"140",
:rebounds=>"14",
:assists=>"13",
:steams=>"12",
:blocks=>"11",
:slam_dunks=>"10"
}
}
]
},
:team2=>
{:name=>"TEAM 2",
:colors=>["red", "yellow"],
:players=> [
{:name=>"Zane",
:number=>"20",
:shoe_size=>"16",
:stats=> {
:points=>"200",
:rebounds=>"20",
:assists=>"19",
:steams=>"18",
:blocks=>"17",
:slam_dunks=>"16"
}
},
{
:name=>"Xavier",
:number=>"21",
:shoe_size=>"17",
:stats=> {
:points=>"210",
:rebounds=>"21",
:assists=>"20",
:steams=>"19",
:blocks=>"18",
:slam_dunks=>"17"
}
},
{
:name=>"Wade",
:number=>"22",
:shoe_size=>"18",
:stats=> {
:points=>"220",
:rebounds=>"22",
:assists=>"21",
:steams=>"20",
:blocks=>"19",
:slam_dunks=>"18"
}
},
{
:name=>"Venus",
:number=>"23",
:shoe_size=>"16",
:stats=> {
:points=>"230",
:rebounds=>"23",
:assists=>"19",
:steams=>"18",
:blocks=>"17",
:slam_dunks=>"16"
}
},
{
:name=>"Todd",
:number=>"24",
:shoe_size=>"11",
:stats=> {
:points=>"180",
:rebounds=>"20",
:assists=>"19",
:steams=>"18",
:blocks=>"17",
:slam_dunks=>"16"
}
}
]
}
}
# 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
# Using the power of Ruby, and the Hashes you created above, answer the following questions:
# Return the number of points scored for any player:
game[:team1][:players][0][:stats][:points]
# Return the shoe size for any player:
game[:team1][:players][0][:shoe_size]
# Return both colors for any team:
# puts game[:team1][:colors]
# puts game[:team2][:colors]
game.each do |team, value|
value.each do |key, value|
value if key==:colors
end
end
# game1.each do |team, value1|
# value1.each do |key, value2|
# puts value2 if key==:colors
# end
# # team.each do |key, value|
# # puts key
# # puts "The colors are #{value}!" if key=="colors"
# # end
# end
# Return both teams names:
game.each do |team, value|
value.each do |key, value|
value if key==:name
end
end
game1 = {
:team1=> {
:name=>"TEAM 1",
:colors=> ["black", "white"],
:players => [
{:name=>"Adam",
:jersey_number=>"10",
:shoe_size=>"19",
:stats=> {
:points=>"100",
:rebounds=>"10",
}},
{:name=>"Sam",
:jersey_number=>"10",
:shoe_size=>"11",
:stats=> {
:points=>"100",
:rebounds=>"10",
}
}
]
}
}
# Return all the player numbers for a team:
# game.each do |team, value|
# value.each do |key, value|
# if key==:players
# value.each do |item|
# item.collect do |key, value|
# value if key==:name
# end
# end
# end
# end
# end
# Return all the stats for a player:
game[:team1][:players][0][:stats]
# Return the rebounds for the player with the largest shoe size
shoes=[]
game.each do |team, value|
value.each do |key, value|
if key==:players
value.each do |item|
item.select do |key, value|
shoes<<value.to_i if key==:shoe_size
end
end
end
end
end
shoes.sort!
big_shoes=shoes[shoes.length-1]
game1.select do |team, value|
value.select do |key, value|
if key==:players
value.select do |item|
item.select do |key, value|
if item[:shoe_size]=19
big_foot=item[:stats][:rebounds]
puts big_foot
end
end
end
end
end
end
# puts game[:team1][:players].each do |key|
# puts key
# end
# [0][:shoe_size]
# Bonus Questions: define methods to return the answer to the following questions:
# Which player has the most points?
#
# Which team has the most points?
#
# Which player has the longest name?
#
# Super Bonus:
# Write a method that returns true if the player with the longest name had the most steals:
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment