Created
July 12, 2018 16:23
-
-
Save aleglez22/011d22478e7d79f9c3b897c4e1732f1e to your computer and use it in GitHub Desktop.
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
def recurs_flatten(lista): | |
result = [] | |
for item in lista: | |
if type(item) == type([]): | |
result.extend(recurs_flatten(item)) | |
else: | |
result.append(item) | |
return result | |
# TESTS | |
def item(depth, value): | |
if depth <= 1: | |
return [value] | |
else: | |
return [item(depth - 1, value)] | |
def test_list(n): | |
lista = [] | |
for i in range(n): | |
if i == 0: | |
lista.append(i) | |
else: | |
lista.append(item(i, i)) | |
return lista | |
generated_nested_list_a=test_list(9) | |
expected_flatten_list_a= [0,1,2,3,4,5,6,7,8] | |
print("test passed:"+ str(expected_flatten_list_a == recurs_flatten(generated_nested_list_a))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment