Factorize len(L)
into chunks of 2s and 3s and then use a loop to divide the list.
import numpy as np
def rand23():
return np.random.randint(2,4)
def get_chunk(sz):
rem = sz
ch = []
while ( rem > 0 ):
if ( rem <= 3 ): #if <= 3 add what is left in the chunk (exit condition)
ch.append(rem)
rem = 0
break
elif ( rem == 4 ): #4 is the only edge case here
ch.append(2)
ch.append(2)
rem = 0
break
else:
ch.append(rand23())
rem -= ch[-1]
return ch
L = [1,1,1,1,1,1,1,1,1,1,1,1]
ch = get_chunk(len(L))
L2 = []
count = 0
#Divide the list into chunks based on ch
for c in ch:
L2.append(tuple(L[count:count+c]))
count += c
print L2
Results:( each line is output of an iteration )
[(1, 1, 1), (1, 1), (1, 1), (1, 1), (1, 1, 1)]
[(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]
[(1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1)]
[(1, 1), (1, 1), (1, 1, 1), (1, 1, 1), (1, 1)]
[(1, 1, 1), (1, 1), (1, 1), (1, 1), (1, 1, 1)]
[(1, 1, 1), (1, 1, 1), (1, 1), (1, 1), (1, 1)]
[(1, 1), (1, 1), (1, 1, 1), (1, 1), (1, 1, 1)]
PS : You can also implement get_chunk()
recursively.