Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs11puzzles-21wi
z3exercises
Commits
1f7e62fc
Commit
1f7e62fc
authored
4 years ago
by
Skynet0
Browse files
Options
Download
Email Patches
Plain Diff
Skeleton + tests
parents
master
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
menu_orderer.py
+68
-0
menu_orderer.py
tests/test_menu_orderer.py
+120
-0
tests/test_menu_orderer.py
with
188 additions
and
0 deletions
+188
-0
menu_orderer.py
0 → 100644
View file @
1f7e62fc
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))
This diff is collapsed.
Click to expand it.
tests/test_menu_orderer.py
0 → 100644
View file @
1f7e62fc
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
)
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help