fork download
  1. # your code goes here
  2. lst = (1, (2, 3), (4, 5), ((6, 7)), (8))
  3. def gen(lst):
  4. for l in lst:
  5. (yield from gen(l)) if isinstance(l, tuple) else (yield l)
  6.  
  7. def serial(lst):
  8. prev = 0
  9. for i in gen(lst):
  10. yield i, prev
  11. prev = i
  12.  
  13. print(list(serial(lst)))
  14.  
  15. def parse(l, *rest, prev=0):
  16. ret = parse(*l, prev=prev) if isinstance(l, tuple) else [(l, prev)]
  17. ret += parse(*rest, prev=ret[-1][0]) if rest else []
  18. return ret
  19.  
  20. #return ([i for j in l for i, last in recurse(j)], last) if isinstance(l, tuple) else [l], last
  21. print(parse(lst))
Success #stdin #stdout 0.02s 9164KB
stdin
Standard input is empty
stdout
[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6), (8, 7)]
[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6), (8, 7)]