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

phoenix class - Anees Quateja #39

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
122 changes: 118 additions & 4 deletions adagrams/game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,125 @@

import random


def draw_letters():
pass
# array_of_strings = [ ]
LETTER_POOL = {

Choose a reason for hiding this comment

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

This is a great dictionary to hold the letters and the count of each letter! When creating it, you've followed the naming convention of a global variable which I agree would be the best way to hold this information just in case we need to use this dictionary elsewhere! The one change I would make here would be to move it outside this function which is what we would do to make sure the dictionary is available to the other functions that might need it. If we have several pieces of this kind of information, it is also possible to put your data in a separate file and import it in as well!

'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
}

letter_pool = []
for letter, count in LETTER_POOL.items():

Choose a reason for hiding this comment

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

I love this way of creating a list to hold every letter while still taking into account the distribution! It's short and concise and works well! Great use of the extend methond!

letter_pool.extend([letter] * count)

hand = []

for _ in range(10):
random_index = random.randint(0, len(letter_pool) - 1)
hand.append(letter_pool[random_index])
letter_pool.pop(random_index)

Choose a reason for hiding this comment

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

This for loop is once again concise and well constructed! Great use of the pop method as well. The only consideration I would add is that the pop method does run in O(n) time. If you'd like an extra challenge, see if you can write this in a way that doesn't use the pop method and runs in O(1) time! Note: That code will be a little more involved but it will decrease the complexity.


return hand





def uses_available_letters(word, letter_bank):
pass

word = word.upper()
letter_bank_remaining = letter_bank.copy()

for letter in word:
if letter in letter_bank_remaining:
letter_bank_remaining.remove(letter)
else:
return False
return True

Choose a reason for hiding this comment

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

Love this function! It's easy to read, well written and concise! No notes!





def score_word(word):
pass

# Define the letter scores
score_chart = {

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 expand on this project, this dictionary would be a great candidate for global variable status!

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

# Initialize score to 0
total_score = 0

# Convert word to uppercase to handle case insensitivity
word = word.upper()

# Sum up the score for each letter
for letter in word:
total_score += score_chart.get(letter, 0) # Add 0 if letter is not found (for safety)

Choose a reason for hiding this comment

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

I like the safety check here!


# Add bonus points if the word is 7-10 letters long
if 7 <= len(word) <= 10:

Choose a reason for hiding this comment

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

Great use of this notation for a range based conditional!

total_score += 8

return total_score




def get_highest_word_score(word_list):
pass
# Initialize the best word and score
best_word = None
best_score = 0

# Loop through each word in the list
for word in word_list:
# Get the score for the current word
current_score = score_word(word)

# Update best_word and best_score if conditions are met
if (

Choose a reason for hiding this comment

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

I love that you handled all of the logic as each word gets scored! I also commend you for taking care of all the logic in one long compound conditional! That being said, I would argue that this is one case where nested conditionals will make the code a little more readable. It will also cut down on some of the conditionals you have to parse through!

best_word is None or # First word
current_score > best_score or # Higher score
(
current_score == best_score and len(word) == 10 and len(best_word) != 10 # Prefer 10-letter word
) or (
current_score == best_score and len(word) < len(best_word) and len(best_word) != 10 # Prefer shorter word
)
):
best_word = word
best_score = current_score

# Return the word with the highest score
return best_word, best_score