-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshopping_list.py
195 lines (159 loc) · 5.53 KB
/
shopping_list.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def split_line_into_item_and_mark(line):
# write your code to the solution here
return []
def mark_to_bool(mark):
# enter code here
return False
def filter_out_ticked_items(items, marks):
filtered_items = []
# enter code here
return filtered_items
def get_list_of_items_to_get(shopping_list):
# enter code here
return []
# BELOW THIS IS EXTENSION QUESTIONS. MAKE SURE YOU HAVE ALL TESTS PASSING FOR THE FUNCTIONS
# BEFORE THIS.
def turn_shopping_list_to_dict(items, marks):
# Delete the line below and and write your code to the solution
return {}
def buy_item(shopping_dict, item):
# Delete the line below and and write your code to the solution
return shopping_dict
def make_shopping_list_string(shopping_dict):
# Delete the line below and and write your code to the solution
return ''
def get_unbought_items(shopping_dict):
# Delete the line below and and write your code to the solution
return []
# BELOW THIS ARE ALL FUNCTIONS YOU SHOULD NOT TOUCH!!. IF YOU ARE INTERESTED
# ABOUT HOW THESE FUNCTIONS WORK, FEEL FREE TO ASK ME.
def get_shopping_list():
shopping_list = []
while True:
line = input()
if line == '':
return '\n'.join(shopping_list)
else:
words = line.split(' ')
if len(words) != 2 or words[-1] not in {'x', '/'}:
print('This is not a valid shopping list item, please enter something else.')
continue
shopping_list += [line]
def split_string(s, character):
assert len(character) == 1, \
'you must use a single character to split a string, e.g. \'a\', not {}'.format(character)
return s.split(character)
def shopping_list_program():
print('Enter your shopping list below. <item-name> <tick(/) or cross(x)>')
print('To finish, press Enter on an empty line')
shopping_list = get_shopping_list()
items_to_get = get_list_of_items_to_get(shopping_list)
print('You need to get: \n{}'.format('\n'.join(items_to_get)))
def advanced_shopping_list_program():
print('Enter your shopping list below. <item-name> <tick(/) or cross(x)>')
print('To finish, press Enter on an empty line')
shopping_list = get_shopping_list()
split = [split_string(s, ' ') for s in shopping_list.split('\n')]
shopping_dict = turn_shopping_list_to_dict(
[x[0] for x in split],
[x[1] for x in split]
)
ended = False
while any([(not bought) for _, bought in shopping_dict.items()]):
print('Enter an item to check off your list')
item = input()
if item in shopping_dict:
shopping_dict = buy_item(shopping_dict, item)
print('Your need to buy:\n{}\n'.format('\n'.join(get_unbought_items(shopping_dict))))
else:
print('This item is not on your shopping list')
print('You have bought all the items on your list.!')
def test(f, *input_expecteds):
print('testing {}:'.format(f.__name__))
for inputs, expected in input_expecteds:
actual = f(*inputs)
if type(actual) != type(expected):
print('ERROR: {} is meant to return a value of type {}, but it returns a {}'.format(
f.__name__,
type(expected).__name__,
type(actual).__name__,
))
continue
satisfied = False
if type(expected) == list:
satisfied = (set(expected) == set(actual)) and (len(expected) == len(actual))
elif type(expected) == dict:
satisfied = expected == actual
else:
satisfied = expected == actual
if not satisfied:
print(
'ERROR: {}({}) returned {}, when it should return {}'.format(
f.__name__,
','.join([to_arg(arg) for arg in inputs]),
actual,
expected
)
)
print('DONE\n')
def to_arg(arg):
if type(arg) == str:
return '\'{}\''.format(arg).replace('\n', '\\n')
else:
return str(arg)
test(
split_line_into_item_and_mark,
(['oranges /'], ['oranges', '/']),
(['apples x'], ['apples', 'x'])
)
test(
mark_to_bool,
(['x'], False),
(['/'], True),
)
test(
filter_out_ticked_items,
([['oranges'], ['/']], []),
([['oranges', 'apples'], ['/', 'x']], ['apples']),
([['a', 'b', 'c', 'd'], ['/', 'x', '/', 'x']], ['b', 'd']),
)
test(
get_list_of_items_to_get,
(['food x'], ['food']),
(['food /'], []),
(['food /\nthings x\nstuff x'], ['things', 'stuff']),
)
print('EXTENSION FUNCTIONS:')
test(
turn_shopping_list_to_dict,
([['orange'], ['x']], {'orange': False}),
([['orange', 'apple'], ['x', '/']], {'orange': False, 'apple': True}),
)
test(
buy_item,
([{'apple': False}, 'apple'], {'apple': True}),
([{'apple': True}, 'apple'], {'apple': True}),
([{'apple': True, 'pears': False}, 'pears'], {'apple': True, 'pears': True})
)
# for dict ordering non-determinism
list_dict = {'apple': False, 'pears': True}
marks = {
True: '/',
False: 'x'
}
expected = '\n'.join(['{} {}'.format(item, marks[v]) for item, v in list_dict.items()])
test(
make_shopping_list_string,
([{'apple': False}], 'apple x'),
([list_dict], expected)
)
test(
get_unbought_items,
([{'apple': False}], ['apple']),
([{'apple': False, 'pears': True}], ['apple']),
(
[{'pears': True, '£50-steam-voucher': False, 'apple': False,}],
['apple', '£50-steam-voucher'])
)
import code # NOQA
code.interact(local=locals())