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

Benjamin Mako Hill || Want to submit a patch?