Skip to content

Instantly share code, notes, and snippets.

@hsauers5
Created November 25, 2019 18:19
Show Gist options
  • Save hsauers5/a4941528403cc81e47631c24d5982e1c to your computer and use it in GitHub Desktop.
Save hsauers5/a4941528403cc81e47631c24d5982e1c to your computer and use it in GitHub Desktop.
def make_permutation(n=4):
result = []
for i in range(1, n+1):
result.append(i)
return result
def get_all_pairs(permutation=make_permutation()):
pairs = []
for i in range(0, len(permutation)):
for j in range(0, len(permutation)):
if i == j:
continue
else:
pairs.append([permutation[i], permutation[j]])
return pairs
def count_inversions(pairs=get_all_pairs()):
count = 0
for pair in pairs:
if pair[0] > pair[1]:
count += 1
return count
permutation = make_permutation(n=4)
all_pairs = get_all_pairs(permutation)
inversions_count = count_inversions(all_pairs)
print(inversions_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment