Created
November 25, 2019 18:19
-
-
Save hsauers5/a4941528403cc81e47631c24d5982e1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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