5db3920ddbcf023b3cf67609e33a4a634e7fb4bd
[wordplay-cdsw-solutions] / solution_6_advanced.py
1 import scrabble
2
3 # Print the longest word where every character is unique.
4 # I used a Set for this. Don't worry: you didn't miss anything if you don't know
5 # what a set is. We didn't teach it, but if you are reading this, you get a bonus!
6
7 # A set is a container like a list or a dict, except that *each element can be stored only once*.
8 # Think of it like the keys of a dict, except there isn't any value associated with each key.
9 # I use Sets to count digits below. Feel free to look up the Set online and try it in the
10 # interpreter.
11
12 new_words = []
13 for word in scrabble.wordlist:
14     local_chars = {}
15     if len(word) == len(set(word)): # Wait what!? See if you can figure out why this works.
16         new_words.append(word)
17
18 # Reuse my code for longest word, (in this case, the code to track all occurences) from the
19 # advanced solution.
20
21 longest_so_far = []
22 length_of_longest_word = 0
23
24 for word in new_words:
25     if len(word) > length_of_longest_word:
26         length_of_longest_word = len(word)
27         longest_so_far = [word]
28     elif len(word) == length_of_longest_word:
29         longest_so_far.append(word)
30
31 print longest_so_far

Benjamin Mako Hill || Want to submit a patch?