Whitespace corrections, a couple bug fixes, replacing a format string with constructi...
authorBen Lewis <benjf5+github@gmail.com>
Mon, 10 Nov 2014 04:11:57 +0000 (20:11 -0800)
committerBen Lewis <benjf5+github@gmail.com>
Mon, 10 Nov 2014 04:11:57 +0000 (20:11 -0800)
12 files changed:
solution_1.py
solution_2.py
solution_2_advanced.py
solution_3.py
solution_4.py
solution_5_easy.py
solution_6.py
solution_6_advanced.py
solution_7.py
solution_8.py
solution_8_broken.py
solution_9.py

index dc2c57b68ca78c07411e39e06549f5e860f0bc1e..fe18444bc69cb2012f831eabb0f33188f7d0e60c 100644 (file)
@@ -1,12 +1,7 @@
 import scrabble
 
-
-# Print every word that has 9 or more letters and starts with a. 
-
+# Print every word that has 9 or more letters and starts with a.
 
 for word in scrabble.wordlist:
     if word[0] == 'a' and len(word) >= 9:
         print word
-            
-
-
index 9d5469d8cbbc5a93f2c0e221cd2214390a545f8b..335c79ae56ea5675693ba047c0df7ee2f2eed4e0 100644 (file)
@@ -1,6 +1,5 @@
 import scrabble
 
-
 # What is the longest word that starts with a q?
 # This is the most common student solution, which prints the *first* occurence of
 # the longest word. See solution_2_advanced.py for more info.
@@ -10,12 +9,8 @@ longest_so_far = '' # Store the longest word we've seen up to this point.
 for word in scrabble.wordlist:
 
     if word[0] == 'q':
-       if len(word) > len(longest_so_far):  # What if I use >= rather than > here?
-           longest_so_far = word
-            #print word  # Use this to see the progression.
-
+        if len(word) > len(longest_so_far): # What if I use >= rather than > here?
+            longest_so_far = word
+            #print word # Use this to see the progression.
 
 print longest_so_far
-            
-
-
index dc157a3095a1ec0a699d04d5a2505d8efd9bae9c..e4f056d844100e90eda53826ce4c80fd488f5f56 100644 (file)
@@ -1,6 +1,5 @@
 import scrabble
 
-
 # What is the longest word that starts with a q?
 
 # This problem was ill-specified. What if there isn't a single longest word? What if
@@ -8,7 +7,6 @@ import scrabble
 # We designed this problem with that ambiguity in mind, because sometimes the thing
 # we want to measure may not exist, or may not exist in the way we anticipate.
 
-
 # This solution keeps EVERY word of the longest length.
 
 longest_so_far = [] 
@@ -17,15 +15,11 @@ length_of_longest_word = 0
 for word in scrabble.wordlist:
 
     if word[0] == 'q':
-       if len(word) > length_of_longest_word:
-           length_of_longest_word = len(word)
-           longest_so_far = [word]
-       elif len(word) == length_of_longest_word:
-           longest_so_far.append(word)
+        if len(word) > length_of_longest_word:
+            length_of_longest_word = len(word)
+            longest_so_far = [word]
+        elif len(word) == length_of_longest_word:
+            longest_so_far.append(word)
             #print longest_so_far  # Use this to see the progression.
 
-
 print longest_so_far
-            
-
-
index a3ea28d4f9e7f5c7cf7398362694c5700b9bc0c9..ea53edf3bc844a7d3590e85e832229534f55b969 100644 (file)
@@ -1,12 +1,7 @@
 import scrabble
 
-
-# Find all the words that end in 'nge' 
-
+# Find all the words that end in 'nge'
 
 for word in scrabble.wordlist:
     if word[-3:] == 'nge':
         print word
-            
-
-
index c1e72aefa1e89687a874c4dcfb1c93f55584e7a5..613aacd9e71df99440ed9a543092669e7df43ad2 100644 (file)
@@ -1,13 +1,7 @@
 import scrabble
 
-
-# Find words that match a**e*y.  
-
-
+# Find words that match a**e*y.
 
 for word in scrabble.wordlist:
-    if len(word) == 6 and word[0] == 'a' and word[3] == 'e' and word[5] == 'y': 
+    if len(word) == 6 and word[0] == 'a' and word[3] == 'e' and word[5] == 'y':
         print word
-            
-
-
index d34efdc085a6b5e12b02df5920f4b821d250808b..eec37fa096f0d551bd321e83dc7da98a96044983 100644 (file)
@@ -1,7 +1,6 @@
 import scrabble
 
-
-# Print every other word that starts with 'a' and ends with 9
+# Print every other word that starts with 'a' and is more than 9 letters long.
 # This is the most basic implementation: keep a boolean to track whether the word
 # was printed.
 
@@ -14,4 +13,3 @@ for word in scrabble.wordlist:
             should_i_print = False
         else:
             should_i_print = True
-            
index e4d7b3e25aa350dc704e0aa99239b136b17bf458..62e13f2d0731c38c9840e6d8fea1fea0e2e62e8d 100644 (file)
@@ -1,13 +1,11 @@
 import scrabble
 
-
-# Print hte longest word where every digit is unique.
+# Print the longest word where every character is unique.
 # I use a double loop in this. Note that I also re-use my "longest word" logic
 # from the easy solution to (2).
 
 # See the advanced solution for a shorter way to do this.
 
-
 new_words = []
 for word in scrabble.wordlist:
     local_chars = {}
@@ -16,18 +14,17 @@ for word in scrabble.wordlist:
         # have we seen this character before?
         if character in local_chars:
             seen_before = True
-            break
+            break # Exit the loop early, since we've found a collision.
         # store the character
         local_chars[character] = 1
-    
+
     if not seen_before:
         new_words.append(word)
-            
-# Reuse my code for longest
+
+# Reuse my code for longest word
 longest_so_far = ''
 for word in new_words:
     if len(word) > len(longest_so_far):
         longest_so_far = word
-        
-print longest_so_far
 
+print longest_so_far
index 24927ad0642e05d883293cd2b8df8bfd6a926bac..5db3920ddbcf023b3cf67609e33a4a634e7fb4bd 100644 (file)
@@ -1,25 +1,21 @@
 import scrabble
 
-
-# Print the longest word where every digit is unique.
+# Print the longest word where every character is unique.
 # I used a Set for this. 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.
 
-
 new_words = []
 for word in scrabble.wordlist:
     local_chars = {}
-    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 (in this case, the code to track all occurences, from the 
+# Reuse my code for longest word, (in this case, the code to track all occurences) from the
 # advanced solution.
 
 longest_so_far = []
@@ -27,8 +23,8 @@ length_of_longest_word = 0
 
 for word in new_words:
     if len(word) > length_of_longest_word:
-       length_of_longest_word = len(word)
-       longest_so_far = [word]
+        length_of_longest_word = len(word)
+        longest_so_far = [word]
     elif len(word) == length_of_longest_word:
         longest_so_far.append(word)
 
index b7f8dde6d7858d1e58d4c0782f067d0c031d0f05..61cb133795aefc14d9764896503dba58b817bba3 100644 (file)
@@ -1,8 +1,6 @@
 import scrabble
 
-
-# Find the most valuable word in the dictionary using a double-loop. 
-
+# Find the most valuable word in the dictionary using a double-loop.
 
 max_score = 0
 max_word = ''
@@ -11,10 +9,8 @@ for word in scrabble.wordlist:
     for char in word:
         score = score + scrabble.scores[char]
     if score > max_score:
-       max_word = word
-       max_score = score
+        max_word = word
+        max_score = score
 
 print max_score
 print max_word
-
-
index d183cd2fbfbfb176f104214431a197cd6f95a73e..75aaa5e2e9cca63c7d901e62e97cfa34690391a2 100644 (file)
@@ -1,22 +1,17 @@
 import scrabble
 
-
 # See if you can tell the difference between this solution and the broken solution.
 
-
-
 max_score = 0
 max_word = ''
 for word in scrabble.wordlist:
-    if len(word) <= 7 and len(word) >= 5 and word[0] == 'a' and word[3] == 'e':
-       score = 0
+    if len(word) <= 7 and len(word) >= 4 and word[0] == 'a' and word[3] == 'e':
+        score = 0
         for char in word:
             score = score + scrabble.scores[char]
         if score > max_score:
-           max_word = word
-           max_score = score
+            max_word = word
+            max_score = score
 
 print max_score
 print max_word
-
-
index 06beb89b378e8c5c487964b19f34ec1dc5616435..554fcd9798c21f47750f3bd66a7ab075646e406b 100644 (file)
@@ -1,22 +1,17 @@
 import scrabble
 
-
-# See if you can tell the difference between this solution and the broken solution.
-
-
+# See if you can tell the difference between this solution and the working solution.
 
 max_score = 0
 max_word = ''
 for word in scrabble.wordlist:
     if word[0] == 'a' and word[3] == 'e' and len(word) <= 7 and len(word) >= 4:
-       score = 0
+        score = 0
         for char in word:
             score = score + scrabble.scores[char]
         if score > max_score:
-           max_word = word
-           max_score = score
+            max_word = word
+            max_score = score
 
 print max_score
 print max_word
-
-
index 1642ee8bf41f6b78dac7bc3033f5a83b102f879d..d1d60bb4506932f80edb4020facf3dd99341c9ac 100644 (file)
@@ -12,20 +12,16 @@ expensive_word_score = {}
 for word in scrabble.wordlist:
     first_letter = word[0]
     if first_letter not in expensive_word:
-       expensive_word[first_letter] = ''
-       expensive_word_score[first_letter] = 0
+        expensive_word[first_letter] = ''
+        expensive_word_score[first_letter] = 0
 
     score = 0
     for char in word:
         score = score + scrabble.scores[char]
 
     if expensive_word_score[first_letter] < score:
-       expensive_word[first_letter] = word
-       expensive_word_score[first_letter] = score
-
+        expensive_word[first_letter] = word
+        expensive_word_score[first_letter] = score
 
 for key in expensive_word:
-       print "{key}: {word}, {score}".format(key=key, word=expensive_word[key], score=expensive_word_score[key])
-         
-
-
+    print(key + ": " + expensive_word[key] + ", " + str(expensive_word_score[key]))

Benjamin Mako Hill || Want to submit a patch?