reworked these examples a bit based on feedback in class master
authorBenjamin Mako Hill <mako@atdot.cc>
Wed, 22 Apr 2015 03:18:02 +0000 (20:18 -0700)
committerBenjamin Mako Hill <mako@atdot.cc>
Wed, 22 Apr 2015 03:18:02 +0000 (20:18 -0700)
solution_6.py
solution_6_advanced.py

index a97e6a037563fa5bceee7a1ff1d294e374b59c8c..93747c9a119db25214c816cb93b716170008993e 100644 (file)
@@ -8,23 +8,27 @@ import scrabble
 
 new_words = []
 for word in scrabble.wordlist:
 
 new_words = []
 for word in scrabble.wordlist:
-    local_chars = {}
-    seen_before = False
+    unique_letters = []
+    duplicated_letters = False
     for character in word:
         # have we seen this character before?
     for character in word:
         # have we seen this character before?
-        if character in local_chars:
-            seen_before = True
-            break # Exit the loop early, since we've found a collision.
-        # store the character
-        local_chars[character] = 1
+        if character in unique_letters:
+            duplicated_letters = True
+        else:
+            # store the character
+            unique_letters.append(character)
 
 
-    if not seen_before:
+    if not duplicated_letters:
         new_words.append(word)
 
 # Reuse my code for longest word
         new_words.append(word)
 
 # Reuse my code for longest word
-longest_so_far = ''
+longest_so_far = []
+longest_length = 0
 for word in new_words:
 for word in new_words:
-    if len(word) > len(longest_so_far):
-        longest_so_far = word
+    if len(word) > longest_length:
+        longest_so_far = [word]
+        longest_length = len(word)
+    elif len(word) == longest_length:
+        longest_so_far.append(word)
 
 print(longest_so_far)
 
 print(longest_so_far)
index 36aa0d2d78390f5032ba646676d70433eaca8d0c..43653c0f6188f0eb1ca274f8fc76fc3a73e227aa 100644 (file)
@@ -1,17 +1,15 @@
 import scrabble
 
 import scrabble
 
-# Print the longest word where every character is unique.  I used a
-# Set for this which we'll talk about today.
+# Print the longest word where every character is unique.  I used a Set for
+# this which we'll talk about today.
 
 
-# Don't worry: you didn't miss anything if you don't know what a set
-# is. We didn't teach it, but if you are reading this, you get a
-# bonus!
+# Don't worry: you didn't miss anything if you don't know what a set is. We
+# didn't teach it, but if you are reading this, you get a bonus!
 
 
-# A set is a container like a list or a dict, except that *each
-# element can be stored only once*.  Think of it like the keys of a
-# dict, except there isn't any value associated with each key.  I use
-# Sets to count digits below. Feel free to look up the Set online and
-# try it in the interpreter.
+# A set is a container like a list or a dict, except that *each element can be
+# stored only once*.  Think of it like the keys of a dict, except there isn't
+# any value associated with each key.  I use Sets to count digits below. Feel
+# free to look up the Set online and try it in the interpreter.
 
 new_words = []
 for word in scrabble.wordlist:
 
 new_words = []
 for word in scrabble.wordlist:
@@ -19,8 +17,8 @@ for word in scrabble.wordlist:
     if len(word) == len(set(word)): # Wait what!? See if you can figure out why this works.
         new_words.append(word)
 
     if len(word) == len(set(word)): # Wait what!? See if you can figure out why this works.
         new_words.append(word)
 
-# Reuse my code for longest word, (in this case, the code to track all occurences) from the
-# advanced solution.
+# Reuse my code for longest word, (in this case, the code to track all
+# occurences) from the advanced solution.
 
 longest_so_far = []
 length_of_longest_word = 0
 
 longest_so_far = []
 length_of_longest_word = 0

Benjamin Mako Hill || Want to submit a patch?