forked from jasonmahony/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess_gifts.rb
More file actions
executable file
·29 lines (25 loc) · 893 Bytes
/
guess_gifts.rb
File metadata and controls
executable file
·29 lines (25 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env ruby
def guess_gifts(wishlist, presents)
gifts = Array.new
# iterate through present and wishlist hashes and find matches
presents.each do |p|
wishlist.each do |w|
if w[:size] == p[:size] && w[:clatters] == p[:clatters] && w[:weight] == p[:weight]
# Add matches to the gifts array
gifts.push(w[:name])
end
end
end
gifts.uniq
end
wishlist = [
{:name => "mini puzzle", :size => "small", :clatters => "yes", :weight => "light"},
{:name => "toy car", :size => "medium", :clatters => "a bit", :weight => "medium"},
{:name => "card game", :size => "small", :clatters => "no", :weight => "light"}
]
presents = [
{:size => "medium", :clatters => "a bit", :weight => "medium"},
{:size => "small", :clatters => "yes", :weight => "light"}
]
# must return ['toy car', 'mini puzzle']
puts guess_gifts(wishlist, presents)