Basic numeric list
Input
sliding_windows([1, 2, 3, 4], 2)
Output
[[1, 2], [2, 3], [3, 4]]
All contiguous windows of size 2 with default step 1.
Full lesson preview
Create contiguous sliding windows from any sequence with a configurable step.
Problem statement
Task
Examples
Input
sliding_windows([1, 2, 3, 4], 2)
Output
[[1, 2], [2, 3], [3, 4]]
All contiguous windows of size 2 with default step 1.
Input
sliding_windows([1,2,3,4,5], 3, 2)
Output
[[1, 2, 3], [3, 4, 5]]
Windows start at indices 0 and 2 because step=2.
Input
sliding_windows('abcdef', 3)
Output
['abc', 'bcd', 'cde', 'def']
Works on strings and returns substrings.
Input format
Output format
Constraints
Samples
Input
sliding_windows([1,2,3], 3)
Output
[[1, 2, 3]]
Window size equal to sequence length returns single window.