Skip to content

Instantly share code, notes, and snippets.

@samtalks
Created October 1, 2013 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samtalks/6778482 to your computer and use it in GitHub Desktop.
Save samtalks/6778482 to your computer and use it in GitHub Desktop.
# sam's algorithm for solving these.
# first, lay out three panels: screen shot of data below, bash window, & subl for visualization
# second, drill down carefully solving for entire set of a kind for results
# third, apply the 'filter' or the conditional to narrow results
game = {
:home => { :team_name => "Charlotte Hornets",
:colors => ["Turquoise", "Purple"],
:players => [
{:player_name => "Jeff Adrien",
:number => 4,
:shoe_size => 18,
:stats => {
:points => 10,
:rebounds => 1,
:assists => 1,
:steals => 2,
:blocks => 7,
:slam_dunks => 2
}
},
{:player_name => "Bismak Biyombo",
:number => 0,
:shoe_size => 16,
:stats => {
:points => 12,
:rebounds => 4,
:assists => 7,
:steals => 7,
:blocks => 15,
:slam_dunks => 10
}
},
{:player_name => "DeSagna Diop",
:number => 2,
:shoe_size => 14,
:stats => {
:points => 24,
:rebounds => 12,
:assists => 12,
:steals => 4,
:blocks => 5,
:slam_dunks => 5
}
},
{:player_name => "Ben Gordon",
:number => 8,
:shoe_size => 15,
:stats => {
:points => 33,
:rebounds => 3,
:assists => 2,
:steals => 1,
:blocks => 1,
:slam_dunks => 0
}
},
{:player_name => "Brendan Haywood",
:number => 33,
:shoe_size => 15,
:stats => {
:points => 6,
:rebounds => 12,
:assists => 12,
:steals => 22,
:blocks => 5,
:slam_dunks => 12
}
}
]
},
:away => { :team_name => "Brooklyn Nets",
:colors => ["Black", "White"],
:players => [
{:player_name => "Alan Anderson",
:number => 0,
:shoe_size => 16,
:stats => {
:points => 22,
:rebounds => 12,
:assists => 12,
:steals => 3,
:blocks => 1,
:slam_dunks => 1
}
},
{:player_name => "Reggie Evans",
:number => 30,
:shoe_size => 14,
:stats => {
:points => 12,
:rebounds => 12,
:assists => 12,
:steals => 12,
:blocks => 12,
:slam_dunks => 7
}
},
{:player_name => "Brook Lopez",
:number => 11,
:shoe_size => 17,
:stats => {
:points => 17,
:rebounds => 19,
:assists => 10,
:steals => 3,
:blocks => 1,
:slam_dunks => 15
}
},
{:player_name => "Mason Plumlee",
:number => 1,
:shoe_size => 19,
:stats => {
:points => 26,
:rebounds => 12,
:assists => 6,
:steals => 3,
:blocks => 8,
:slam_dunks => 5
}
},
{:player_name => "Jason Terry",
:number => 31,
:shoe_size => 15,
:stats => {
:points => 19,
:rebounds => 2,
:assists => 2,
:steals => 4,
:blocks => 11,
:slam_dunks => 1
}
}
]
}
}
# Return the number of points scored for any player.
def points_scored(game_hash, player)
game_hash.each do |team, team_hash|
team_hash[:players].each do |player_set, no_data|
return player_set[:stats][:points] if player_set[:player_name] == player
end
end
end
points_scored(game, "Jeff Adrien")
# Return the shoe size for any player.
def shoe_size?(game_hash, player)
game_hash.each_value do |team_set|
team_set[:players].each do |player_set|
return player_set[:shoe_size] if player_set[:player_name] == player
end
end
end
shoe_size?(game, "Jeff Adrien")
# Return both colors for any team.
def return_colors(game_hash, team)
game_hash.each do |side, side_hash|
side_hash.each do |team_id, team_hash|
return team_hash if team_id == :colors && side_hash[:team_name] == team
end
end
end
p return_colors(game, "Charlotte Hornets")
# Return both teams names.
def teams_playing(game_hash)
game_hash.each do |side, side_hash|
side_hash.each do |team_id, team_hash|
p team_hash if team_id == :team_name
end
end
end
teams_playing(game)
# Return all the player numbers for a team.
def player_nums(game_hash, team)
game_hash.each do |side, side_hash|
side_hash[:players].each do |player_array|
player_array.each do |player_ids, player_hash|
p player_hash if player_ids == :number && side_hash[:team_name] == team
end
end
end
end
# player_nums(game, "Charlotte Hornets")
# Return the rebounds for the player with the largest shoe size.
def largest_shoe(game_hash)
array = []
game_hash.each do |side, side_hash|
side_hash[:players].each do |player_array|
array << player_array[:shoe_size] # array of shoe sizes
end
side_hash[:players].each do |player_array|
p player_array[:stats][:rebounds] if player_array[:shoe_size] == array.max # rebounds only if max shoe size
end
end
end
# largest_shoe(game)
# 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?
# 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