-
Notifications
You must be signed in to change notification settings - Fork 45
Dragonfly_Qiaoqiao Tan #37
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: main
Are you sure you want to change the base?
Conversation
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.
Great job on Adagrams, Qiaoqiao!
for alphabet, frenquency in LETTER_POOL.items(): | ||
letter_pool_list.extend([alphabet] * frenquency) |
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.
Great job on the loop here! I love that you used .items
and .extend
!
adagrams/game.py
Outdated
while len(letter_list)<10: | ||
i = randint(0, len(letter_pool_list)-1) | ||
letter_list.append(letter_pool_list[i]) | ||
letter_pool_list.pop(i) | ||
return letter_list |
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.
Recall that .pop
not only removes the element at the specified position i
but also returns it. With that we could rewrite what you have to the following:
while len(letter_list) < 10:
i = randint(0, len(letter_pool_list)-1)
letter_list.append(letter_pool_list.pop(i))
return letter_list
|
||
def uses_available_letters(word, letter_bank): | ||
pass | ||
letter_bank_list = list(letter_bank) |
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.
✨
|
||
def score_word(word): | ||
pass | ||
score_dict = { | ||
'A': 1, 'E': 1, 'I': 1, 'O': 1, 'U': 1, 'L': 1, 'N': 1, 'R': 1, 'S': 1, 'T': 1, |
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.
We should use multiple lines (see here in the style guide about using multilines and indentation to create data structures).
score = 0 | ||
for alphabet in word.upper(): | ||
score += score_dict[alphabet] | ||
if len(word) in {7,8,9,10}: |
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.
Though this is functionally sound, I don't feel as if this communicates the intention behind what's going on here as readily as something like if len(word) >= 7
. This is how you will conventionally see the length of things checked in Python.
letter_list.append(letter_pool_list[i]) | ||
letter_pool_list.pop(i) | ||
return letter_list | ||
|
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.
Another approach you could take to this is use a cumulative weight. This would cut the list of 98 letters from our code. We would essentially make another dictionary cumulating the weights:
LETTER_POOL = {'A': 9, 'B': 2, 'C': 2, 'D': 4, 'E': 12}
cumulative_weights = {'A': 9, 'B': 11, 'C': 13, 'D': 17, 'E': 29} # Total Weight: 29
Letter | Count | Cumulative Weight |
---|---|---|
A | 9 | 9 (1-9 → 'A') |
B | 2 | 11 (10-11 → 'B') |
C | 2 | 13 (12-13 → 'C') |
D | 4 | 17 (14-17 → 'D') |
E | 12 | 29 (18-29 → 'E') |
We could then randomly select a number between 1 - total weight. After that we would loop through the cumulative_weights
dictionary and if the random number we got is less than the cumulative weight we are looking at on the current iteration we will select that that letter.
Note: The chances of choosing any number between 1 - 9 from a pile of numbers 1 - 98 is the same as choosing one of the 9 A's from the pile of 98 letters.
This code could look something like this:
Click to view the draw_letters
function
import random
LETTER_POOL = {
'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
}
def compute_cumulative_weights(letter_pool):
cumulative_weights = {}
total_weight = 0
for letter, weight in letter_pool.items():
total_weight += weight
cumulative_weights[letter] = total_weight
return cumulative_weights, total_weight
def draw_letters():
cumulative, total_weight = compute_cumulative_weights(LETTER_POOL)
selected_letters = []
for _ in range(10):
rand_value = random.randint(1, total_weight) # Pick a random number
for letter, cumulative_weight in cumulative.items():
if rand_value <= cumulative_weight:
selected_letters.append(letter)
break
return selected_letters
min_len = len(tie_word) | ||
|
||
return (max_word,max_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.
Great job on breaking these up! It makes the flow a lot easier when you create essentially two sub-problems. One being to find the tied words for highest score and then the other being to implement the tie breaking logic. Perfect way to approach a problem! ⭐️
Co-authored-by: Mikelle <[email protected]>
Co-authored-by: Mikelle <[email protected]>
Co-authored-by: Mikelle <[email protected]>
No description provided.