Skip to content

Instantly share code, notes, and snippets.

@aleglez22
Created July 12, 2018 16:23
Show Gist options
  • Save aleglez22/011d22478e7d79f9c3b897c4e1732f1e to your computer and use it in GitHub Desktop.
Save aleglez22/011d22478e7d79f9c3b897c4e1732f1e to your computer and use it in GitHub Desktop.
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