How To Improve Permutation Algorithm Efficiency With Python
I have a file that contains just over 100,000 words. What I have to do is run through every 5 letter combination of the alphabet to work out the 5 letters that are used by the leas
Solution 1:
- you can use combinations instead of permutations
- why not just scan all words once, count the number of appearances of each letter, then select the 5 with minimum number of appearances ?
Solution 2:
This is not a question about increasing permutation efficiency. This is really a question about how to make a smarter algorithm, it is a data structure question.
I have a file that contains just over 100,000 words. What I have to do is run through every 5 letter combination of the alphabet to work out the 5 letters that are used by the least number of words.
Loop through the 26 letters of the alphabet and count the number of words in your list which use each letter:
importstring
alphabet = string.ascii_lowercase
counts = {k: sum(k in word.lower() for word in words) for k in alphabet}
This should be pretty fast, and should give you enough information to trivially pick out the five least popular letters.
Equivalent approach, probably more efficient but maybe less clear than the above.
from itertools import chain
from collections import Counter
counter = Counter({k: 0for k in string.ascii_lowercase})
counter.update(Counter(c for w in words for c inset(w.lower())))
Post a Comment for "How To Improve Permutation Algorithm Efficiency With Python"