-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw5-1.py
67 lines (56 loc) · 1.97 KB
/
hw5-1.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
# -----------------
# User Instructions
#
# In this problem, you will use a faster version of Pwin, which we will call
# Pwin2, that takes a state as input but ignores whether it is player 1 or
# player 2 who starts. This will reduce the number of computations to about
# half. You will define a function, Pwin3, which will be called by Pwin2.
#
# Pwin3 will only take me, you, and pending as input and will return the
# probability of winning.
#
# Keep in mind that the probability that I win from a position is always
# (1 - probability that my opponent wins).
from functools import update_wrapper
def decorator(d):
"Make function d a decorator: d wraps a function fn."
def _d(fn):
return update_wrapper(d(fn), fn)
update_wrapper(_d, d)
return _d
@decorator
def memo(f):
"""Decorator that caches the return value for each call to f(args).
Then when called again with same args, we can just look it up."""
cache = {}
def _f(*args):
try:
return cache[args]
except KeyError:
cache[args] = result = f(*args)
return result
except TypeError:
# some element of args refuses to be a dict key
return f(args)
_f.cache = cache
return _f
goal = 40
def Pwin2(state):
"""The utility of a state; here just the probability that an optimal player
whose turn it is to move can win from the current state."""
_, me, you, pending = state
return Pwin3(me, you, pending)
@memo
def Pwin3(me, you, pending):
## your code here
def test():
epsilon = 0.0001 # used to make sure that floating point errors don't cause test() to fail
assert goal == 40
assert len(Pwin3.cache) <= 50000
assert Pwin2((0, 42, 25, 0)) == 1
assert Pwin2((1, 12, 43, 0)) == 0
assert Pwin2((0, 34, 42, 1)) == 0
assert abs(Pwin2((0, 25, 32, 8)) - 0.736357188272) <= epsilon
assert abs(Pwin2((0, 19, 35, 4)) - 0.493173612834) <= epsilon
return 'tests pass'
print test()