Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Explained solution in Clear category for Count Inversions by Selindian
def count_inversion(sequence:tuple) -> int:
inversions = 0
for idx, val in enumerate(sequence):
# Add the length of a list that only includes all smaller elements coming after the current one
inversions += len([num for num in sequence[idx:] if num < val])
return inversions
if __name__ == '__main__':
print("Example:");
print(count_inversion([1, 2, 5, 3, 4, 7, 6]));
#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"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Jan. 5, 2023
Comments: