Whitespace and a couple fixes.
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
-
-
-
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.
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
-
-
-
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
# 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 = []
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
-
-
-
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
-
-
-
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
-
-
-
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.
should_i_print = False
else:
should_i_print = True
-
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 = {}
# 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
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 = []
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)
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 = ''
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
-
-
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
-
-
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
-
-
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]))