So, I have a list of groups
[['a', 'b'], ['c', 'd', 'e'], ['f']]
and I need to shuffle a flattened version of this list
[a, b, c, d, e, f]
so that elements of the same group would end at some distance from each other. E. g.
[a, c, b, d, f, e] and not [a, c, b, d, e, f], because d and e are in the same group.
I don’t care if the distance is just one element or more, but any element must not be near another element from it’s group. Is there any algorithm for this?
The algorithm also needs to tell if this cannot be done.
This code makes a copy of your original list of groups and takes each time a random element from the (at each moment) largest remaining group. Not quite randomized, but should work for any case when there is no group with over the half of all elements.