Keys of a hash whose values sum to a particular value

General Tech Bugs & Fixes 2 years ago

0 3 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (3)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I have a hash:

a = {"Q1"=>1, "Q2"=>2, "Q5"=>3, "Q8"=>3}

I want to retrieve a set of keys from it such that the sum of their values equals a certain number, for example 5. In such case, the output should be:

Q2 Q5

Please help me on how to get this.

profilepic.png
manpreet 2 years ago
def fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, tot)
  arr = h.to_a
  (href="https://forum.tuteehub.com/tag/1">1..arr.size).fihref="https://forum.tuteehub.com/tag/n">nd do |href="https://forum.tuteehub.com/tag/n">n|
    ehref="https://forum.tuteehub.com/tag/n">num = arr.combihref="https://forum.tuteehub.com/tag/n">natiohref="https://forum.tuteehub.com/tag/n">n(href="https://forum.tuteehub.com/tag/n">n).fihref="https://forum.tuteehub.com/tag/n">nd { |e| e.map(&:last).sum == tot }
    returhref="https://forum.tuteehub.com/tag/n">n ehref="https://forum.tuteehub.com/tag/n">num.map(&:first) uhref="https://forum.tuteehub.com/tag/n">nless ehref="https://forum.tuteehub.com/tag/n">num.href="https://forum.tuteehub.com/tag/n">nil?
  href="https://forum.tuteehub.com/tag/ehref="https://forum.tuteehub.com/tag/n">nd">ehref="https://forum.tuteehub.com/tag/n">nd
 href="https://forum.tuteehub.com/tag/ehref="https://forum.tuteehub.com/tag/n">nd">ehref="https://forum.tuteehub.com/tag/n">nd
h = {"Qhref="https://forum.tuteehub.com/tag/1">1"=>href="https://forum.tuteehub.com/tag/1">1, "Q2"=>2, "Qhref="https://forum.tuteehub.com/tag/5">5"=>3, "Q8"=>3}

fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, href="https://forum.tuteehub.com/tag/5">5)   #=> ["Q2", "Qhref="https://forum.tuteehub.com/tag/5">5"] 
fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, 2)   #=> ["Q2"] 
fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, 6)   #=> ["Qhref="https://forum.tuteehub.com/tag/5">5", "Q8"] 
fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, 4)   #=> ["Qhref="https://forum.tuteehub.com/tag/1">1", "Qhref="https://forum.tuteehub.com/tag/5">5"] 
fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, 8)   #=> ["Q2", "Qhref="https://forum.tuteehub.com/tag/5">5", "Q8"] 
fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, 9)   #=> ["Qhref="https://forum.tuteehub.com/tag/1">1", "Q2", "Qhref="https://forum.tuteehub.com/tag/5">5", "Q8"] 
fihref="https://forum.tuteehub.com/tag/n">nd_combo(h, href="https://forum.tuteehub.com/tag/1">10)  #=> href="https://forum.tuteehub.com/tag/n">nil 

0 views   0 shares

profilepic.png
manpreet 2 years ago

Just out of curiosity:

hash = {"Q1"=>1, "Q2"=>2, "Q5"=>3, "Q8"=>3}
arr = hash.to_a

1.upto(hash.size).
  lazy.
  find do |i|
    res = arr.combination(i).find do |h|
      h.map(&:last).sum == 5
    end
    break res if res
  end.tap { |result| break result.to_h if result }  
#⇒ {"Q2" => 2, "Q5" => 3}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.