-
-
Save t-mart/4674128bda3b56427a8c 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
from operator import mul | |
from functools import reduce | |
from itertools import permutations | |
def factorial(n): | |
return reduce(mul, range(1,n+1), 1) | |
def word_index(word): | |
index = 1 | |
alpha = sorted(word) | |
for letter in word: | |
order = alpha.index(letter) | |
order1 = sorted(list(set(alpha))).index(letter) | |
del alpha[order] | |
left = len(alpha) | |
repeats = left - len(set(alpha)) | |
index += (factorial(left)/factorial(repeats)) * order1 | |
return index | |
def index_choices(iterable): | |
s = sorted(iterable) | |
l = [] | |
for i in iterable: | |
index = s.index(i) | |
l.append(index) | |
del s[index] | |
return l | |
if __name__ == '__main__': | |
words = 'abab aaab baaa question bookkeeper nonintuitiveness'.split() | |
expected = [2, 1, 4, 24572, 10743, 8222334634] | |
# words = permutations('a b c d'.split()) | |
# expected = range(reduce(mul, range(2,5), 1)) | |
for word, exp in zip(words, expected): | |
act = word_index(word) | |
print('{word} exp:{exp} act:{act} {result}'.format(word=word, exp=exp, | |
act=act, result='PASS' if exp == act else 'FAIL')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment