Skip to content

Instantly share code, notes, and snippets.

@arunmlvtec
Created September 18, 2017 07:44
Show Gist options
  • Save arunmlvtec/225807fb29e10b41e250961f20677fb7 to your computer and use it in GitHub Desktop.
Save arunmlvtec/225807fb29e10b41e250961f20677fb7 to your computer and use it in GitHub Desktop.
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
""" Function takes an array of arbitrarily nested arrays of integers and flattens it into a 1 dimensional array. """
def flatten(inpArr):
outputArr = []
for element in inpArr:
if type(element) is int:
outputArr.append(element)
else:
outputArr += flatten(element)
return outputArr
print(flatten([1,2]))
print(flatten([1,[2,[3,4]]]))
print(flatten([[1,2,[3],4],5]))
print(flatten([[1,2,3,4],[[5,6,[[7]]],[8,9,10]],11,[[[12,13,14],[15,16]]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment