-
Notifications
You must be signed in to change notification settings - Fork 2
/
tribute_finder.py
55 lines (41 loc) · 1.78 KB
/
tribute_finder.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
"""Pick a tribute to run a meeting or whatever."""
import random
from collections import Counter
def pick_tribute(population, previous_tributes, seed=None):
"""Pick a tribute. May the odds be ever in your favor.
Parameters
----------
population : list
List of all the choices to choose from.
previous_tributes : list
List of previous tributes to be considered.
Duplicates are allowed. In fact, the more duplicates are detected
for a choice, the lower the chance that choice is picked.
seed : int, optional
Random seed for reproducibility.
Returns
-------
tribute
Selected tribute from ``population``.
"""
population = list(set(population)) # Ensure uniqueness
c = Counter(previous_tributes)
max_weight = len(population)
if max_weight == 0:
raise ValueError('Empty population')
weights = [max_weight if c[name] == 0 else 1 / c[name]
for name in population]
if seed is not None:
random.seed(seed)
# NOTE: k > 1 can return duplicate, so just return one
return random.choices(population, weights=weights)[0]
def test_pick_tribute():
population = ['Robb Stark', 'Jon Snow', 'Sansa Stark', 'Arya Stark',
'Bran Stark', 'Rickon Stark', 'Theon Greyjoy']
# Arya Stark should be the least likely, followed by Robb Stark,
# then Jon/Theon. Dolores left. The rest is fair game.
previous_tributes = ['Robb Stark', 'Jon Snow', 'Robb Stark',
'Arya Stark', 'Theon Greyjoy', 'Arya Stark',
'Arya Stark', 'Dolores']
assert pick_tribute(population, previous_tributes, seed=1234) == 'Sansa Stark' # noqa
assert pick_tribute(population, previous_tributes, seed=4321) == 'Bran Stark' # noqa