Last active
August 29, 2015 14:16
-
-
Save IanWitham/b25e2aa49db78f044183 to your computer and use it in GitHub Desktop.
Alternative method for creating anagram keys. In practice seems to be faster to use something like ''.join(sorted(word.upper()))
This file contains 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 | |
# indices 0 - 64 are non-word characters | |
# followed by indices corrolating to capital letters | |
# followed by 6 non-word characters | |
# followed by indices corrolating to lower-case letters | |
primes = [1] * 65 \ | |
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101] \ | |
+ [1] * 6 \ | |
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101] | |
# Non-word characters will not change the anagram key (1 is the multiplicative identity) and are effectively ignored. | |
# The primes list is duplicated for upper and lower-case characters, so case is also ignored. | |
def anagram_key(word): | |
# Get the ordinal or "character code" for each character in the word | |
# eg. "abc" becomes [97, 98, 99] | |
word_ordinals = map(ord, word) | |
# Map each of these ordinals to a number in the prime list | |
word_primes = [primes[i] for i in word_ordinals] | |
# The product of these primes will be unique to those primes (number theory), | |
# However, because the order does not matter with respect to multiplication | |
# (commutative property of multiplication) we can say that this key will be | |
# unique to this word AND all of its anagrams. | |
product_of_primes = reduce(mul, word_primes) | |
return product_of_primes | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment