Created
October 1, 2013 05:15
-
-
Save polycarpou/6774144 to your computer and use it in GitHub Desktop.
Hasketball question without the bonus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
# | |
# 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 | |
hasketball = { | |
:lions => { | |
:colors => ["yellow", "gold"], | |
:players1 => | |
{ | |
:name => "Lex", | |
:number => "1", | |
:shoe => "11", | |
:stats => [14,4,3,7,5,2] | |
}, | |
:player2 => | |
{ | |
:name => "Isaac", | |
:number => "3", | |
:shoe => "12", | |
:stats => [8,3,6,17,2,8] | |
}, | |
:player3 => | |
{ | |
:name => "Oprah", | |
:number => "5", | |
:shoe => "13", | |
:stats => [1,2,6,3,1,0] | |
}, | |
:player4 => | |
{ | |
:name => "Nick", | |
:number => "7", | |
:shoe => "14", | |
:stats => [2,6,8,4,1,12] | |
}, | |
:player5 => | |
{ | |
:name => "Stan", | |
:number => "8", | |
:shoe => "15", | |
:stats => [8,2,7,4,2,6] | |
} | |
}, | |
:bears => { | |
:colors => ["brown", "white"], | |
:player1 => | |
{ | |
:name =>"Bill", | |
:number => "2", | |
:shoe => "13", | |
:stats => [4,13,7,4,8,1] | |
}, | |
:player2 => | |
{ | |
:name => "Ed", | |
:number => "4", | |
:shoe => "14", | |
:stats => [2,12,7,2,9,1] | |
}, | |
:player3 => | |
{ | |
:name => "Alex", | |
:number => "6", | |
:shoe => "11", | |
:stats => [11,3,1,7,4,2] | |
}, | |
:player4 => | |
{ | |
:name => "Rob", | |
:number => "8", | |
:shoe => "15", | |
:stats => [8,2,6,3,2,8] | |
}, | |
:player5 => | |
{ | |
:name => "Sam", | |
:number => "10", | |
:shoe => "9", | |
:stats => [9,2,2,1,12,8] | |
} | |
} | |
} | |
# 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 player_points(hasketball, player) | |
hasketball.keys.each do |teams| | |
hasketball[teams].keys.each do |p| | |
if p != :colors && hasketball[teams][p][:name] == player | |
return hasketball[teams][p][:stats][0] | |
end | |
end | |
end | |
end | |
#p player_points(hasketball, "Oprah") | |
# Return the shoe size for any player: | |
# | |
def player_shoe(hasketball, player) | |
hasketball.keys.each do |teams| | |
hasketball[teams].keys.each do |p| | |
if p != :colors && hasketball[teams][p][:name] == player | |
return hasketball[teams][p][:shoe] | |
end | |
end | |
end | |
end | |
#p player_shoe(hasketball, "Oprah") | |
# Return both colors for any team: | |
# | |
def team_colors(hasketball, team) | |
hasketball.keys.each do |t| | |
if t == team | |
return hasketball[t][:colors] | |
end | |
end | |
end | |
#p team_colors(hasketball, :lions) | |
# Return both teams names: | |
# | |
def team_names(hasketball) | |
hasketball.keys | |
end | |
#p team_names(hasketball) | |
# Return all the player numbers for a team: | |
# | |
# Return all the stats for a player: | |
# | |
def player_stat(hasketball, player) | |
hasketball.keys.each do |teams| | |
hasketball[teams].keys.each do |p| | |
if p != :colors && hasketball[teams][p][:name] == player | |
return hasketball[teams][p][:stats] | |
end | |
end | |
end | |
end | |
#p player_stat(hasketball, "Lex") | |
# Return the rebounds for the player with the largest shoe size | |
# | |
def largest_shoe_rebounds(hasketball) | |
largest_shoe = 0 | |
rebounds = 0 | |
hasketball.keys.each do |teams| | |
hasketball[teams].keys.each do |p| | |
if p != :colors && hasketball[teams][p][:shoe].to_i > largest_shoe | |
rebounds = hasketball[teams][p][:stats][1] | |
end | |
end | |
end | |
rebounds | |
end | |
p largest_shoe_rebounds(hasketball) | |
# 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