fork download
  1. #
  2. from itertools import tee, islice, groupby
  3.  
  4. def inversions(it):
  5. a, b = tee(it)
  6. return sum(1 for _ in islice(
  7. groupby(x >= y for x, y in zip(a, islice(b, 1, None))), 1, None
  8. ))
  9.  
  10. print( *map(inversions, ((1, 2, 5, 3, 4, 7, 6),
  11. (0, 1, 2, 3),
  12. (4, 3, 2, 1, 0, 1)
  13. )),
  14. sep='\n'
  15. )
  16.  
Success #stdin #stdout 0.02s 8688KB
stdin
Standard input is empty
stdout
3
0
1