| Total Complexity | 1 |
| Total Lines | 20 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | def count_inversion(sequence): |
||
| 2 | # Count inversions in a sequence of numbers. |
||
| 3 | return sum( |
||
| 4 | [ |
||
| 5 | 1 |
||
| 6 | for i in range(len(sequence) - 1, -1, -1) |
||
| 7 | for j in range(i - 1, -1, -1) |
||
| 8 | if sequence[i] < sequence[j] |
||
| 9 | ] |
||
| 10 | ) |
||
| 11 | |||
| 12 | |||
| 13 | if __name__ == '__main__': |
||
| 14 | # These "asserts" using only for self-checking and not necessary for |
||
| 15 | # auto-testing |
||
| 16 | assert count_inversion((1, 2, 5, 3, 4, 7, 6)) == 3, "Example" |
||
| 17 | assert count_inversion((0, 1, 2, 3)) == 0, "Sorted" |
||
| 18 | assert count_inversion((99, -99)) == 1, "Two numbers" |
||
| 19 | assert count_inversion((5, 3, 2, 1, 0)) == 10, "Reversed" |
||
| 20 |