Skip to content

Instantly share code, notes, and snippets.

@zmagg
Last active December 21, 2015 02:38
Show Gist options
  • Save zmagg/6235901 to your computer and use it in GitHub Desktop.
Save zmagg/6235901 to your computer and use it in GitHub Desktop.
hacker school mini improvised contest for the fastest solution to find what words are the longest anagrams of each other (single words, please, no phrases) in the sowpods scrabble dictionary.
f = open("sowpods.txt", "r")
canonized_words = {}
word1 = ""
word2 = ""
max_len = 0
for line in f:
if len(line) > max_len:
sorted_word = ''.join(sorted(line))
if sorted_word in canonized_words:
found_word = canonized_words[sorted_word]
word1 = line
word2 = found_word
max_len = len(word1)
else:
canonized_words[sorted_word] = line
print word1 + ", " + word2
@zmagg
Copy link
Author

zmagg commented Aug 14, 2013

This solution runs in .130 seconds in my intel core 2 duo 2.4 Ghz, 4GB ram, SSD

@machinaut
Copy link

It's running much faster that that on mine, so I probably have a smaller dictionary. What URL did you pull the sowpods.txt from?

@zmagg
Copy link
Author

zmagg commented Aug 15, 2013

: D you might just have a better CPU...or SSD read rate? http://norvig.com/ngrams/sowpods.txt

@zmagg
Copy link
Author

zmagg commented Aug 15, 2013

hah, also, we all compared 'real' time within hacker school (as opposed to system time). i actually don't understand the differences between the 3 (real, user, sys). i assume you do?

@machinaut
Copy link

Cool! Now I wanna look at the problem :-}

Real is the clock time spent solving the problem (actual time passed between start and finish)
User is the processor time spent in user mode (not in the kernel) -- if your process runs on more than one CPU at once this can actually be greater than real time.
Sys is the CPU time spent in the application not in user mode -- also can be greater than real if running on more than one CPU -- examples of this are time spent in syscalls, or when in the kernel doing work on behalf of the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment