Skip to content

Instantly share code, notes, and snippets.

@yovany-lg
Created January 3, 2018 07:11
Show Gist options
  • Save yovany-lg/7daeccc222af2b138fa1b379ad7c4399 to your computer and use it in GitHub Desktop.
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].
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