Created
September 18, 2017 07:44
-
-
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].
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
""" 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