Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created June 29, 2017 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgopalan/3e5823b7491e3ce67af76e2b6276441f to your computer and use it in GitHub Desktop.
Save cgopalan/3e5823b7491e3ce67af76e2b6276441f to your computer and use it in GitHub Desktop.
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