Created
June 29, 2017 17:57
-
-
Save cgopalan/3e5823b7491e3ce67af76e2b6276441f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def flatten(arg_list, result_list): | |
""" Flatten an array of arbitrarily nested arrays of | |
integers into a flat array of integers. """ | |
for x in arg_list: | |
if isinstance(x, list): | |
flatten(x, result_list) | |
else: | |
result_list.append(x) | |
return result_list | |
# Tests | |
class TestFlatten(unittest.TestCase): | |
def test_blank_list(self): | |
self.assertTrue(flatten([], []) == []) | |
def test_same_list(self): | |
self.assertTrue(flatten([1,2,3,4], []) == [1,2,3,4]) | |
def test_single_nested_list(self): | |
self.assertTrue(flatten([1,2,[3],4], []) == [1,2,3,4]) | |
def test_multiple_nested_list(self): | |
self.assertTrue(flatten([1,2,[3],[4]], []) == [1,2,3,4]) | |
def test_complex_nested_list(self): | |
self.assertTrue(flatten([[1,2,[3]],4], []) == [1,2,3,4]) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment