-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.py
26 lines (22 loc) · 867 Bytes
/
Utils.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
#-*- coding: utf-8 -*-
import ext.colorama; ext.colorama.init() # this makes termcolor work on windows
from ext.termcolor import colored, cprint
from operator import mul
################################################################################
## Non-program specific utilities and hofs
################################################################################
def product( xs ): return reduce( mul, xs, 1 )
def weightedChoice( cws ):
''':: [(Choice,Weight)] -> Rand Choice'''
from random import random
from bisect import bisect
values, weights = zip(*cws)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
x = random() * total
i = bisect(cum_weights, x)
return values[i]
def debug( txt ): print colored( txt, 'white', 'on_blue' )