-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz4-9.py
63 lines (58 loc) · 3 KB
/
quiz4-9.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
# ----------------
# User Instructions
#
# Write two functions, path_states and path_actions. Each of these
# functions should take a path as input. Remember that a path is a
# list of [state, action, state, action, ... ]
#
# path_states should return a list of the states. in a path, and
# path_actions should return a list of the actions.
def path_states(path):
"Return a list of states in this path."
return [node for node in path if not node[2] in ['->','<-']]
def path_actions(path):
"Return a list of actions in this path."
return [node for node in path if node[2] in ['->','<-']]
def test():
testpath = [(frozenset([1, 10]), frozenset(['light', 2, 5]), 5), # state 1
(5, 2, '->'), # action 1
(frozenset([10, 5]), frozenset([1, 2, 'light']), 2), # state 2
(2, 1, '->'), # action 2
(frozenset([1, 2, 10]), frozenset(['light', 5]), 5),
(5, 5, '->'),
(frozenset([1, 2]), frozenset(['light', 10, 5]), 10),
(5, 10, '->'),
(frozenset([1, 10, 5]), frozenset(['light', 2]), 2),
(2, 2, '->'),
(frozenset([2, 5]), frozenset([1, 10, 'light']), 10),
(10, 1, '->'),
(frozenset([1, 2, 5]), frozenset(['light', 10]), 10),
(10, 10, '->'),
(frozenset([1, 5]), frozenset(['light', 2, 10]), 10),
(10, 2, '->'),
(frozenset([2, 10]), frozenset([1, 5, 'light']), 5),
(5, 1, '->'),
(frozenset([2, 10, 5]), frozenset([1, 'light']), 1),
(1, 1, '->')]
assert path_states(testpath) == [(frozenset([1, 10]), frozenset(['light', 2, 5]), 5), # state 1
(frozenset([10, 5]), frozenset([1, 2, 'light']), 2), # state 2
(frozenset([1, 2, 10]), frozenset(['light', 5]), 5),
(frozenset([1, 2]), frozenset(['light', 10, 5]), 10),
(frozenset([1, 10, 5]), frozenset(['light', 2]), 2),
(frozenset([2, 5]), frozenset([1, 10, 'light']), 10),
(frozenset([1, 2, 5]), frozenset(['light', 10]), 10),
(frozenset([1, 5]), frozenset(['light', 2, 10]), 10),
(frozenset([2, 10]), frozenset([1, 5, 'light']), 5),
(frozenset([2, 10, 5]), frozenset([1, 'light']), 1)]
assert path_actions(testpath) == [(5, 2, '->'), # action 1
(2, 1, '->'), # action 2
(5, 5, '->'),
(5, 10, '->'),
(2, 2, '->'),
(10, 1, '->'),
(10, 10, '->'),
(10, 2, '->'),
(5, 1, '->'),
(1, 1, '->')]
return 'tests pass'
print test()