So maybe a better way of explaining, I wish to jitter a repeating list of 4 indexes, in such way that the last item of each branch is different from the first item of the next branch.
import random
def shuffle_without_consecutive_repeats(lst):
"""Shuffles the input list ensuring no consecutive repetitions"""
while True:
random.shuffle(lst)
if all(lst[i] != lst[i + 1] for i in range(len(lst) - 1)):
return lst
def generate_partitioned_lists(num_partitions=4, num_indexes=4):
"""Generates a partitioned list of randomized indexes without consecutive repetition"""
partitions = []
last_partition = []
for _ in range(num_partitions):
new_partition = shuffle_without_consecutive_repeats(list(range(num_indexes)))
# Ensure no consecutive repeat between partitions
while last_partition and new_partition[0] == last_partition[-1]:
new_partition = shuffle_without_consecutive_repeats(list(range(num_indexes)))
partitions.append(new_partition)
last_partition = new_partition
return partitions
random.seed(42)
partitions = generate_partitioned_lists()
for partition in partitions:
print(partition)
You can integrate it in your GH
Hope this helps,
Farouk
I just want to say how much I appreciate this community. You guys are always up for a challenge and consistently blow me away with your elegance of thought. Every time I ask a question I learn so much more than I expected.