Created
February 27, 2014 18:40
-
-
Save sirpengi/9256344 to your computer and use it in GitHub Desktop.
Look at that O(n)
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
[sirpengi@localhost tmp]$ python anagrams.py | |
############# (1.31171178818) | |
################ (1.60090017319) | |
################### (1.9009168148) | |
###################### (2.23292398453) | |
######################### (2.55905890465) |
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 collections import Counter | |
def main(n): | |
with open("/usr/share/dict/words") as fh: | |
solutions = {} | |
for i, line in enumerate(fh.readlines()): | |
if i > n: | |
break | |
w = line.lower().strip() | |
h = tuple(sorted(Counter(w).elements())) | |
if h not in solutions: | |
solutions[h] = set() | |
solutions[h].add(w) | |
def bar(v): | |
print "{0} ({1})".format("#" * (int(v * 10)), v) | |
if __name__ == '__main__': | |
import timeit | |
bar(timeit.timeit("main(500)", number=10, setup="from __main__ import main")) | |
bar(timeit.timeit("main(1000)", number=10, setup="from __main__ import main")) | |
bar(timeit.timeit("main(1500)", number=10, setup="from __main__ import main")) | |
bar(timeit.timeit("main(2000)", number=10, setup="from __main__ import main")) | |
bar(timeit.timeit("main(2500)", number=10, setup="from __main__ import main")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment