fork download
  1.  
  2. # -*- coding: utf-8 -*-
  3.  
  4. from itertools import izip, chain
  5.  
  6. A = "A"
  7. B = "B"
  8.  
  9. def flatten(xs):
  10. return chain.from_iterable(xs)
  11.  
  12. def pairwise(xs):
  13. ys = iter(xs)
  14. return izip(ys, ys)
  15.  
  16. def func(sequence):
  17. threshold = sequence[sequence.index(B)-1]
  18. return flatten((value,key) for value,key in pairwise(sequence)
  19. if not (key == A and value < threshold))
  20.  
  21.  
  22. if __name__ == "__main__":
  23.  
  24. dataA = [0,A,9,B]
  25. dataB = [0,A,1,A,3,A,5,B,8,A,13,A,20,A]
  26.  
  27. print list(func(dataA))
  28. print list(func(dataB))
Success #stdin #stdout 0.09s 10928KB
stdin
Standard input is empty
stdout
[9, 'B']
[5, 'B', 8, 'A', 13, 'A', 20, 'A']