-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
52 lines (43 loc) · 1.61 KB
/
utilities.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
# utilities.py - AIPython useful utilities
# AIFCA Python3 code Version 0.8.1 Documentation at http://aipython.org
# Artificial Intelligence: Foundations of Computational Agents
# http://artint.info
# Copyright David L Poole and Alan K Mackworth 2017.
# This work is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# See: http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en
import random
def argmax(gen):
"""gen is a generator of (element,value) pairs, where value is a real.
argmax returns an element with maximal value.
If there are multiple elements with the max value, one is returned at random.
"""
maxv = float('-Infinity') # negative infinity
maxvals = [] # list of maximal elements
for (e,v) in gen:
if v>maxv:
maxvals,maxv = [e], v
elif v==maxv:
maxvals.append(e)
return random.choice(maxvals)
# Try:
# argmax(enumerate([1,6,3,77,3,55,23]))
def flip(prob):
"""return true with probability prob"""
return random.random() < prob
def dict_union(d1,d2):
"""returns a dictionary that contains the keys of d1 and d2.
The value for each key that is in d2 is the value from d2,
otherwise it is the value from d1.
This does not have side effects.
"""
d = dict(d1) # copy d1
d.update(d2)
return d
def test():
"""Test part of utilities"""
assert argmax(enumerate([1,6,55,3,55,23])) in [2,4]
assert dict_union({1:4, 2:5, 3:4},{5:7, 2:9}) == {1:4, 2:9, 3:4, 5:7}
print("Passed unit test in utilities")
if __name__ == "__main__":
test()