Skip to content

Heather M. & Esther A. adagrams project#52

Open
cathos wants to merge 16 commits intoada-c17:masterfrom
cathos:master
Open

Heather M. & Esther A. adagrams project#52
cathos wants to merge 16 commits intoada-c17:masterfrom
cathos:master

Conversation

@cathos
Copy link
Copy Markdown

@cathos cathos commented Apr 1, 2022

No description provided.

@cathos cathos changed the title Heather M. & Esther Annorzie adagrams project Heather M. & Esther A. adagrams project Apr 1, 2022
Copy link
Copy Markdown

@kelsey-steven-ada kelsey-steven-ada left a 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!

Comment thread adagrams/game.py
@@ -1,11 +1,132 @@
# from tests.test_wave_01 import LETTER_POOL
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
Returns: A randomized list of ten letters.
'''

letters = [letter for letter, letter_frequency in LETTER_POOL.items() for number in range(letter_frequency)]
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
@@ -1,11 +1,132 @@
# from tests.test_wave_01 import LETTER_POOL
import random
LETTER_POOL = {
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
'Z': 1
}

SCORE_CHART = {
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
word_uppercase = word.upper()

for letter in word_uppercase:
if letter not in letter_bank or (word_uppercase.count(letter) > letter_bank.count(letter)):
Copy link
Copy Markdown

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:

Suggested change
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):

Comment thread adagrams/game.py
}

SCORE_CHART = {
("A", "E", "I", "O", "U", "L", "N", "R", "S", "T") : 1,
Copy link
Copy Markdown

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:

Suggested change
("A", "E", "I", "O", "U", "L", "N", "R", "S", "T") : 1,
"AEIOULNRST" : 1,

Comment thread adagrams/game.py

Return the index of the winning word.
'''
highest_index = 0
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
Comment on lines +102 to +108
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
Copy link
Copy Markdown

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:

Suggested change
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

Comment thread adagrams/game.py
'''
highest_scoring_words = []
high_score = 0
high_score_index = 0
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
high_score = score
elif score == high_score:
highest_scoring_words.append(word)
if len(highest_scoring_words) > 1:
Copy link
Copy Markdown

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.

Comment thread adagrams/game.py
highest_scoring_words.append(word)
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
Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants