## L219
## Albert Puente Encinas

from cdi import *

'''
Let X stand for a set/list/tuple/range with n = len(X) > 1. 
Let W be a list of n positive numbers. 
Define a function select(X,W) that returns an element of X with probability 
proportional to the corresponding weight.

'''

def select(X, W):
    # No need to normalize weights with this random float
    e = rd_float(0,sum(W))  
    l = 0                   # Set initial acumulation to zero
    for i in range(len(X)): # For each element, index i
        l += W[i]           # Acumulate the weight
        if (e <= l): return X[i] # If the random is in the "zone", pick it

