Merge pull request #1 from ben-zen/master
[wordplay-cdsw-solutions] / solution_6.py
1 import scrabble
2
3 # Print the longest word where every character is unique.
4 # I use a double loop in this. Note that I also re-use my "longest word" logic
5 # from the easy solution to (2).
6
7 # See the advanced solution for a shorter way to do this.
8
9 new_words = []
10 for word in scrabble.wordlist:
11     local_chars = {}
12     seen_before = False
13     for character in word:
14         # have we seen this character before?
15         if character in local_chars:
16             seen_before = True
17             break # Exit the loop early, since we've found a collision.
18         # store the character
19         local_chars[character] = 1
20
21     if not seen_before:
22         new_words.append(word)
23
24 # Reuse my code for longest word
25 longest_so_far = ''
26 for word in new_words:
27     if len(word) > len(longest_so_far):
28         longest_so_far = word
29
30 print longest_so_far

Benjamin Mako Hill || Want to submit a patch?