-
Notifications
You must be signed in to change notification settings - Fork 69
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
Heather M. & Esther A. adagrams project #52
base: master
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 work Heather & Esther! Really interesting, concise approaches to the functions. I've added some alternatives and suggestions to refactor for readability. Take a look, and let me know if you have any questions!
@@ -1,11 +1,132 @@ | |||
# from tests.test_wave_01 import LETTER_POOL |
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 want to remove commented code from files when we're done testing.
Returns: A randomized list of ten letters. | ||
''' | ||
|
||
letters = [letter for letter, letter_frequency in LETTER_POOL.items() for number in range(letter_frequency)] |
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.
This is a really cool solution to condense filling the letters list to a single line! I would suggest breaking this apart to make it easier to read and understand. The PEP 8 style guide recommends limiting line to a max of 79 characters to make code easier to read, especially with multiple editor windows open: https://peps.python.org/pep-0008/#maximum-line-length. Outside of the style guide, when you're in situations where other people will be reading and working in the same code, the ability to quickly and easily comprehend what the code is doing is often more valuable than reducing line count. For list comprehensions like this, I would strongly suggest including a comment that describes what the line is doing.
@@ -1,11 +1,132 @@ | |||
# from tests.test_wave_01 import LETTER_POOL | |||
import random | |||
LETTER_POOL = { |
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.
Since LETTER_POOL
is only used by the draw_letters
function, it would be reasonable to place this inside the function rather than as a constant. There are tradeoffs, this structure does clutter the function some, but it would keep the data as close as possible to where it's being used, and would mean other functions would have no way to access it to accidentally alter the contents.
'Z': 1 | ||
} | ||
|
||
SCORE_CHART = { |
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.
The feedback on line 3 about placing data in the function where its used applies here too.
word_uppercase = word.upper() | ||
|
||
for letter in word_uppercase: | ||
if letter not in letter_bank or (word_uppercase.count(letter) > letter_bank.count(letter)): |
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.
This is another line I'd recommend breaking for length:
if letter not in letter_bank or (word_uppercase.count(letter) > letter_bank.count(letter)): | |
letter_count_word = word_uppercase.count(letter) | |
letter_count_bank = letter_bank.count(letter) | |
if letter not in letter_bank or (letter_count_word > letter_count_bank): |
} | ||
|
||
SCORE_CHART = { | ||
("A", "E", "I", "O", "U", "L", "N", "R", "S", "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.
Since they are iterable and non-mutable, we could also use strings instead of tuples here:
("A", "E", "I", "O", "U", "L", "N", "R", "S", "T") : 1, | |
"AEIOULNRST" : 1, |
|
||
Return the index of the winning word. | ||
''' | ||
highest_index = 0 |
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.
highest_index
feels a little ambiguous in meaning to me here. Something along the lines of highest_scored_index
or winning_index
might describe the contents a little closer.
highest_index = 0 | ||
for i in range(len(word_list)): | ||
if len(word_list[i]) == 10: | ||
return i | ||
elif len(word_list[i]) < len(word_list[highest_index]): | ||
highest_index = i | ||
return highest_index |
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 would be to loop directly over the word_list and return the winning word itself:
highest_index = 0 | |
for i in range(len(word_list)): | |
if len(word_list[i]) == 10: | |
return i | |
elif len(word_list[i]) < len(word_list[highest_index]): | |
highest_index = i | |
return highest_index | |
shortest_word = None | |
for word in word_list: | |
if len(word) == 10: | |
return word | |
elif not shortest_word or len(word) < len(shortest_word): | |
shortest_word = word | |
return shortest_word |
highest_scoring_words = [] | ||
high_score = 0 | ||
high_score_index = 0 |
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.
I recommend moving this initialization down to right above if len(highest_scoring_words) > 1:
on line 130 to keep the creation of this data close to where it is used.
elif score == high_score: | ||
highest_scoring_words.append(word) | ||
if len(highest_scoring_words) > 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.
I'd recommend whitespace between the for loop that finds the max scored words and the if statement that handles tie breaks & returns to help folks see the logical sections of the function at a glance.
if len(highest_scoring_words) > 1: | ||
high_score_index = get_index_tie_break(highest_scoring_words) | ||
return highest_scoring_words[high_score_index], high_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.
I like how you're using lists where you need to build up data then converting it to a tuple right when you need it.
No description provided.