Passed
Push — master ( 8edc1f...ae5669 )
by Ken M.
01:14
created

count_inversions   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A count_inversion() 0 8 1
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