Last active
December 24, 2015 08:59
-
-
Save rayning0/6774203 to your computer and use it in GitHub Desktop.
Hasketball (finished ALL Hasketball questions, including all 4 bonus questions!)
This file contains hidden or 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
| # Raymond Gan, Oct. 1, 2013 | |
| game = { | |
| :home => { :teamname => "LA Lakers", | |
| :colors => ["gold", "orange"], | |
| :players => [ | |
| {:playername => "Raymond Gan", | |
| :number => 3, | |
| :shoesize => 12, | |
| :stats => { | |
| :points => 20, | |
| :rebounds => 5, | |
| :assists => 8, | |
| :steals => 10, | |
| :blocks => 15, | |
| :slamdunks => 20 | |
| } | |
| }, | |
| {:playername => "Sam Wang", | |
| :number => 2, | |
| :shoesize => 16, | |
| :stats => { | |
| :points => 12, | |
| :rebounds => 4, | |
| :assists => 7, | |
| :steals => 7, | |
| :blocks => 15, | |
| :slamdunks => 10 | |
| } | |
| }, | |
| {:playername => "Hercule Poirot", | |
| :number => 2, | |
| :shoesize => 14, | |
| :stats => { | |
| :points => 24, | |
| :rebounds => 12, | |
| :assists => 12, | |
| :steals => 4, | |
| :blocks => 5, | |
| :slamdunks => 5 | |
| } | |
| }, | |
| {:playername => "Batman", | |
| :number => 8, | |
| :shoesize => 15, | |
| :stats => { | |
| :points => 33, | |
| :rebounds => 3, | |
| :assists => 2, | |
| :steals => 1, | |
| :blocks => 1, | |
| :slamdunks => 0 | |
| } | |
| }, | |
| {:playername => "Yo Momma", | |
| :number => 33, | |
| :shoesize => 15, | |
| :stats => { | |
| :points => 6, | |
| :rebounds => 12, | |
| :assists => 12, | |
| :steals => 22, | |
| :blocks => 5, | |
| :slamdunks => 12 | |
| } | |
| } | |
| ] | |
| }, | |
| :away => { :teamname => "San Francisco Watermelons", | |
| :colors => ["blue", "red"], | |
| :players => [ | |
| {:playername => "Crazy Eight", | |
| :number => 0, | |
| :shoesize => 16, | |
| :stats => { | |
| :points => 22, | |
| :rebounds => 12, | |
| :assists => 12, | |
| :steals => 3, | |
| :blocks => 1, | |
| :slamdunks => 1 | |
| } | |
| }, | |
| {:playername => "Frodo Baggins", | |
| :number => 30, | |
| :shoesize => 14, | |
| :stats => { | |
| :points => 12, | |
| :rebounds => 12, | |
| :assists => 12, | |
| :steals => 12, | |
| :blocks => 12, | |
| :slamdunks => 7 | |
| } | |
| }, | |
| {:playername => "Brad Pitt", | |
| :number => 11, | |
| :shoesize => 17, | |
| :stats => { | |
| :points => 17, | |
| :rebounds => 19, | |
| :assists => 10, | |
| :steals => 3, | |
| :blocks => 1, | |
| :slamdunks => 15 | |
| } | |
| }, | |
| {:playername => "Goofy", | |
| :number => 1, | |
| :shoesize => 19, | |
| :stats => { | |
| :points => 26, | |
| :rebounds => 12, | |
| :assists => 6, | |
| :steals => 3, | |
| :blocks => 8, | |
| :slamdunks => 5 | |
| } | |
| }, | |
| {:playername => "Mickey Mouse", | |
| :number => 31, | |
| :shoesize => 15, | |
| :stats => { | |
| :points => 19, | |
| :rebounds => 2, | |
| :assists => 2, | |
| :steals => 4, | |
| :blocks => 11, | |
| :slamdunks => 1 | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| def playerpoints(player, game) | |
| game.each do |k, v| | |
| v[:players].each do |k2, v2| | |
| return k2[:stats][:points] if k2[:playername] == player | |
| end | |
| end | |
| end | |
| player = "Mickey Mouse" | |
| puts "1. #{player} scored #{playerpoints('Mickey Mouse', game)}" | |
| # game[:home][:players][0][:stats][:points] | |
| def shoesize(player, game) | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| return k2[:shoesize] if k2[:playername] == player | |
| end | |
| end | |
| end | |
| puts "2. #{player}'s shoe size is #{shoesize(player, game)}" | |
| def teamcolors(team, game) | |
| game.each_value do |v| | |
| return v[:colors] if v[:teamname] == team | |
| end | |
| end | |
| team = "San Francisco Watermelons" | |
| puts "3. #{team}'s colors are #{teamcolors(team, game)}" | |
| def teamnames(game) | |
| game.each_value.collect {|v| v[:teamname]} | |
| end | |
| puts "4. The team names are #{teamnames(game)}" | |
| def playernums(team, game) | |
| nums = [] | |
| game.each_value do |v| | |
| v[:players].each do |k, v2| | |
| nums << k[:number] if v[:teamname] == team | |
| end | |
| end | |
| nums | |
| end | |
| team = "LA Lakers" | |
| puts "5. The numbers for team #{team} are #{playernums(team, game)}" | |
| def playerstats(player, game) | |
| stats = [] | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| k2.each do |k3| | |
| stats = k3 if k2[:playername] == player | |
| end | |
| end | |
| end | |
| stats[1] | |
| end | |
| player = "Sam Wang" | |
| puts "6. Stats for #{player} are #{playerstats(player, game)}" | |
| def rebounds(game) | |
| ssize = [] | |
| rebound = 0 | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| ssize << shoesize(k2[:playername], game) | |
| end | |
| end | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| rebound = k2[:stats][:rebounds] if k2[:shoesize] == ssize.max | |
| end | |
| end | |
| return rebound, ssize.max | |
| end | |
| puts "7. #{rebounds(game)[0]} rebounds for player with max shoe size of #{rebounds(game)[1]}" | |
| puts | |
| # Bonus Questions | |
| def playermostpoints(game) | |
| points = {} | |
| player = '' | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| points[k2[:playername]] = playerpoints(k2[:playername], game) | |
| end | |
| end | |
| player = points.key(points.values.max) | |
| return player, points.values.max | |
| end | |
| puts "Bonus 1. #{playermostpoints(game)[0]} got the most points of all players: #{playermostpoints(game)[1]}" | |
| def teammostpoints(game) | |
| tnames = teamnames(game) | |
| totaltpoints = {} | |
| teammax = '' | |
| tnames.each do |t| | |
| tpoints = 0 | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| tpoints += playerpoints(k2[:playername], game) if v[:teamname] == t | |
| end | |
| end | |
| totaltpoints[t] = tpoints | |
| end | |
| teammax = totaltpoints.key(totaltpoints.values.max) | |
| return teammax, totaltpoints.values.max | |
| end | |
| teammostpoints(game) | |
| puts "Bonus 2. #{teammostpoints(game)[0]} got the most points of all teams: #{teammostpoints(game)[1]}" | |
| def longestname(game) | |
| names = [] | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| names << k2[:playername] | |
| end | |
| end | |
| names.max_by(&:length) | |
| end | |
| longestn = longestname(game) | |
| puts "Bonus 3. Longest name of all players: #{longestn}" | |
| def moststeals?(longestn, game) | |
| steals = [] | |
| stealslongestn = 0 | |
| game.each_value do |v| | |
| v[:players].each do |k2, v2| | |
| steals << k2[:stats][:steals] | |
| stealslongestn = k2[:stats][:steals] if k2[:playername] == longestn | |
| end | |
| end | |
| return steals.max, stealslongestn, steals.max == stealslongestn | |
| end | |
| moststeals = [] | |
| moststeals = moststeals?(longestn, game) | |
| puts | |
| puts "Super Bonus: Did #{longestn} have the most steals? #{moststeals[2]}." | |
| puts "He had #{moststeals[1]} steals, but maximum steals by any player was #{moststeals[0]}" | |
| =begin ---------------OUTPUT---------------- | |
| 1. Mickey Mouse scored 19 | |
| 2. Mickey Mouse's shoe size is 15 | |
| 3. San Francisco Watermelons's colors are ["blue", "red"] | |
| 4. The team names are ["LA Lakers", "San Francisco Watermelons"] | |
| 5. The numbers for team LA Lakers are [3, 2, 2, 8, 33] | |
| 6. Stats for Sam Wang are {:points=>12, :rebounds=>4, :assists=>7, :steals=>7, :blocks=>15, :slamdunks=>10} | |
| 7. 12 rebounds for player with max shoe size of 19 | |
| Bonus 1. Batman got the most points of all players: 33 | |
| Bonus 2. San Francisco Watermelons got the most points of all teams: 96 | |
| Bonus 3. Longest name of all players: Hercule Poirot | |
| Super Bonus: Did Hercule Poirot have the most steals? false. | |
| He had 4 steals, but maximum steals by any player was 22 | |
| =end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment