2017-09-21 9 views
1

私はルビーが新しく、ハッシュとキーを含む問題を解決しています。この問題は、ハッシュを引数として受け入れるメソッド#pet_typesを実装するように求めています。ハッシュは人の#名前をキーとして使用し、値はその人が所有するペットのタイプの配列です。私の質問は、配列内の各numを繰り返し処理するために各メソッドを使用することです。私はハッシュ#またはhash.sort.eachを使用して問題を解決する間に何か違いがあるのだろうかと思いましたか? 私は数時間かけて別の解決策を考え出しましたが、それでも問題を解決する2つの方法の違いは何かを把握することができました。 https://repl.it/H0xp/6か、以下を参照してくださいすることができます:ルビーにハッシュを実装する際の質問

私はrepl.itに私のコードが含まれ

# Pet Types 
# ------------------------------------------------------------------------------ 
# Implement a method, #pet_types, that accepts a hash as an argument. The hash uses people's 
# names as keys, and the values are arrays of pet types that the person owns. 

# Example input: 
# { 
# "yi" => ["dog", "cat"], 
# "cai" => ["dog", "cat", "mouse"], 
# "venus" => ["mouse", "pterodactyl", "chinchilla", "cat"] 
# } 
def pet_types(owners_hash) 

    results = Hash.new {|h, k| h[k] = [ ] } 
    owners_hash.sort.each { |k, v| v.each { |pet| results[pet] << k } } 
    results 
end 

puts "-------Pet Types-------" 

owners_1 = { 
    "yi" => ["cat"] 
} 
output_1 = { 
    "cat" => ["yi"] 
} 

owners_2 = { 
    "yi" => ["cat", "dog"] 
} 
output_2 = { 
    "cat" => ["yi"], 
    "dog" => ["yi"] 
} 

owners_3 = { 
    "yi" => ["dog", "cat"], 
    "cai" => ["dog", "cat", "mouse"], 
    "venus" => ["mouse", "pterodactyl", "chinchilla", "cat"] 
} 
output_3 = { 
    "dog" => ["cai", "yi"], 
    "cat" => ["cai", "venus", "yi"], 
    "mouse" => ["cai", "venus"], 
    "pterodactyl" => ["venus"], 
    "chinchilla" => ["venus"] 
} 


    # method 2 
    # The 2nd and 3rd method should return a hash that uses the pet types as keys and the values should 
    # be a list of the people that own that pet type. The names in the output hash should 
    # be sorted alphabetically 

    # switched_hash = Hash.new() 

    # owners_hash.each do |owner, pets_array| 
    # pets_array.each do |pet| 
    #  select_owners = owners_hash.select { |owner, pets_array| 

owners_hash[owner].include?(pet) } 

    #  switched_hash[pet] = select_owners.keys.sort 
    # end 
    # end 

    # method 3 
    #switched_hash 
    # pets = Hash.new {|h, k| h[k] = [ ] } # WORKS SAME AS: pets = Hash.new(Array.new) 
    # owners = owners_hash.keys.sort 
    # owners.each do |owner| 
    # owners_hash[owner].each do |pet| 
    #  pets[pet] << owner 
    # end 
    # end 
    # pets 
# Example output: 

# output_3 = { 
# "dog" => ["cai", "yi"], 
# "cat" => ["cai", "venus", "yi"], ---> (sorted alphabetically!) 
# "mouse" => ["cai", "venus"], 
# "pterodactyl" => ["venus"], 
# "chinchilla" => ["venus"] 
# } 

は、私が最初にこの問題を解決するために私のプログラムでは、ハッシュデータ構造を使用していました。その後、pet_hashを使って書き直そうとしました。

def pet_types(owners_hash) 
    pets_hash = Hash.new { |k, v| v = [] } 

    owners_hash.each do |owner, pets| 
    pets.each do |pet| 
     pets_hash[pet] += [owner] 
    end 
    end 


    pets_hash.values.each(&:sort!) 

    pets_hash 
end 

puts "-------Pet Types-------" 
owners_1 = { 
    "yi" => ["cat"] 
} 
output_1 = { 
    "cat" => ["yi"] 
} 
owners_2 = { 
    "yi" => ["cat", "dog"] 
} 
output_2 = { 
    "cat" => ["yi"], 
    "dog" => ["yi"] 
} 
owners_3 = { 
    "yi" => ["dog", "cat"], 
    "cai" => ["dog", "cat", "mouse"], 
    "venus" => ["mouse", "pterodactyl", "chinchilla", "cat"] 
} 
output_3 = { 
    "dog" => ["cai", "yi"], 
    "cat" => ["cai", "venus", "yi"], 
    "mouse" => ["cai", "venus"], 
    "pterodactyl" => ["venus"], 
    "chinchilla" => ["venus"] 
} 
puts pet_types(owners_1) == output_1 
puts pet_types(owners_2) == output_2 
puts pet_types(owners_3) == output_3 
+0

私はコードをテストし、それぞれのメソッドはすべてテストに合格しました。 – DataEngineer

答えて

2

ハッシュ#ソートは、Array#ソートが続くハッシュ#のto_aとして(少なくとも私の基本的なテスト用)と同じ効果を持っていますし、私の最後のコードは次のようです。

hash = {b: 2, a: 1} 
hash.to_a.sort # => [[:a, 1, [:b, 2]] 
hash.sort  # => the same 

次に、ハッシュとアレイの両方で#eachを見てみましょう。

ブロックに2つの引数を指定すると、両方の場合に対応できます。ハッシュの場合は、最初の引数がキーになり、2番目の値が値になります。ネストされた配列の場合、値は基本的に引数に出スプラッティングを取得:

[[:a, 1, 2], [:b, 3, 4]].each { |x, y, z| puts "#{x}-#{y}-#{z}" } 
# => a-1-2 
# => b-3-4 

だから基本的に、あなたは、Array#ソートに続いて#のto_aをハッシュへの近道であることをハッシュ#ソートを考えると、その#を認識すべきですそれぞれは、配列(入れ子にされた配列)に変換されたハッシュとして同じものをハッシュで処理します。この場合、どちらのアプローチをとっても問題ありません。明らかに、キーで繰り返しのソートが必要な場合は、sortを使用する必要があります。

関連する問題