Created
January 3, 2018 07:11
-
-
Save yovany-lg/7daeccc222af2b138fa1b379ad7c4399 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 hidden or 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
array = [[1,2,[3]],4] | |
def arrayFlat(arr, newArray = []): | |
"""Function that receives an array of integers, there can be nested array | |
elements, and returns the flattened version of the given aray""" | |
for el in arr: | |
if not isinstance(el, list): | |
newArray.append(el) | |
else: | |
arrayFlat(el, newArray) | |
return newArray | |
if __name__ == '__main__': | |
print arrayFlat(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment