Skip to content
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

Kunzite - Cindy V. #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 128 additions & 4 deletions adagrams/game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,135 @@
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 draw_letters():
pass
letter_list = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'F', 'F', 'G', 'G', 'G', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'J', 'K', 'L', 'L', 'L', 'M', 'M', 'N', 'N', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'P', 'P', 'Q', 'R', 'R', 'R', 'R', 'R', 'S', 'S', 'S', 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'U', 'U', 'U', 'U', 'V', 'V', 'W', 'X', 'Y', 'Y', 'Z']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do need a copy of the letters each time the function runs since the list is being modified (removing a tiles each call), but instead of hard coding the list, which is error prone due to the repetition (for instance, there are 9 Es in the list, but we're supposed to have 12 of them!), could we make use of the more readable dictionary data to generate the list?

players_hand = []


while len(players_hand) < 10:
random_letter = random.choice(letter_list)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using a library function like this that we haven't discussed, I encourage you to either include a comment that explains your understanding of how it works, or consider implementing the logic yourself rather than using the library function for additional practice!

players_hand.append(random_letter)

letter_list.remove(random_letter)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we start to discuss big O (coming up) we'll see that removing from a list is realtively inefficient. This does guarantee that future picks won't use a letter we've already "consumed", but in a loop, we'd like to avoid remove if possible. To remove a value from a list, we need to iterate to find the value, then continue iterating to the end of the list shifting the remaining values forward (we can't have holes in a list). So each time we add a letter to the hand, we also need to iterate over the letter list. For the length of the letter list in our scenario, this isn't a big deal, but in a more general case, we'd like to avoid this if possible.

Is there some other way we could track which letters we've used without needing to remove them, but also ensuring that we don't pick the same tile again?


return players_hand




def uses_available_letters(word, letter_bank):
pass
word = word.upper()

for letter in word:
if word.count(letter) > letter_bank.count(letter):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were going to implement count ourselves, we would need to iterate through the list. So each time this loops over a letter from word, we are iterating over the word and the bank. Could we do all our counts a different way before we start looping? What tradeoff would we be making to store the results for later use?

return False

return True




def score_word(word):
pass
score_dict = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the LETTER_POOL, this could live externally to the function (globally) so that it doesn't need to be regenerated with every call to the function.

'A': 1,
'B': 3,
'C': 3,
'D': 2,
'E': 1,
'F': 4,
'G': 2,
'H': 4,
'I': 1,
'J': 8,
'K': 5,
'L': 1,
'M': 3,
'N': 1,
'O': 1,
'P': 3,
'Q': 10,
'R': 1,
'S': 1,
'T': 1,
'U': 1,
'V': 4,
'W': 4,
'X': 8,
'Y': 4,
'Z': 10
}

result = 0
for letter in word.upper():
result += score_dict[letter]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Great approach to look up the score for each letter and accumulate it into the result.

if len(word) >= 7:
result += 8
Comment on lines +92 to +93

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nice bonus handling.


return result



def get_highest_word_score(word_list):
pass
high_score = 0
best_word = ""

for word in word_list:
score = score_word(word)

if score > high_score:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nice job breaking down the conditons into these checks. They read very cleanly.

high_score = score
best_word = word


elif score == high_score:
if len(best_word) == 10:
continue
elif len(word) == 10:
high_score = score
best_word = word

elif len(word) < len(best_word) :
high_score = score
best_word = word


return best_word, high_score

# - Has one parameter: `word_list`, which is a list of strings
# - Returns a tuple that represents the data of a winning word and it's score. The tuple must contain the following elements:
# - index 0 ([0]): a string of a word
# - index 1 ([1]): the score of that word
# - In the case of tie in scores, use these tie-breaking rules:
# - prefer the word with the fewest letters...
# - ...unless one word has 10 letters. If the top score is tied between multiple words and one is 10 letters long, choose the one with 10 letters over the one with fewer tiles
# - If the there are multiple words that are the same score and the same length, pick the first one in the supplied list