-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz4-17.py
39 lines (34 loc) · 1.16 KB
/
quiz4-17.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
# -----------------
# User Instructions
#
# Write a function, path_cost, which takes a path as input
# and returns the total cost associated with that path.
# Remember that paths will obey the convention
# path = [state, (action, total_cost), state, ...]
#
# If a path is less than length 3, your function should
# return a cost of 0.
def path_cost(path):
"""The total cost of a path (which is stored in a tuple
with the final action."""
# path = [state, (action, total_cost), state, ... ]
if len(path) < 3:
return path[1][1]
else:
result = path[1::2]
result = reversed(result)
return result.next()[1]
def bcost(action):
"""Returns the cost (a number) of an action in the
bridge problem."""
# An action is an (a, b, arrow) tuple; a and b are
# times; arrow is a string.
a, b, arrow = action
return max(a,b)
def test():
assert path_cost(('fake_state1', ((2, 5, '->'), 5), 'fake_state2')) == 5
assert path_cost(('fs1', ((2, 1, '->'), 2), 'fs2', ((3, 4, '<-'), 6), 'fs3')) == 6
assert bcost((4, 2, '->'),) == 4
assert bcost((3, 10, '<-'),) == 10
return 'tests pass'
print test()