From a19bf60b8a173094f7fdfe84c1d00d5cd0e67b11 Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Wed, 22 Mar 2023 21:55:52 -0700 Subject: [PATCH 1/8] wave 1 passed --- adagrams/game.py | 32 ++++++++++++++++++++++++-------- adagrams_wave_1.py | 0 tests/test_wave_01.py | 1 + tests/test_wave_04.py | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 adagrams_wave_1.py diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..d6761704 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,11 +1,27 @@ -def draw_letters(): - pass +# def draw_letters(): +# def uses_available_letters(word, letter_bank): +# def score_word(word): +# def get_highest_word_score(word_list): + +import random -def uses_available_letters(word, letter_bank): - pass +# Function to draw random letters +def draw_letters(): + # Define the letter pool + 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 + } + + # Create a list of letters based on their frequency in the letter pool + letter_list = [] + for letter, count in LETTER_POOL.items(): + letter_list.extend([letter] * count) -def score_word(word): - pass + # Draw 10 random letters from the letter list + hand = random.sample(letter_list, 10) + + # Return the hand + return hand -def get_highest_word_score(word_list): - pass \ No newline at end of file diff --git a/adagrams_wave_1.py b/adagrams_wave_1.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index ef48e03b..8312e8b5 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -1,3 +1,4 @@ + import pytest from adagrams.game import draw_letters diff --git a/tests/test_wave_04.py b/tests/test_wave_04.py index e586621c..3ec5d7eb 100644 --- a/tests/test_wave_04.py +++ b/tests/test_wave_04.py @@ -73,9 +73,9 @@ def test_get_highest_word_tie_prefers_ten_letters_unsorted_list(): assert best_word[0] == "AAAAAAAAAA" assert best_word[1] == 18 -def test_get_highest_word_tie_same_length_prefers_first(): # Arrange words = ["AAAAAAAAAA", "EEEEEEEEEE"] +def test_get_highest_word_tie_same_length_prefers_first(): # Act best_word = get_highest_word_score(words) From 7af2d6d8795e668adafb5b825087c28e8ee011aa Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Wed, 22 Mar 2023 22:21:29 -0700 Subject: [PATCH 2/8] wave 1 passed this is so cool --- adagrams/game.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adagrams/game.py b/adagrams/game.py index d6761704..3e249b81 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -25,3 +25,6 @@ def draw_letters(): # Return the hand return hand + + + From 4c9dabab322d3d24d060b51b85f31e6b61447218 Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Wed, 22 Mar 2023 22:56:21 -0700 Subject: [PATCH 3/8] wave 2 test passed here we go --- adagrams/game.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/adagrams/game.py b/adagrams/game.py index 3e249b81..6ad6877c 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -25,6 +25,27 @@ def draw_letters(): # Return the hand return hand +def score_word(word): + word = word.upper() + letter_scores = { + "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 + } + +def uses_available_letters(word, letter_bank): + word = word.upper() + letter_bank_copy = letter_bank.copy() + for letter in word: + if letter in letter_bank_copy: + letter_bank_copy.remove(letter) + else: + return False + return True From f3c80b680c7bc7ca996d1ad2152863b4bb6ea8b3 Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Wed, 22 Mar 2023 23:02:38 -0700 Subject: [PATCH 4/8] wave 3 test passed only one more to go --- adagrams/game.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 6ad6877c..d660cd0c 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -25,6 +25,19 @@ def draw_letters(): # Return the hand return hand + +def uses_available_letters(word, letter_bank): + word = word.upper() + letter_bank_copy = letter_bank.copy() + + for letter in word: + if letter in letter_bank_copy: + letter_bank_copy.remove(letter) + else: + return False + + return True + def score_word(word): word = word.upper() letter_scores = { @@ -37,15 +50,11 @@ def score_word(word): "Q": 10, "Z": 10 } -def uses_available_letters(word, letter_bank): - word = word.upper() - letter_bank_copy = letter_bank.copy() - + score = 0 for letter in word: - if letter in letter_bank_copy: - letter_bank_copy.remove(letter) - else: - return False + score += letter_scores[letter] - return True + if 7 <= len(word) <= 10: + score += 8 + return score \ No newline at end of file From 90b09c83e0c6b1a6ef9c41cdbb5afaa1f975e176 Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Thu, 23 Mar 2023 21:43:40 -0700 Subject: [PATCH 5/8] wave 4 has passed. code comments are still being added --- adagrams/game.py | 70 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index d660cd0c..7283c0b7 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,41 +1,39 @@ -# def draw_letters(): -# def uses_available_letters(word, letter_bank): -# def score_word(word): -# def get_highest_word_score(word_list): - import random -# Function to draw random letters +# Function to draw 10 random letters from the letter pool + def draw_letters(): - # Define the letter pool 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 } - - # Create a list of letters based on their frequency in the letter pool + + # Create a list of letters with the frequency as specified in the pool letter_list = [] for letter, count in LETTER_POOL.items(): letter_list.extend([letter] * count) - # Draw 10 random letters from the letter list + # Draw a random sample of 10 letters from the letter list hand = random.sample(letter_list, 10) - # Return the hand return hand - +# Function to check if the given word can be formed using the available letters def uses_available_letters(word, letter_bank): word = word.upper() + # Create a copy of the letter bank to avoid modifying the original + letter_bank_copy = letter_bank.copy() + # Iterate through each letter in the word for letter in word: if letter in letter_bank_copy: letter_bank_copy.remove(letter) else: return False + # If all letters are in the letter bank copy, the word is valid return True def score_word(word): @@ -57,4 +55,50 @@ def score_word(word): if 7 <= len(word) <= 10: score += 8 - return score \ No newline at end of file + return score + +def get_highest_word_score(word_list): + word_scores = [(word, score_word(word)) for word in word_list] + highest_word = word_scores[0] + + for word, score in word_scores[1:]: + if score > highest_word[1] or ( + score == highest_word[1] and + (len(word) == 10 and len(highest_word[0]) != 10) or + (len(highest_word[0]) != 10 and len(word) < len(highest_word[0])) + ): + highest_word = (word, score) + elif score == highest_word[1] and len(word) == len(highest_word[0]): + highest_word = (word_list.index(word) < word_list.index(highest_word[0])) and (word, score) or highest_word + + return highest_word + + +def play_game(): + all_words = [] + + for i in range(4): + hand = draw_letters() + print(f"Hand {i + 1}: {hand}") + + word = input("Enter a word using the available letters: ") + + if uses_available_letters(word, hand): + score = score_word(word) + print(f"The word '{word}' is valid and has a score of {score}.") + all_words.append(word) + else: + print(f"The word '{word}' is not valid.") + + result = get_highest_word_score(all_words) + if result: + highest_word, highest_score = result + print(f"The highest scoring word is '{highest_word}' with a score of {highest_score}.") +if __name__ == "__main__": + play_game() + + + + + + From 601d472692aed8100ac109fb1f377e5e38536372 Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Thu, 23 Mar 2023 21:48:47 -0700 Subject: [PATCH 6/8] test had indent error, line 76 or 78 word variable was slightly out of order --- tests/test_wave_04.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_wave_04.py b/tests/test_wave_04.py index 3ec5d7eb..546b23de 100644 --- a/tests/test_wave_04.py +++ b/tests/test_wave_04.py @@ -73,10 +73,10 @@ def test_get_highest_word_tie_prefers_ten_letters_unsorted_list(): assert best_word[0] == "AAAAAAAAAA" assert best_word[1] == 18 - # Arrange - words = ["AAAAAAAAAA", "EEEEEEEEEE"] + def test_get_highest_word_tie_same_length_prefers_first(): - + # Arrange + words = ["AAAAAAAAAA", "EEEEEEEEEE"] # Act best_word = get_highest_word_score(words) From fc85bc2b99f3519b0167d06c8dd213f3d0e433ab Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Fri, 24 Mar 2023 03:32:49 -0700 Subject: [PATCH 7/8] all tests actually passed previouse version false --- adagrams/game.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 7283c0b7..e5b8af19 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -63,17 +63,23 @@ def get_highest_word_score(word_list): for word, score in word_scores[1:]: if score > highest_word[1] or ( - score == highest_word[1] and - (len(word) == 10 and len(highest_word[0]) != 10) or - (len(highest_word[0]) != 10 and len(word) < len(highest_word[0])) + score == highest_word[1] and ( + (len(word) == 10 and len(highest_word[0]) != 10) or + (len(highest_word[0]) != 10 and len(word) < len(highest_word[0])) + ) ): highest_word = (word, score) elif score == highest_word[1] and len(word) == len(highest_word[0]): - highest_word = (word_list.index(word) < word_list.index(highest_word[0])) and (word, score) or highest_word + if word_list.index(word) < word_list.index(highest_word[0]): + highest_word = (word, score) return highest_word + + + + def play_game(): all_words = [] From b1f8634345a868bf22968410bbca6d5a89ba232f Mon Sep 17 00:00:00 2001 From: Moyo Moz Date: Fri, 24 Mar 2023 03:51:56 -0700 Subject: [PATCH 8/8] comments added explaining logic --- adagrams/game.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index e5b8af19..294a981f 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -58,46 +58,65 @@ def score_word(word): return score def get_highest_word_score(word_list): + # Calculate the score for each word in the word_list and store them as tuples word_scores = [(word, score_word(word)) for word in word_list] + + # Initialize the highest_word variable with the first word and its score highest_word = word_scores[0] + # Iterate through the rest of the word_scores list (skipping the first element) for word, score in word_scores[1:]: + # Check if the current word has a higher score than the highest_word if score > highest_word[1] or ( + # If the scores are equal, apply tie-breaking rules: score == highest_word[1] and ( + # If the current word has 10 letters and the highest_word doesn't, choose the current word (len(word) == 10 and len(highest_word[0]) != 10) or + # If neither word has 10 letters, choose the word with fewer letters (len(highest_word[0]) != 10 and len(word) < len(highest_word[0])) ) ): - highest_word = (word, score) + # Update the highest_word with the current word and its score + highest_word = (word, score) + # If the scores are equal and the word lengths are also equal, use the order in the supplied list elif score == highest_word[1] and len(word) == len(highest_word[0]): - if word_list.index(word) < word_list.index(highest_word[0]): - highest_word = (word, score) + # Check the index of the current word and highest_word in the original list + # If the index of the current word is lower, update highest_word with the current word and its score + highest_word = (word_list.index(word) < word_list.index(highest_word[0])) and (word, score) or highest_word + # Return the highest_word tuple containing the winning word and its score return highest_word - - - - - def play_game(): + # Initialize an empty list to store all the words played all_words = [] + # Play 4 rounds of the game for i in range(4): + # Draw a new hand of 10 letters hand = draw_letters() + # Print the current hand print(f"Hand {i + 1}: {hand}") + # Ask the user to input a word using the available letters word = input("Enter a word using the available letters: ") + # Check if the input word is valid using the available letters if uses_available_letters(word, hand): + # Calculate the score of the input word score = score_word(word) + # Print the valid word and its score print(f"The word '{word}' is valid and has a score of {score}.") + # Add the valid word to the list of all_words all_words.append(word) else: + # Print an error message if the input word is not valid print(f"The word '{word}' is not valid.") - + + # Calculate the highest scoring word and its score result = get_highest_word_score(all_words) if result: + # result tuple into highest_word and highest_score variables highest_word, highest_score = result print(f"The highest scoring word is '{highest_word}' with a score of {highest_score}.") if __name__ == "__main__":