Array inversion indicates how far the array is from being sorted.

Inversions are pairs of elements in array that are out of order.

## Examples

```
[1, 2, 3, 4]  =>  0 inversions
[1, 3, 2, 4]  =>  1 inversion: 2 and 3
[4, 1, 2, 3]  =>  3 inversions: 4 and 1, 4 and 2, 4 and 3
[4, 3, 2, 1]  =>  6 inversions: 4 and 3, 4 and 2, 4 and 1, 3 and 2, 3 and 1, 2 and 1
```

## Goal

The goal is to come up with a function that can calculate inversions for any arbitrary array

Source: codewars | Difficulty: EASY
Tags: Data structures

Starter code: def count_inversions(array):
	

Solutions:
def count_inversions(array):
	inv_count = 0
	for i in range(len(array)):
		for j in range(i, len(array)):
			if array[i] > array[j]:
				inv_count += 1
	return inv_count


def count_inversions(array):
	return sum((x > y for (i, x) in enumerate(array) for y in array[i + 1:]))


count_inversions = lambda arr: sum((sum((k > j for k in arr[:i])) for (i, j) in enumerate(arr)))


def count_inversions(array):
	n = len(array)
	return sum([1 for i in range(n) for j in range(n) if i < j and array[i] > array[j]])


def count_inversions(lst):
	return sum((1 for (i, m) in enumerate(lst, 1) for n in lst[i:] if n < m))


from itertools import islice

def count_inversions(array):
	return sum((sum((x > y for y in islice(array, i + 1, None))) for (i, x) in enumerate(array)))


def count_inversions(collection):
	lenghts = len(collection)
	swapped = False
	swapped_counter = 0
	for i in range(lenghts - 1):
		for j in range(lenghts - i - 1):
			if collection[j] > collection[j + 1]:
				(collection[j], collection[j + 1]) = (collection[j + 1], collection[j])
				swapped = True
				swapped_counter += 1
		if not swapped:
			break
	return swapped_counter


fun1 = lambda a1, a2: sum([1 if a1 > i else 0 for i in a2])

def count_inversions(array):
	s = 0
	for i in range(len(array)):
		if i != len(array) - 1:
			s += fun1(array[i], array[i + 1:])
	return s