I would like to devise an algorithm that generates all combinations of five consecutive elements from a list of integers. For example:
Set -> [1, 2, 3, 4, 5, 6, 7]
Subset 1 -> [1, 2, 3, 4, 5]
Subset 2 -> [2, 3, 4, 5, 6]
Subset 3 -> [3, 4, 5, 6, 7]
I would like to devise an algorithm that generates all combinations of five consecutive elements from a list of integers. For example:
Set -> [1, 2, 3, 4, 5, 6, 7]
Subset 1 -> [1, 2, 3, 4, 5]
Subset 2 -> [2, 3, 4, 5, 6]
Subset 3 -> [3, 4, 5, 6, 7]
Implementation in Python using list slicing:
lst = [1,2,3,4,5,6,7,8,9,10,11,12]
size = 5
num_iterations = len(lst) - size + 1
for i in range(0, num_iterations):
print(lst[i:size + i])