Skip to content

Instantly share code, notes, and snippets.

@joegiralt
Created June 11, 2013 00:44
Show Gist options
  • Save joegiralt/5753700 to your computer and use it in GitHub Desktop.
Save joegiralt/5753700 to your computer and use it in GitHub Desktop.
hashkett ball
# 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.
# 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.
# Return the shoe size for any player.
# Return both colors for any team.
# Return both teams names.
$game =
{#new hash
:team_A =>
{#new hash
:name => "Hornets",
:colors => ["red","blue"],
:player_1 =>
{#new hash
:name => "Josh",
:number => 22,
:shoe_size => 14,
:stats =>
{#new hash
:points => 200,
:rebounds => 321,
:assists => 123,
:steals => 131,
:blocks => 234,
:slam_dunks => 235
},
},
:player_2 =>
{#new hash
:name => "Ning",
:number => 23,
:shoe_size => 12,
:stats =>
{#new hash
:points => 245,
:rebounds => 311,
:assists => 93,
:steals => 34,
:blocks => 78,
:slam_dunks => 500
},
},
:player_3 =>
{#new hash
:name => "Chris",
:number => 24,
:shoe_size => 12,
:stats =>
{#new hash
:points => 240,
:rebounds => 319,
:assists => 83,
:steals => 35,
:blocks => 72,
:slam_dunks => 40
},
},
:player_4 =>
{#new hash
:name => "Joe",
:number => 27,
:shoe_size => 11,
:stats =>
{#new hash
:points => 245,
:rebounds => 311,
:assists => 93,
:steals => 34,
:blocks => 78,
:slam_dunks => 500
},
},
:player_5 =>
{#new hash
:name => "Mendel",
:number => 99,
:shoe_size => 9,
:stats =>
{#new hash
:points => 267,
:rebounds => 309,
:assists => 193,
:steals => 84,
:blocks => 98,
:slam_dunks => 670
},
},
},
:team_B =>
{#new hash
:name => "jets",
:colors => ["yellow","green"],
:player_1 =>
{#new hash
:name => "Thomas",
:number => 12,
:shoe_size => 14,
:stats =>
{#new hash
:points => 200,
:rebounds => 321,
:assists => 123,
:steals => 131,
:blocks => 234,
:slam_dunks => 235
},
},
:player_2 =>
{#new hash
:name => "Jordan",
:number => 63,
:shoe_size => 12,
:stats =>
{#new hash
:points => 245,
:rebounds => 311,
:assists => 93,
:steals => 34,
:blocks => 78,
:slam_dunks => 500
},
},
:player_3 =>
{#new hash
:name => "George",
:number => 64,
:shoe_size => 12,
:stats =>
{#new hash
:points => 240,
:rebounds => 319,
:assists => 83,
:steals => 35,
:blocks => 72,
:slam_dunks => 40
},
},
:player_4 =>
{#new hash
:name => "kristan",
:number => 67,
:shoe_size => 11,
:stats =>
{#new hash
:points => 245,
:rebounds => 311,
:assists => 93,
:steals => 34,
:blocks => 78,
:slam_dunks => 500
},
},
:player_5 =>
{#new hash
:name => "Ruthie",
:number => 98,
:shoe_size => 9,
:stats =>
{#new hash
:points => 267,
:rebounds => 309,
:assists => 193,
:steals => 84,
:blocks => 98,
:slam_dunks => 670
}
}
}
}
# 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
# Using the power of Ruby, and the Hashes you created above, answer the following questions:
# Return the number of points scored for any player.
puts "#{$game[:team_A][:player_1][:stats][:points]}"
# Return the shoe size for any player.
puts "#{$game[:team_B][:player_4][:shoe_size] }"
# Return both colors for any team.
puts "#{$game[:team_B][:colors]}"
# Return both teams names.
puts "#{$game[:team_A][:name]}\n#{$game[:team_B][:name]}"
# Return all the player numbers for a team.
for num in (1..5)
puts "#{$game[:team_A]["player_#{num}".to_sym][:number]}"
end
# Return all the stats for a player.
puts "#{$game[:team_A][:player_2][:stats]}"
# Return the rebounds for the player with the largest shoe size.
def point_shoe_size
array = []
for num in (1..5)
array << $game[:team_A]["player_#{num}".to_sym][:shoe_size]
$x = array.max
end
for num in 1..5
if $x = $game[:team_A]["player_#{num}".to_sym][:shoe_size]
puts "#{$game[:team_A]["player_#{num}".to_sym][:name]}" + " #{$game[:team_A]["player_#{num}".to_sym][:stats][:rebounds]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment