Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Count Inversions by bartorbard
def count_inversion(sequence):
sequence = list(sequence)
array = sequence
last = len(array)-1
n = 0
idx = 0
tmp=0
counting = 0
while n array[idx+1]:
tmp=array[idx]
array[idx]=array[idx+1]
array[idx+1]=tmp
n=0
idx=0
counting = counting+1
else:
idx=idx+1
n=n+1
return (counting)
"""
Count inversions in a sequence of numbers
"""
return 0
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"
Nov. 28, 2016