Conversation
… True. Passed 2 tests.
…nt in letter bank
…ional in loop to count how many letters are in the letter bank, and used count to equal the number of letters in word
…eated a nested loop to read into the dictionary and lists to find the letter and then add the value to the score, which iterated through the word. Added the additioanl points if length of word is greater than 6.
… the string has any lowercase letters.
… the score and word to variables outside of the loop.
…cking the word with the highest score.
| output is a list of 10 random letters | ||
| """ | ||
| # assign variable names to the letters and max usage num | ||
| letter_list, usage_list = list(letter_pool.keys()), list(letter_pool.values()) |
There was a problem hiding this comment.
Love the multi-assignment and your use of the dictionary methods here!
| add_letter = hand.append(random_letter) | ||
| else: | ||
| continue | ||
| return hand |
There was a problem hiding this comment.
Right now your code isn't actually utilizing the weight distribution of the letters. Currently, all letters have the same chance of being selected (they all can only appear their respective limits). randint is only randomly selecting from a pool of integers from 0 to 26. This means that Z is just as likely as likely to be selected as E. You would need it to select from a pool of integers from 0 to 98 to have the correct distribution. How could we change your letter pool to consider this weighted distribution?
| # word = word.upper() | ||
| # count = 0 | ||
| # for letter in word: | ||
| # if word.count(letter) > letter_bank.count(letter): | ||
| # return False | ||
| # elif letter in letter_bank: | ||
| # count += 1 | ||
| # if count == len(word): | ||
| # return True |
There was a problem hiding this comment.
Don't forget to remove your comments like these.
| # if count == len(word): | ||
| # return True | ||
|
|
||
| word = word.upper() |
There was a problem hiding this comment.
Nice work on ensuring that all the letters where of the same casing. You could also use casefold, which is used for caseless matching. You can find more information on the method here.
Also, be mindful of re-assigning the parameter of a function. That could cause issues for the caller of the function if the change is not expected. Usually we want to avoid this unless specifically told to.
|
|
||
| for each_letter in word: | ||
| if each_letter in letter_bank_copy: | ||
| list.append(each_letter) |
There was a problem hiding this comment.
Right now I don't see the use case for making the list and appending things to it. If you remove it from your code it would work the same. Feel free to reply to this comment and explain your reasoning.
| letter_bank_copy.remove(each_letter) | ||
| else: | ||
| return False | ||
| return True |
There was a problem hiding this comment.
An Alternative approach to this function could be:
def uses_available_letters(word, letter_bank):
casefold_word = word.casefold()
temporary_letter_bank = [letter.casefold() for letter in letter_bank]
for letter in casefold_word:
if letter not in temporary_letter_bank:
return False
temporary_letter_bank.remove(letter)
return True| if len(word) > 6: | ||
| score += 8 | ||
|
|
||
| return score |
| # if the lenth of word is 10, chooses it or the first word | ||
| if each_value == 10: | ||
| highest_value_word = each_key | ||
| break |
| word_length = each_value | ||
| highest_value_word = each_key | ||
|
|
||
| return highest_value_word, highest_value No newline at end of file |
There was a problem hiding this comment.
Nice work writing this function. The separation of the getting the tying score words and the tie-breaking logic help this to be more readable here (especially if you ever have to debug it). An alternative approach could be like this:
def get_highest_word_score(word_list):
best_word = ""
best_score = 0
for word in word_list:
score = score_word(word)
if score > best_score:
best_word = word
best_score = score
elif score < best_score:
pass
elif len(best_word) == 10:
break
elif len(word) == 10 or len(word) < len(best_word):
best_word = word
best_score = score
return (best_word, best_score)
No description provided.