Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. wishlist = [
  2. {:name => "mini puzzle", :size => "small", :clatters => "yes", :weight => "light"},
  3. {:name => "toy car", :size => "medium", :clatters => "a bit", :weight => "medium"},
  4. {:name => "card game", :size => "small", :clatters => "no", :weight => "light"}
  5. ]
  6.  
  7. presents = [
  8. {:size => "medium", :clatters => "a bit", :weight => "medium"},
  9. {:size => "small", :clatters => "yes", :weight => "light"}
  10. ]
  11.  
  12. # guess_gifts(wishlist, presents) # must return ['toy car', 'mini puzzle']
  13.  
  14. def guess_gifts(wishlist, presents)
  15. sizes = wishlist.map{|item| item[:size]}
  16. weights = wishlist.map{|item| item[:weight]}
  17. clatters = wishlist.map{|item| item[:clatters]}
  18.  
  19. gifts = []
  20. presents.each do |present|
  21. guess = wishlist.find{|gift| present[:size] == gift[:size] &&
  22. present[:clatters] == gift[:clatters] &&
  23. present[:weight] == gift[:weight] }
  24.  
  25. gifts << guess[:name]
  26. end
  27. return gifts
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement