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

Commit 0ebf301

Browse files
committed
Added "flatten" list function
Signed-off-by: Serhii Horodilov <[email protected]>
1 parent b969453 commit 0ebf301

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/datasets/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"get_least_bricks_position",
2222
"get_least_bricks_count",
2323
"filter_by_values",
24+
"flatten",
2425
]
2526

2627
from datasets.func import *

src/datasets/func.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
from typing import Any, Dict, Hashable, List, Optional, Set, Tuple, Union
77

88

9-
# BASIC OPERATIONS
10-
# ================
11-
129
def swap_dict_loop(origin: Dict[Hashable, Hashable]
1310
) -> Dict[Hashable, Hashable]:
1411
"""
@@ -143,9 +140,6 @@ def get_both_top_low_students(students_data: StudentsList
143140
return top_student, low_student # type: ignore
144141

145142

146-
# BRICK WALL CHALLENGE
147-
# ====================
148-
149143
def get_bricks_count(structure: List[List[int]]) -> int:
150144
"""Return number of bricks in the wall structure
151145
@@ -247,9 +241,6 @@ def get_least_bricks_count(structure: List[List[int]]) -> int:
247241
return len(structure) - max_value
248242

249243

250-
# FILTER DATASET CHALLENGE
251-
# ========================
252-
253244
def filter_by_values(origin: List[Dict[str, Hashable]],
254245
keys: Optional[List[str]] = None
255246
) -> List[Dict[str, Any]]:
@@ -314,3 +305,34 @@ def filter_by_values(origin: List[Dict[str, Hashable]],
314305
filtered_dataset.append(entry)
315306

316307
return filtered_dataset
308+
309+
310+
NestedIntList = Union[int, List["NestedIntList"]]
311+
312+
313+
def flatten(origin: List[NestedIntList]) -> List[int]:
314+
"""
315+
Flattens a list of integers and nested lists of integers
316+
317+
:param origin: an original list
318+
319+
:return: a single-level list of integers
320+
321+
Usage:
322+
323+
>>> assert flatten([1, 2, 3]) == [1, 2, 3]
324+
>>> assert flatten(([[1], [2], [3]])) == [1, 2, 3]
325+
>>> assert flatten([1, [2, 3]]) == [1, 2, 3]
326+
>>> assert flatten([1, [[2], [3]]]) == [1, 2, 3]
327+
328+
"""
329+
330+
result: List[int] = []
331+
332+
for item in origin:
333+
if isinstance(item, list):
334+
result.extend(flatten(item))
335+
else:
336+
result.append(item)
337+
338+
return result

tests/datasets/func_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ def test_dataset_filter_no_keys(dataset):
6363
{"x": 1, "y": 1}, {"x": 1, "y": 2}, {"x": 2, "y": 1}, {"x": 2, "y": 2},
6464
]
6565
assert datasets.filter_by_values(dataset) == test_dataset
66+
67+
68+
def test_flatten_list():
69+
assert datasets.flatten([1, 2, 3]) == [1, 2, 3]
70+
assert datasets.flatten(([[1], [2], [3]])) == [1, 2, 3]
71+
assert datasets.flatten([1, [2, 3]]) == [1, 2, 3]
72+
assert datasets.flatten([1, [[2], [3]]]) == [1, 2, 3]

0 commit comments

Comments
 (0)