Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 60472d3

Browse files
committed
Resolve #106. Add "slices of a series of digits" function
1 parent 357c910 commit 60472d3

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/sequences/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Balanced Parentheses
2626
--------------------
2727
28-
A parentheses is said to be balanced if each left parenthesis has its
28+
A parentheses are said to be balanced if each left parenthesis has its
2929
respective right parenthesis to match its pair in a well-nested format.
3030
3131
.. autofunction:: are_parentheses_balanced
@@ -39,4 +39,4 @@
3939
"get_longest_uniq_length",
4040
]
4141

42-
from .func import *
42+
from sequences.func import *

src/sequences/func.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def get_longest_palindrome(origin: str, /) -> str:
7979

8080
size: int = len(origin)
8181

82+
# noinspection PyMissingOrEmptyDocstring
8283
def expand(left: int, right: int) -> int:
8384
while left >= 0 and right < size and origin[left] == origin[right]:
8485
left -= 1
@@ -233,3 +234,43 @@ def add_spaces(origin: str) -> str:
233234
origin = origin.replace(" ", "")
234235

235236
return "".join(f" {c}" if c.isupper() else c for c in origin).lstrip()
237+
238+
239+
def get_consecutive_slices(origin: str, n: int) -> List[List[int]]:
240+
"""
241+
Return possible slices of string as a collection of lists of digits
242+
243+
:param origin: the input string of digits
244+
:type origin: str
245+
:param n: the length of slices to be extracted
246+
:type n: int
247+
248+
:return: a list containing sublists of consecutive slices of length "n"
249+
:rtype: list
250+
251+
:raise: ValueError
252+
253+
Given a string of digits and an integer `n`, this function returns all
254+
consecutive slices of length `n` from the string.
255+
256+
Usage:
257+
258+
>>> assert get_consecutive_slices("0123", 1) == [[0], [1], [2], [3]]
259+
>>> assert get_consecutive_slices("0123", 2) == [[0, 1], [1, 2], [2, 3]]
260+
>>> assert get_consecutive_slices("0123", 3) == [[0, 1, 2], [1, 2, 3]]
261+
262+
"""
263+
264+
size: int = len(origin)
265+
if size < n:
266+
raise ValueError("slice size is bigger than origin length")
267+
268+
origin = list(map(int, origin))
269+
result = []
270+
idx = 0
271+
272+
while idx <= size - n:
273+
result.append(origin[idx:idx + n])
274+
idx += 1
275+
276+
return result

tests/sequences/func_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
import sequences
24

35

@@ -77,3 +79,19 @@ def test_add_space():
7779
assert sequences.add_spaces("test_test") == "test_test"
7880
assert sequences.add_spaces("JohnDoe") == "John Doe"
7981
assert sequences.add_spaces("John Doe") == "John Doe"
82+
83+
84+
def test_get_consecutive_slices():
85+
assert sequences.get_consecutive_slices("0123", 1) == [[0], [1], [2], [3]]
86+
assert (
87+
sequences.get_consecutive_slices("0123", 2) == [[0, 1], [1, 2], [2, 3]]
88+
)
89+
assert (
90+
sequences.get_consecutive_slices("0123", 3) == [[0, 1, 2], [1, 2, 3]]
91+
)
92+
93+
with pytest.raises(ValueError,
94+
match="slice size is bigger than origin length"):
95+
sequences.get_consecutive_slices("0123", 5)
96+
with pytest.raises(ValueError):
97+
sequences.get_consecutive_slices("abcdefg", 1)

0 commit comments

Comments
 (0)