fork download
  1. import itertools
  2.  
  3.  
  4. def functional(a):
  5. return itertools.chain(
  6. itertools.chain.from_iterable(zip(a[1::2], a[::2])),
  7. [a[-1]] if len(a) % 2 else []
  8. )
  9.  
  10.  
  11. def gener(it):
  12. while True:
  13. try:
  14. a = next(it)
  15. except StopIteration:
  16. break
  17. try:
  18. b = next(it)
  19. except StopIteration:
  20. yield a
  21. break
  22. yield b
  23. yield a
  24.  
  25.  
  26. for upper in (10, 11):
  27. a = list(range(1, upper))
  28.  
  29. print(list(functional(a)))
  30. print(list(gener(iter(a))))
  31.  
Success #stdin #stdout 0.04s 9352KB
stdin
Standard input is empty
stdout
[2, 1, 4, 3, 6, 5, 8, 7, 9]
[2, 1, 4, 3, 6, 5, 8, 7, 9]
[2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
[2, 1, 4, 3, 6, 5, 8, 7, 10, 9]