Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Count Inversions by MartynaDziubalka
def count_inversion(sequence):
result = 0
list = []
for i in sequence:
list.append(i)
for x in range(0, len(list)):
for y in range((x + 1), len(list)):
if list[x] > list[y]:
a = list[x]
b = list[y]
list[x] = b
list[y] = a
result += 1
return result
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert count_inversion((1, 2, 5, 3, 4, 7, 6)) == 3, "Example"
assert count_inversion((0, 1, 2, 3)) == 0, "Sorted"
assert count_inversion((99, -99)) == 1, "Two numbers"
assert count_inversion((5, 3, 2, 1, 0)) == 10, "Reversed"
Jan. 27, 2016