Skip to content

Instantly share code, notes, and snippets.

@lurym
Created July 31, 2017 16:06
Show Gist options
  • Save lurym/8b1164da90bdec1f0a5c0060cbe22088 to your computer and use it in GitHub Desktop.
Save lurym/8b1164da90bdec1f0a5c0060cbe22088 to your computer and use it in GitHub Desktop.
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