-
Notifications
You must be signed in to change notification settings - Fork 89
Snow Leopards - Elsje & Dainiz #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ae4c837
d53f07e
a3fa686
dcc200e
36072b3
cc92fda
1407c31
5f74a23
56f5fcd
3e7810e
c2ec6af
7233f28
b81e62d
75da116
e95ccb2
fed616b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,109 @@ | ||
| import random | ||
|
|
||
| LETTER_DISTRIBUTION = { | ||
| "A": 9, | ||
| "B": 2, | ||
| "C": 2, | ||
| "D": 4, | ||
| "E": 12, | ||
| "F": 2, | ||
| "G": 3, | ||
| "H": 2, | ||
| "I": 9, | ||
| "J": 1, | ||
| "K": 1, | ||
| "L": 4, | ||
| "M": 2, | ||
| "N": 6, | ||
| "O": 8, | ||
| "P": 2, | ||
| "Q": 1, | ||
| "R": 6, | ||
| "S": 4, | ||
| "T": 6, | ||
| "U": 4, | ||
| "V": 2, | ||
| "W": 2, | ||
| "X": 1, | ||
| "Y": 2, | ||
| "Z": 1 | ||
| } | ||
|
|
||
| SCORE_CHART = { | ||
| 1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], | ||
| 2: ["D", "G"], | ||
| 3: ["B", "C", "M", "P"], | ||
| 4: ["F", "H", "V", "W", "Y"], | ||
| 5: ["K"], | ||
| 8: ["J", "X"], | ||
| 10: ["Q", "Z"] | ||
| } | ||
|
|
||
| def draw_letters(): | ||
| pass | ||
| letter_pool = [] | ||
| letter_bank = [] | ||
| for letter, count in LETTER_DISTRIBUTION.items(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works perfectly well! I have a couple of suggestions - you don't really need to turn the for letter in LETTER_DISTRIBUTION:
count = LETTER_DISTRIBUTION[letter]
while count > 0:
letter_pool.append(letter)
count -= 1 |
||
| for i in range(count): | ||
| letter_pool.append(letter) | ||
| # Should we rewrite 45-47 with list comprehension? This is what we tried: | ||
| # letter_pool = [[letter] * count | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to write a list comprehension using the nested loop approach, you'd need a nested list comprehension. I speak for myself in saying I don't usually find those to be very readable. |
||
| # for letter, count in LETTER_DISTRIBUTION.items()] | ||
| for letter in range(10): | ||
| random_letter = random.choice(letter_pool) | ||
| letter_bank.append(random_letter) | ||
| letter_pool.remove(random_letter) | ||
| return letter_bank | ||
|
|
||
| def uses_available_letters(word, letter_bank): | ||
| pass | ||
| bank_dict = {} | ||
| # We tried to implement dictionary comprehension for this, | ||
| # but are unsure if it's possible: | ||
| # alt_bank_dict = {letter, 1 for letter in letter_bank | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible! Try this: bank_dict = {letter: letter_bank.count(letter) for letter in letter_bank} |
||
| # if item not in alt_bank_dict else letter, + 1} | ||
| for item in letter_bank: | ||
| if item not in bank_dict: | ||
| bank_dict[item] = 1 | ||
| else: | ||
| bank_dict[item] += 1 | ||
| for letter in word.upper(): | ||
| if letter not in bank_dict.keys(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python can iterate over a dictionary and uses its keys to do so, so no need to convert the dictionary to a list of dictionary keys just for iterating over it. |
||
| return False | ||
| elif letter in bank_dict.keys(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, no need to convert to a list first. |
||
| bank_dict[letter] -= 1 | ||
|
|
||
| if bank_dict[letter] < 0: | ||
| return False | ||
| return True | ||
|
|
||
| def score_word(word): | ||
| pass | ||
| total_score = 0 | ||
| for character in word.upper(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does work however, if you create a data structure like: {
'A': 1,
'B': 3,
'C': 3,
'D': 2,
...
}Then you can avoid nested iteration: for letter in word:
score_total += LETTER_VALUES[letter]Which is much simpler and easier to read! Always consider the best data structure that allows the simplest and most straightforward access. |
||
| for tally, letters in SCORE_CHART.items(): | ||
| if character in letters: | ||
| total_score += tally | ||
| if len(word) >= 7: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice |
||
| total_score += 8 | ||
| return total_score | ||
|
|
||
| def get_highest_word_score(word_list): | ||
| pass | ||
| word_info = [{"word": word, "score": score_word(word), "length": len(word)} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function works great! One minor improvement would be to avoid looping by checking whether there's only one element in if len(word_info) == 1:
return word_info[0]["word"], word_info[0]["score"]Then go on to do all the tie-break looping only if you actually have a tie. |
||
| for word in word_list] | ||
|
|
||
| highest_score = max(word_info, key=lambda word_info: word_info["score"]) | ||
|
|
||
| word_info = list(filter(lambda word_dict: word_dict["score"] | ||
| == highest_score["score"], word_info)) | ||
| # We also came up with this implementation for lines 94-95: | ||
| # for word_dict in word_info: | ||
| # if word_dict["score"] != highest_score["score"]: | ||
| # word_info.remove(word_dict) | ||
|
|
||
| smallest_length = min(word_info, key=lambda word_info: word_info["length"]) | ||
|
|
||
| for word_dict in word_info: | ||
| if word_dict["length"] == 10: | ||
| return word_dict["word"], word_dict["score"] | ||
|
|
||
| for word_dict in word_info: | ||
| if word_dict["length"] == smallest_length["length"]: | ||
| return word_dict["word"], word_dict["score"] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent documentation of the plan!