Участник:F.Nikitin/RandomPickWithBlacklist

Материал из DISCOPAL
Перейти к: навигация, поиск
import random
 
 
class Solution:
    def __init__(self, N, blacklist):
        self.n = N - len(blacklist)
        self.b2w = {}
        white = iter(set(range(self.n, N)) - set(blacklist))
        for black in blacklist:
            if black < self.n:
                self.b2w[black] = next(white)
 
 
    def pick(self):
        index = random.randint(0, self.n-1)
        return self.b2w[index] if index in self.b2w else index