Created
August 19, 2016 18:41
-
-
Save Hygens/f6e9ab64f5d2f0699013a71e6a52a8c8 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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].