Last active
May 13, 2016 00:45
-
-
Save Randy1Burrell/8ea66a5affd71f817035b03b7d23d35d to your computer and use it in GitHub Desktop.
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
#Python | |
import numbers | |
import types | |
def arrayFlatten(A = [], b = []): | |
'''This function take one or two array and recursively | |
flattens array A''' | |
#Based case | |
#Making sure the size of A is greater than 0 | |
if(len(A) < 1): | |
return A | |
else: | |
#Using a loop to go through A's contents 1 by 1 | |
b = [] | |
for i in A: | |
#Making sure i is a number and not a list | |
if (isinstance(i, numbers.Number) or type(i) is str): | |
b.append(i) | |
#if i is not a number then recursively call this function to flatten | |
#i array also | |
else: | |
b.extend(arrayFlatten(i, b)) | |
#return the output to the caller | |
return b | |
#Testing the array | |
print arrayFlatten([[1,2,[3]],4, [6,6,[2,77]],{9:9, 6: 0.9, 'd':5}, [[1,2,[3]],4]]) | |
#As you can see this function flattens the array regardless of type | |
#Output: | |
#[1, 2, 3, 4, 6, 6, 2, 77, 9, 'd', 6, 1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment