I\'ve such a problem:
There is a list of elements of class CAnswer
(no need to describe the class), and I need to shuffle it, but with one constraint -
In linear time, constant space using random.shuffle() source:
from random import random
def shuffle_with_freeze(x):
for i in reversed(xrange(1, len(x))):
if x[i].freeze: continue # fixed
# pick an element in x[:i+1] with which to exchange x[i]
j = int(random() * (i+1))
if x[j].freeze: continue #NOTE: it might make it less random
x[i], x[j] = x[j], x[i] # swap