Skip to content

Instantly share code, notes, and snippets.

@Hygens
Created August 19, 2016 18:41
Show Gist options
  • Save Hygens/f6e9ab64f5d2f0699013a71e6a52a8c8 to your computer and use it in GitHub Desktop.
Save Hygens/f6e9ab64f5d2f0699013a71e6a52a8c8 to your computer and use it in GitHub Desktop.
def main():
from sys import stdin
from ast import literal_eval
def flatten(l):
i = 0
nao_muda = False
while i<len(l):
nao_muda = False
try:
if isinstance(l[i],list):
l.extend(l[i])
del l[i]
nao_muda = True
except TypeError:
pass
if not nao_muda: i+=1
return l
def innerList(l,r=[]):
for e in l:
try:
if isinstance(e,list):
r = flatten(e)
l.remove(e)
return innerList(l,r)
except TypeError:
r.append(e)
continue
l.extend(r)
return sorted(l)
a = literal_eval(stdin.readline())
print(innerList(a))
if __name__=='__main__':
main()
@Hygens
Copy link
Author

Hygens commented Aug 19, 2016

This implementation is for:
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].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment