samedi 11 juillet 2020

Creating a list without back-to-back repetitions from multiple repeating elements

I am trying to create a list from multiple repeating elements (type does not really matter as long as they are all the same) in which a given element does not repeat itself back-to-back. Or put differently, there should be no repetitions from index n to index n+1 in the resulting list.

More specifically, I have for example...

shape_typesA = [1, 1, 2, 2]
shape_typesB = [2, 2, 1, 3]

...and I would like to randomly combine them in a list like so:

shape_typesALL = [2, 1, 2, 1, 2, 1, 2, 3]

Each element of each original list (i.e. shape_typesA / shape_typesB) is only allowed to appear once in the resulting list (i.e. shape_typesALL). Lists have typically the same length. If there is no solution, an error should be raised.

What is the best solution for this problem?




Aucun commentaire:

Enregistrer un commentaire