Michael Carlos
Michael Carlos

Reputation: 41

Finding the largest value in a nested hash

I'm having trouble finding the largest value associated with a shoe size key in a hash. Here is what I have so far

    def biggest_shoe
      max_size = nil

      game_hash.each do |location, team_data|
        team_data[:players].each do |player, stats|
          size = stats[:shoe]
          if max_size < size
            max_size = size
            #binding.pry
          end
        end
      end
    end

I was able to successfully grab the individual value of a shoe throughout iteration with stats[:shoe] but from there I haven't figured a way to pull the largest size from the hash its iterating.

The hash:

    def game_hash
      {
        home: {
          :team_name => "Brooklyn Nets",
          :colors => ["Black", "White"],
          :players => {
            "Alan Anderson" => {
              :number => 0,
              :shoe => 16,
              :points => 22,
              :rebounds => 12,
              :assists => 12,
              :steals => 3,
              :blocks => 1,
              :slam_dunks => 1
            },
            "Reggie Evans" => {
              :number => 30,
              :shoe => 14,
              :points => 12,
              :rebounds => 12,
              :assists => 12,
              :steals => 12,
              :blocks => 12,
              :slam_dunks => 7
            },
            "Brook Lopez" => {
              :number => 11,
              :shoe => 17,
              :points => 17,
              :rebounds => 19,
              :assists => 10,
              :steals => 3,
              :blocks => 1,
              :slam_dunks => 15
            },
            "Mason Plumlee" => {
              :number => 1,
              :shoe => 19,
              :points => 26,
              :rebounds => 12,
              :assists => 6,
              :steals => 3,
              :blocks => 8,
              :slam_dunks => 5
            },
            "Jason Terry" => {
              :number => 31,
              :shoe => 15,
              :points => 19,
              :rebounds => 2,
              :assists => 2,
              :steals => 4,
              :blocks => 11,
              :slam_dunks => 1
            }
          }
        },
        away: {
          :team_name => "Charlotte Hornets",
          :colors => ["Turquoise", "Purple"],
          :players => {
            "Jeff Adrien" => {
              :number => 4,
              :shoe => 18,
              :points => 10,
              :rebounds => 1,
              :assists => 1,
              :steals => 2,
              :blocks => 7,
              :slam_dunks => 2,
            },
            "Bismak Biyombo" => {
              :number => 0,
              :shoe => 16,
              :points => 12,
              :rebounds => 4,
              :assists => 7,
              :steals => 7,
              :blocks => 15,
              :slam_dunks => 10
            },
            "DeSagna Diop" => {
              :number => 2,
              :shoe => 14,
              :points => 24,
              :rebounds => 12,
              :assists => 12,
              :steals => 4,
              :blocks => 5,
              :slam_dunks => 5
            },
            "Ben Gordon" => {
              :number => 8,
              :shoe => 15,
              :points => 33,
              :rebounds => 3,
              :assists => 2,
              :steals => 1,
              :blocks => 1,
              :slam_dunks => 0
            },
            "Brendan Haywood" => {
              :number => 33,
              :shoe => 15,
              :points => 6,
              :rebounds => 12,
              :assists => 12,
              :steals => 22,
              :blocks => 5,
              :slam_dunks => 12
            }
          }
        }
      }
    end

Upvotes: 1

Views: 390

Answers (3)

iGian
iGian

Reputation: 11183

This one liner to get all the shoe value into an array, then pick the max.

game_hash.map { |_, v| v[:players].map { |_, v| v[:shoe] } }.flatten.max

Creating a method to get the max for a chosen key

def pick_max_stat_from(h, key)
  h.map { |_, v| v[:players].map { |_, v| v[key] } }.flatten.max
end

pick_max_stat_from game_hash, :slam_dunks # => 15

Maybe this could be useful to get the max by location:

game_hash.transform_values { |v| v[:players].map { |_, v| v[:shoe] }.max }
# = {:home=>19, :away=>18}

Upvotes: 0

Cary Swoveland
Cary Swoveland

Reputation: 110675

game_hash[:home][:players].max_by { |_name, stats| stats[:shoe] }.last[:shoe]
  #=> 19

Upvotes: 1

tadman
tadman

Reputation: 211560

Like a lot of common things, Ruby has a tool for that:

game_hash.values.map do |team_data|
  team_data[:players]
end.values.map do |stats|
  stats[:shoe]
end.max

Upvotes: 2

Related Questions