fork download
  1. def count_inversion(seq):
  2. i = 1
  3. s = 0
  4.  
  5. while i < len(seq):
  6. j = i - 1
  7. while seq[i] < seq[j]:
  8. s += 1
  9. i += 1
  10. if i == len(seq):
  11. return s
  12. i += 1
  13.  
  14. return s
  15.  
  16.  
  17. def test():
  18. seq_0 = (1, 2, 5, 3, 4, 7, 6)
  19. seq_1 = (0, 1, 2, 3, 4)
  20. seq_2 = list(reversed(range(10)))
  21. assert count_inversion(seq_0) == 3
  22. assert count_inversion(seq_1) == 0
  23. assert count_inversion(seq_2) == 9
  24. print('ok')
  25.  
  26.  
  27. test()
  28.  
Success #stdin #stdout 0.02s 8696KB
stdin
Standard input is empty
stdout
ok