Created
July 31, 2017 16:06
-
-
Save lurym/8b1164da90bdec1f0a5c0060cbe22088 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
def flatten(arg): | |
if isinstance(arg, list): | |
if len(arg) == 0: | |
return arg | |
else: | |
return flatten(arg[0]) + flatten(arg[1:]) | |
else: | |
return [arg] | |
import unittest | |
class MyTest(unittest.TestCase): | |
def testBasic(self): | |
self.assertEqual(flatten([[1,2,[3]],4]), [1,2,3,4]) | |
def testArrMatrix(self): | |
self.assertEqual(flatten([[1,2,3],[4,5,6],[7,8,9]]), [1,2,3,4,5,6,7,8,9]) | |
def testEmptyList(self): | |
self.assertEqual(flatten([]), []) | |
def testSingleElement(self): | |
self.assertEqual(flatten([1]), [1]) | |
def testDeepArrayComposition(self): | |
self.assertEqual(flatten([[[[[4]]]]]), [4]) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment