From a1ba91c1e07756c6ab193e09baec33915bdb73eb Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Mon, 9 Dec 2024 19:38:36 -0800 Subject: [PATCH] Flatten --- kyu_5/flatten/__init__.py | 1 + kyu_5/flatten/flatten.py | 16 +++++++++++----- kyu_5/flatten/test_flatten.py | 9 +++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/kyu_5/flatten/__init__.py b/kyu_5/flatten/__init__.py index e69de29bb2d..4563a1eb870 100644 --- a/kyu_5/flatten/__init__.py +++ b/kyu_5/flatten/__init__.py @@ -0,0 +1 @@ +"""Flatten.""" diff --git a/kyu_5/flatten/flatten.py b/kyu_5/flatten/flatten.py index d97e29e9db7..8dc41a571b4 100644 --- a/kyu_5/flatten/flatten.py +++ b/kyu_5/flatten/flatten.py @@ -1,5 +1,6 @@ """ -Solution for -> flatten() +Solution for -> flatten(). + Created by Egor Kostan. GitHub: https://github.com/ikostan """ @@ -7,6 +8,8 @@ def flatten(*args) -> list: """ + Flatten function. + The method takes in any number of arguments and flattens them into a single array. If any of the arguments passed in are an array then @@ -26,13 +29,16 @@ def flatten(*args) -> list: def unpack(data, collection: list): """ - Helper method. Unpack data until its not list or a tuple. + Unpack helper method. + + Unpack data until its not list or a tuple. :param data: :param collection: :return: """ - if not isinstance(data, list) and not isinstance(data, tuple): - collection.append(data) - else: + if isinstance(data, (list, tuple)): for d in data: unpack(d, collection) + else: + collection.append(data) + diff --git a/kyu_5/flatten/test_flatten.py b/kyu_5/flatten/test_flatten.py index 3334bcc9dff..72c1f712b7d 100644 --- a/kyu_5/flatten/test_flatten.py +++ b/kyu_5/flatten/test_flatten.py @@ -1,5 +1,6 @@ """ -Solution for -> flatten() +Solution for -> flatten(). + Created by Egor Kostan. GitHub: https://github.com/ikostan """ @@ -26,12 +27,12 @@ name='Source/Kata') # pylint: enable-msg=R0801 class FlattenTestCase(unittest.TestCase): - """ - Testing flatten function - """ + """Testing flatten function.""" def test_flatten(self): """ + Testing flatten function with various test data. + For this exercise you will create a global flatten method. The method takes in any number of arguments and flattens them into a single array. If any of the arguments passed in