Sliding String Windows

When you need to analyze a string piece by piece, it is useful to take consecutive windows of the same length. The function slices receives a digit string series and returns all substrings of length length, moving from left to right one character at a time. If the parameters are invalid, raise ValueError.

Примеры

Ввод
("01234", 1)
Вывод
["0", "1", "2", "3", "4"]
Ввод
("97867564", 2)
Вывод
["97", "78", "86", "67", "75", "56", "64"]
Ввод
("12345", 5)
Вывод
["12345"]

Нужно реализовать

def slices(series: str, length: int) -> list[str]:

Теги

stringssliding-windowlists
PostgreSQLv16

Čia bus kodo vykdymo rezultatas

Visible tests
1
Input
"01234", 1
Expected["0","1","2","3","4"]
2
Input
"97867564", 2
Expected["97","78","86","67","75","56","64"]
3
Input
"12345", 5
Expected["12345"]
4
Input
"918493904243", 5
Expected["91849","18493","84939","49390","93904","39042","90424","04243"]
5
Input
"918493904243", 1
Expected["9","1","8","4","9","3","9","0","4","2","4","3"]
6
Input
"01234!", 1
Expected["0","1","2","3","4","!"]
Focus radio
Paused · SomaFM · Fluid