Last active
December 12, 2015 08:49
-
-
Save naush/4746770 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Author
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
This doesn't identify all possible menus.
e.g.
should return 8 possible choices for orders: