Skip to content

Instantly share code, notes, and snippets.

@computerex
Last active January 28, 2019 03:40
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 computerex/493eed0c43ad6f42349449dfe3338820 to your computer and use it in GitHub Desktop.
Save computerex/493eed0c43ad6f42349449dfe3338820 to your computer and use it in GitHub Desktop.
input = [[1,2,[3]],4, [5], [[[[6],7], 8]]]
def flatten(input, flattened):
for e in input:
if type(e) == list:
flatten(e, flattened)
else:
flattened.append(e)
return flattened
print("input: " + str(input))
output = flatten(input, [])
print("output: " + str(output))
fail = False
for e in output:
if type(e) == list:
print("fail: array not flattened")
fail = True
break
if fail:
print("Testing failed")
else:
print("All tests passed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment