Skip to content

Instantly share code, notes, and snippets.

@t-mart
Created February 5, 2015 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-mart/4674128bda3b56427a8c to your computer and use it in GitHub Desktop.
Save t-mart/4674128bda3b56427a8c to your computer and use it in GitHub Desktop.
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