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

Benjamin Mako Hill || Want to submit a patch?