From 2d750ed9059253ed3eda2189c357a9de779a48a6 Mon Sep 17 00:00:00 2001 From: Shaina Beth Date: Wed, 22 Sep 2021 14:20:22 -0700 Subject: [PATCH 1/5] completed draw_letters function by randomly choosing a letter and adding it to the returned draw_ten list --- adagrams/game.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..dc05a40e 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,5 +1,57 @@ +import random + def draw_letters(): - pass + pool_of_letters = { + '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 + } + + list_of_letters = list(pool_of_letters.keys()) + + draw_ten = [] + + + + + for i in range(10): + random_letter = random.randint(0, 25) + letter = list_of_letters[random_letter] + + while pool_of_letters[letter] == 0: + random_letter = random.randint(0, 25) + letter = list_of_letters[random_letter] + + draw_ten.append(letter) + pool_of_letters[letter] -= 1 + + + return draw_ten + + def uses_available_letters(word, letter_bank): pass From 6738500c305a9162b376837c16b4a501dbbd004d Mon Sep 17 00:00:00 2001 From: Ngozi Amaefule Date: Wed, 22 Sep 2021 21:49:23 -0400 Subject: [PATCH 2/5] Wave 02 complete; all tests passed --- adagrams/game.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index dc05a40e..c0736cc7 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -54,7 +54,32 @@ def draw_letters(): def uses_available_letters(word, letter_bank): - pass + letter_list = letter_bank[:] + if len(word) > len(letter_list): + return False + + elif len(word) == len(letter_list): + for letter in word: + if letter in letter_list: + letter_list.remove(letter) + if letter_list is False: + return True + else: + return False + + else: + whole_word = [] + for letter in word: + if letter not in letter_list: + return False + else: + whole_word.append(letter) + letter_list.remove(letter) + if len(whole_word) == len(word): + return True + else: + return False + def score_word(word): pass From 82e5da0fd3d6f513c96cdf162f08cd994d8ee5bc Mon Sep 17 00:00:00 2001 From: Shaina Beth Date: Fri, 24 Sep 2021 16:46:19 -0700 Subject: [PATCH 3/5] Completed Wave 2 by adding a scoring system that returns the sum of the total value of the word --- adagrams/game.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index c0736cc7..b51a0ac1 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -79,10 +79,42 @@ def uses_available_letters(word, letter_bank): return True else: return False + def score_word(word): - pass + value_1 = ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"] + value_2 = ["D", "G"] + value_3 = ["B", "C", "M", "P"] + value_4 = ["F", "H", "V", "W", "Y"] + value_5 = ["K"] + value_8 = ["J", "X"] + value_10 = ["Q", "Z"] + + sum = 0 + + if len(word) > 6: + sum += 8 + + for letter in word.upper(): + if letter in value_1: + sum += 1 + elif letter in value_2: + sum += 2 + elif letter in value_3: + sum += 3 + elif letter in value_4: + sum += 4 + elif letter in value_5: + sum += 5 + elif letter in value_8: + sum += 8 + elif letter in value_10: + sum += 10 + + return sum + + def get_highest_word_score(word_list): pass \ No newline at end of file From 6a9cedc2bf2f79e8a48ed2b40daa5520fc6a0012 Mon Sep 17 00:00:00 2001 From: Ngozi Amaefule Date: Sat, 25 Sep 2021 12:16:56 -0400 Subject: [PATCH 4/5] Wave 04 Complete; passed all tests. Invoked function from Wave 03 to do so. --- adagrams/game.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index b51a0ac1..ba510e69 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -117,4 +117,30 @@ def score_word(word): def get_highest_word_score(word_list): - pass \ No newline at end of file + score_dict = {} + max_score = 0 + high_score_words = {} + + for word in word_list: + score_dict[word] = score_word(word) + + for score in score_dict.values(): + if score > max_score: + max_score = score + + for word, score in score_dict.items(): + if score == max_score: + high_score_words[word] = score + + shortest_word_length = 10 + + for word, score in high_score_words.items(): + if len(word) == 10: + return word, score + + elif len(word) < shortest_word_length: + shortest_word_length = len(word) + + for word, score in high_score_words.items(): + if len(word) == shortest_word_length: + return word, score From 2470532e179d5af14d77f2c42ff2f0182808fd46 Mon Sep 17 00:00:00 2001 From: Ngozi Amaefule Date: Mon, 11 Oct 2021 17:08:15 -0400 Subject: [PATCH 5/5] Refactoring complete --- adagrams/game.py | 146 +++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index ba510e69..4f9517ce 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,7 +1,6 @@ import random -def draw_letters(): - pool_of_letters = { +POOL_OF_LETTERS = { 'A': 9, 'B': 2, 'C': 2, @@ -30,20 +29,26 @@ def draw_letters(): 'Z': 1 } - list_of_letters = list(pool_of_letters.keys()) - - draw_ten = [] +# Wave 01 +""" +The draw_letters function builds a hand of 10 letters +for the user. The letters are randomly drawn from the +pool of letters. +""" +def draw_letters(): + pool_of_letters = POOL_OF_LETTERS.copy() + list_of_letters = list(pool_of_letters.keys()) + draw_ten = [] - for i in range(10): - random_letter = random.randint(0, 25) + while len(draw_ten) < 10: + random_letter = random.randint(0, len(list_of_letters) - 1) letter = list_of_letters[random_letter] - while pool_of_letters[letter] == 0: - random_letter = random.randint(0, 25) - letter = list_of_letters[random_letter] + if pool_of_letters[letter] == 0: + continue draw_ten.append(letter) pool_of_letters[letter] -= 1 @@ -51,96 +56,91 @@ def draw_letters(): return draw_ten +# Wave 02 +""" +The use_available_letters function checks to see +whether or not the input word only uses characters +that are contained within a collection (or hand) +of drawn letters. +""" def uses_available_letters(word, letter_bank): letter_list = letter_bank[:] + letter_map = {} + if len(word) > len(letter_list): return False - elif len(word) == len(letter_list): - for letter in word: - if letter in letter_list: - letter_list.remove(letter) - if letter_list is False: - return True - else: - return False - - else: - whole_word = [] - for letter in word: - if letter not in letter_list: - return False - else: - whole_word.append(letter) - letter_list.remove(letter) - if len(whole_word) == len(word): - return True + whole_word = [] + for letter in letter_list: + if letter in letter_map: + letter_map[letter] += 1 else: + letter_map[letter] = 1 + + for letter in word: + if letter not in letter_map.keys() or letter_map[letter] == 0: return False - + + whole_word.append(letter) + letter_map[letter] -= 1 + + return len(whole_word) == len(word) +# Wave 03 + +""" +The score_word function returns the score of a given word as +defined by the Adagrams game. +""" def score_word(word): - value_1 = ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"] - value_2 = ["D", "G"] - value_3 = ["B", "C", "M", "P"] - value_4 = ["F", "H", "V", "W", "Y"] - value_5 = ["K"] - value_8 = ["J", "X"] - value_10 = ["Q", "Z"] + score_map = { + "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 + } - sum = 0 + LONG_WORD_BONUS = 8 + LONG_WORD_BONUS_LENGTH = 7 + BASIC_SCORE = 0 - if len(word) > 6: - sum += 8 + score = LONG_WORD_BONUS if len(word) >= LONG_WORD_BONUS_LENGTH else BASIC_SCORE for letter in word.upper(): - if letter in value_1: - sum += 1 - elif letter in value_2: - sum += 2 - elif letter in value_3: - sum += 3 - elif letter in value_4: - sum += 4 - elif letter in value_5: - sum += 5 - elif letter in value_8: - sum += 8 - elif letter in value_10: - sum += 10 + if letter in score_map: + score += score_map[letter] - return sum + return score +# Wave 04 +""" +The get_highest_word_score function checks for the +highest scored word that the user has submitted. +""" def get_highest_word_score(word_list): score_dict = {} max_score = 0 - high_score_words = {} for word in word_list: score_dict[word] = score_word(word) - for score in score_dict.values(): - if score > max_score: - max_score = score - - for word, score in score_dict.items(): - if score == max_score: - high_score_words[word] = score - + max_score = max(score_dict.values()) shortest_word_length = 10 + shortest_word = None - for word, score in high_score_words.items(): - if len(word) == 10: - return word, score - - elif len(word) < shortest_word_length: - shortest_word_length = len(word) + for word, score in score_dict.items(): + if score == max_score: + if len(word) == 10: + return word, score + elif len(word) < shortest_word_length: + shortest_word_length = len(word) + shortest_word = word - for word, score in high_score_words.items(): - if len(word) == shortest_word_length: - return word, score + return shortest_word, max_score \ No newline at end of file