Commit 1f7e62fc authored by Skynet0's avatar Skynet0
Browse files

Skeleton + tests

parents
No related merge requests found
Showing with 188 additions and 0 deletions
+188 -0
from pprint import pprint
from typing import List, Tuple
from z3 import *
def find_all_orders(menu_items: List[Tuple[str, int]],
goal_price: int) -> List[List[Tuple[str, int]]]:
"""Find all orders that cost exactly the goal price, given the menu.
Inspired by https://xkcd.com/287/.
Args:
menu_items (List[Tuple[str, int]]): A list of all menu items and their
prices. Assume that all prices are positive.
goal_price (int): Desired exact amount to spend.
Returns:
List[List[Tuple[str, int]]]: A list of all orders that cost exactly the
goal price. Items not ordered are present with amount 0.
"""
pass
# TODO: Implement me!
if __name__ == '__main__':
pass
# You can uncomment the below test cases to try out your implementation,
# or add your own scenarios.
# xkcd_menu = [
# ('mixed_fruit', 215),
# ('french_fries', 275),
# ('side_salad', 335),
# ('hot_wings', 355),
# ('mozz_sticks', 420),
# ('sampler_plate', 580),
# ]
# goal_price = 1505
# pprint(find_all_orders(xkcd_menu, goal_price))
# red_door_menu = [
# ('avocado_toast', 625),
# ('blue_salad', 725),
# ('chicken_sandwich', 750),
# ('coffee', 230),
# ('mocha', 460),
# ('latte', 400),
# ('hot_chocolate', 300),
# ('chai', 350),
# ('cold_coffee', 500),
# ('brain_freeze', 525)
# ]
# goal_price = 1315
# pprint(find_all_orders(red_door_menu, goal_price))
# chouse_menu = [
# ('quesadilla', 100),
# ('chicken_nuggets', 300),
# ('burger', 400),
# ('boba', 150),
# ('tofu_nuggets', 150),
# ('fries', 150),
# ('special', 500),
# ('soda', 100),
# ]
# goal_price = 1025
# pprint(find_all_orders(chouse_menu, goal_price))
from typing import List, NamedTuple, Tuple
import menu_orderer
import pytest
class MenuOrdererTestCase(NamedTuple):
menu: List[Tuple[str, int]]
goal_price: int
expected_output: List[List[Tuple[str, int]]]
xkcd_287 = MenuOrdererTestCase(
menu=[
('mixed_fruit', 215),
('french_fries', 275),
('side_salad', 335),
('hot_wings', 355),
('mozz_sticks', 420),
('sampler_plate', 580),
],
goal_price=1505,
expected_output=[
[
('mixed_fruit', 1),
('french_fries', 0),
('side_salad', 0),
('hot_wings', 2),
('mozz_sticks', 0),
('sampler_plate', 1),
],
[
('mixed_fruit', 7),
('french_fries', 0),
('side_salad', 0),
('hot_wings', 0),
('mozz_sticks', 0),
('sampler_plate', 0),
],
],
)
red_door = MenuOrdererTestCase(
menu=[
('avocado_toast', 625),
('blue_salad', 725),
('chicken_sandwich', 750),
('coffee', 230),
('mocha', 460),
('latte', 400),
('hot_chocolate', 300),
('chai', 350),
('cold_coffee', 500),
('brain_freeze', 525),
],
goal_price=1315,
expected_output=[
[
('avocado_toast', 1),
('blue_salad', 0),
('chicken_sandwich', 0),
('coffee', 3),
('mocha', 0),
('latte', 0),
('hot_chocolate', 0),
('chai', 0),
('cold_coffee', 0),
('brain_freeze', 0),
],
[
('avocado_toast', 1),
('blue_salad', 0),
('chicken_sandwich', 0),
('coffee', 1),
('mocha', 1),
('latte', 0),
('hot_chocolate', 0),
('chai', 0),
('cold_coffee', 0),
('brain_freeze', 0),
],
],
)
chouse = MenuOrdererTestCase(
menu=[
('quesadilla', 100),
('chicken_nuggets', 300),
('burger', 400),
('boba', 150),
('tofu_nuggets', 150),
('fries', 150),
('special', 500),
('soda', 100),
],
goal_price=1025,
expected_output=[],
)
# Keep raw data outside the decorator to avoid polluting failed test traceback
# with irrelevant test data. The relevant data is included regardless.
@pytest.mark.parametrize('data', [
pytest.param(xkcd_287, id='xkcd_287'),
pytest.param(red_door, id='red_door'),
pytest.param(chouse, id='no_solns:chouse')
])
@pytest.mark.timeout(10)
def test_find_all_orders(data: MenuOrdererTestCase):
actual = menu_orderer.find_all_orders(data.menu, data.goal_price)
expected = data.expected_output
# Sort nested lists
for e in actual:
e.sort()
for e in expected:
e.sort()
# Compare outer sorted lists
assert sorted(actual) == sorted(expected)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment