Skip to content

Instantly share code, notes, and snippets.

@naush
Last active December 12, 2015 08:49
Show Gist options
  • Save naush/4746770 to your computer and use it in GitHub Desktop.
Save naush/4746770 to your computer and use it in GitHub Desktop.
# http://xkcd.com/287/
def order(menu, amount, items)
return [] if amount < 0
return [items] if amount == 0
menu.inject([]) do |meals, (item, price)|
meals += order(menu, amount - price, items + [item])
end
end
menu = {:mixed_fruit => 215, :french_fries => 275, :side_salad => 335, :hot_wings => 355, :mozzarella_sticks => 420, :sampler_plate => 580}
order(menu, 1505, []).map(&:sort).uniq
@naush
Copy link
Author

naush commented Feb 9, 2013

I intended it to return on the first match since the original comic didn't specify if he wanted all possible choices. Let me see if I can write a program that identify all choices. Thanks for providing a solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment