From: Ben Lewis Date: Mon, 10 Nov 2014 04:11:57 +0000 (-0800) Subject: Whitespace corrections, a couple bug fixes, replacing a format string with constructi... X-Git-Url: https://projects.mako.cc/source/wordplay-cdsw-solutions/commitdiff_plain/d57af8e41dd0a0a46fc411ad2488d029244921e1 Whitespace corrections, a couple bug fixes, replacing a format string with constructing the string the normal way (just to reduce confusion.) --- diff --git a/solution_1.py b/solution_1.py index dc2c57b..fe18444 100644 --- a/solution_1.py +++ b/solution_1.py @@ -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 - - - diff --git a/solution_2.py b/solution_2.py index 9d5469d..335c79a 100644 --- a/solution_2.py +++ b/solution_2.py @@ -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 - - - diff --git a/solution_2_advanced.py b/solution_2_advanced.py index dc157a3..e4f056d 100644 --- a/solution_2_advanced.py +++ b/solution_2_advanced.py @@ -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 - - - diff --git a/solution_3.py b/solution_3.py index a3ea28d..ea53edf 100644 --- a/solution_3.py +++ b/solution_3.py @@ -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 - - - diff --git a/solution_4.py b/solution_4.py index c1e72ae..613aacd 100644 --- a/solution_4.py +++ b/solution_4.py @@ -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 - - - diff --git a/solution_5_easy.py b/solution_5_easy.py index d34efdc..eec37fa 100644 --- a/solution_5_easy.py +++ b/solution_5_easy.py @@ -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 - diff --git a/solution_6.py b/solution_6.py index e4d7b3e..62e13f2 100644 --- a/solution_6.py +++ b/solution_6.py @@ -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 diff --git a/solution_6_advanced.py b/solution_6_advanced.py index 24927ad..5db3920 100644 --- a/solution_6_advanced.py +++ b/solution_6_advanced.py @@ -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) diff --git a/solution_7.py b/solution_7.py index b7f8dde..61cb133 100644 --- a/solution_7.py +++ b/solution_7.py @@ -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 - - diff --git a/solution_8.py b/solution_8.py index d183cd2..75aaa5e 100644 --- a/solution_8.py +++ b/solution_8.py @@ -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 - - diff --git a/solution_8_broken.py b/solution_8_broken.py index 06beb89..554fcd9 100644 --- a/solution_8_broken.py +++ b/solution_8_broken.py @@ -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 - - diff --git a/solution_9.py b/solution_9.py index 1642ee8..d1d60bb 100644 --- a/solution_9.py +++ b/solution_9.py @@ -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]))