From 1750352ebb2f5f403d15a38af9a5c911212890e9 Mon Sep 17 00:00:00 2001 From: Nicole Washington Date: Mon, 20 Sep 2021 23:01:03 -0700 Subject: [PATCH 1/9] Added draw_letters functionfor wave 01, with an implementation that passses wave 01 test --- adagrams/game.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..63968d86 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,5 +1,17 @@ +import random + def draw_letters(): - pass + letter_pool = ['A','A','A','A','A','A','A','A','A', + 'B','B', 'C','C', 'D','D','D','D', + 'E','E','E','E','E','E','E','E','E','E','E','E', + 'F','F','G','G','G', 'H','H', 'I','I','I','I','I','I','I','I','I', + 'J', 'K', 'L','L','L','L', 'M','M', 'N','N','N','N','N','N', + 'O','O','O','O','O','O','O','O', 'P','P', 'Q', 'R','R','R','R','R','R', + 'S','S','S','S', 'T','T','T','T','T','T', 'U','U','U','U', + 'V','V', 'W','W', 'X', 'Y','Y','Z'] + player_hand = random.sample(letter_pool, k=10) + return player_hand + def uses_available_letters(word, letter_bank): pass From 619d18c191b9809afd643179d83c214f996b0e55 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 21 Sep 2021 21:21:43 -0400 Subject: [PATCH 2/9] added draw_letters, passed all tests in wave 1 --- adagrams/game.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..306916cf 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,5 +1,101 @@ +import random + +def build_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 +} + return letter_pool + +def build_letter_pool_list(letter_pool): + list_letter_pool = [] + for letter, frequency in letter_pool.items(): + for i in range(frequency): + list_letter_pool.append(letter) + return list_letter_pool + def draw_letters(): - pass + # Building a dictionary instead of hard coding into a list directly + # Allows for efficiency when we ever need to change letter pool later + letter_pool_dict = build_letter_pool() + # Convert into a list to ensure weighted probability + letter_pool_list = build_letter_pool_list(letter_pool_dict) + + letters = [] + while len(letters) < 10: + chosen_letter = random.choice(letter_pool_list) + letter_pool_list.remove(chosen_letter) + letters.append(chosen_letter) + return letters + +# def build_letter_pool(): +# letter_pool = { +# "A": ["A" for i in range(9)], +# "B": ["B" for i in range (2)], +# "C": ["C" for i in range (2)], +# "D": ["D" for i in range (4)], +# "E": ["E" for i in range (12)], +# "F": ["F" for i in range (2)], +# "G": ["G" for i in range (3)], +# "H": ["H" for i in range (2)], +# "I": ["I" for i in range (9)], +# "J": ["J" for i in range (1)], +# "K": ["K" for i in range (1)], +# "L": ["L" for i in range (4)], +# "M": ["M" for i in range (2)], +# "N": ["N" for i in range (6)], +# "O": ["O" for i in range (8)], +# "P": ["P" for i in range (2)], +# "Q": ["Q" for i in range (1)], +# "R": ["R" for i in range (6)], +# "S": ["S" for i in range (4)], +# "T": ["T" for i in range (6)], +# "U": ["U" for i in range (6)], +# "V": ["V" for i in range (2)], +# "W": ["W" for i in range (2)], +# "X": ["X" for i in range (1)], +# "Y": ["Y" for i in range (2)], +# "Z": ["Z" for i in range (1)] } +# return letter_pool + +# def draw_letters(): +# letter_pool = build_letter_pool() + +# list_letter_pool = [] +# for letter_frequencies in letter_pool.values(): +# for i in range(len(letter_frequencies)): +# list_letter_pool.append(letter_frequencies[i]) + +# letters = [] +# while len(letters) < 10: +# chosen_letter = random.choice(list_letter_pool) +# list_letter_pool.remove(chosen_letter) +# letters.append(chosen_letter) +# return letters def uses_available_letters(word, letter_bank): pass From e4f2eaf124354cb82665664a7584dd261c3c5478 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 21 Sep 2021 22:17:00 -0400 Subject: [PATCH 3/9] added use_available_letters, passed all tests in wave 2 --- adagrams/game.py | 58 ++++++++++-------------------------------------- 1 file changed, 12 insertions(+), 46 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 306916cf..010d75fd 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -52,53 +52,19 @@ def draw_letters(): letters.append(chosen_letter) return letters -# def build_letter_pool(): -# letter_pool = { -# "A": ["A" for i in range(9)], -# "B": ["B" for i in range (2)], -# "C": ["C" for i in range (2)], -# "D": ["D" for i in range (4)], -# "E": ["E" for i in range (12)], -# "F": ["F" for i in range (2)], -# "G": ["G" for i in range (3)], -# "H": ["H" for i in range (2)], -# "I": ["I" for i in range (9)], -# "J": ["J" for i in range (1)], -# "K": ["K" for i in range (1)], -# "L": ["L" for i in range (4)], -# "M": ["M" for i in range (2)], -# "N": ["N" for i in range (6)], -# "O": ["O" for i in range (8)], -# "P": ["P" for i in range (2)], -# "Q": ["Q" for i in range (1)], -# "R": ["R" for i in range (6)], -# "S": ["S" for i in range (4)], -# "T": ["T" for i in range (6)], -# "U": ["U" for i in range (6)], -# "V": ["V" for i in range (2)], -# "W": ["W" for i in range (2)], -# "X": ["X" for i in range (1)], -# "Y": ["Y" for i in range (2)], -# "Z": ["Z" for i in range (1)] } -# return letter_pool - -# def draw_letters(): -# letter_pool = build_letter_pool() - -# list_letter_pool = [] -# for letter_frequencies in letter_pool.values(): -# for i in range(len(letter_frequencies)): -# list_letter_pool.append(letter_frequencies[i]) - -# letters = [] -# while len(letters) < 10: -# chosen_letter = random.choice(list_letter_pool) -# list_letter_pool.remove(chosen_letter) -# letters.append(chosen_letter) -# return letters - def uses_available_letters(word, letter_bank): - pass + for char in word: + if char in letter_bank: + letter_bank.remove(char) + else: + return False + + # Adds back used letters do letter_bank is not changed + i = 0 + for char in word: + letter_bank.insert(i, char) + i += 1 + return True def score_word(word): pass From 4b5f82d39631ab5cbfbea02f796af705fa22922b Mon Sep 17 00:00:00 2001 From: Nicole Washington Date: Tue, 21 Sep 2021 23:11:11 -0700 Subject: [PATCH 4/9] Added function for wave 02, function is passing wave 02 test --- adagrams/game.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 63968d86..0dda3e4e 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -9,12 +9,20 @@ def draw_letters(): 'O','O','O','O','O','O','O','O', 'P','P', 'Q', 'R','R','R','R','R','R', 'S','S','S','S', 'T','T','T','T','T','T', 'U','U','U','U', 'V','V', 'W','W', 'X', 'Y','Y','Z'] - player_hand = random.sample(letter_pool, k=10) + # random sample function returns list of K size + player_hand = random.sample(letter_pool, k=10) return player_hand def uses_available_letters(word, letter_bank): - pass + invalid = [] + for letter in word: + if letter not in letter_bank or (word.count(letter) > letter_bank.count(letter)): + invalid.append(letter) + if len(invalid) > 0: + return False + else: + return True def score_word(word): pass From 0197b73b63ff95c6feed666fe293d3d6d50b466c Mon Sep 17 00:00:00 2001 From: Nicole Washington Date: Wed, 22 Sep 2021 01:04:26 -0700 Subject: [PATCH 5/9] Added function for wave 3, function implementation is passing wave 03 test --- adagrams/game.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index 0dda3e4e..cbb6a5af 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -25,7 +25,27 @@ def uses_available_letters(word, letter_bank): return True def score_word(word): - pass + + letter_values = { + 1:['A','E','I','O','U','L','N','R','S','T'], + 2:['D','G'], + 3:['B','C','M','P'], + 4:['F','H','V','W','Y'], + 5:['K'], + 8:['J','X'], + 10:['Q','Z'], + } + lng_wrd_pnts = 8 + score = 0 + cap_word = word.upper() + for letter in cap_word: + for key,val in letter_values.items(): + if letter in val: + score += key + if len(cap_word) >= 7 and len(cap_word) <= 10: + score += lng_wrd_pnts + + return score def get_highest_word_score(word_list): pass \ No newline at end of file From 5a9c3d5a1185fc78097429d0715d470d9fac9a11 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Thu, 23 Sep 2021 01:55:16 -0400 Subject: [PATCH 6/9] added score_word, passed all tests in wave 3 --- adagrams/game.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index 010d75fd..c950b002 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -66,8 +66,52 @@ def uses_available_letters(word, letter_bank): i += 1 return True +def build_score_chart(): + score_chart = { + "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 + } + return score_chart + def score_word(word): - pass + score_chart = build_score_chart() + + score = 0 + cap_word = word.upper() + + for char in cap_word: + if not char: + return 0 + else: + point = score_chart[char] + score += point + if 7 <= len(cap_word) <=10: + return score + 8 + return score def get_highest_word_score(word_list): pass \ No newline at end of file From f709fd07ef775db07f70075aa693ed3b3dfb1168 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Fri, 24 Sep 2021 18:44:13 -0400 Subject: [PATCH 7/9] merged conflict resolve after pull, refactored wave functions 1-3 --- adagrams/game.py | 2 +- main.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 0d8e14b7..99c61c10 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -90,6 +90,7 @@ def build_score_chart(): } return score_chart + def score_word(word): score_chart = build_score_chart() score = 0 @@ -106,6 +107,5 @@ def score_word(word): return score - def get_highest_word_score(word_list): pass \ No newline at end of file diff --git a/main.py b/main.py index d583cfd9..081cd8ab 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ def wave_1_run_game(): display_retry_instructions() continue_input = input() game_continue = continue_input == "y" - + display_goodbye_message() def wave_2_run_game(): @@ -34,7 +34,7 @@ def wave_2_run_game(): display_retry_instructions() continue_input = input() game_continue = continue_input == "y" - + display_goodbye_message() def wave_3_run_game(): From 6c69a0829ca05c0a6cb17814c7c9794c08f77411 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Fri, 24 Sep 2021 22:59:26 -0400 Subject: [PATCH 8/9] worked with nicole to pass all tests for wave 4 --- adagrams/game.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/adagrams/game.py b/adagrams/game.py index 99c61c10..211a8daf 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -108,4 +108,61 @@ def score_word(word): def get_highest_word_score(word_list): - pass \ No newline at end of file + points_list = [] + + for word in word_list: + points = score_word(word) + points_list.append(points) + + highest_points = max(points_list) + # Highest_points_index and highest_word are for no tie cases + highest_points_index = points_list.index(highest_points) + highest_word = word_list[highest_points_index] + + # Tie case variable + index_list = [] + high_word_list = [] + winning_word = "" + + if points_list.count(highest_points) == 1: + return highest_word,highest_points + else: + high_word_list = get_high_word_list(points_list, word_list) + print(f"{high_word_list=}") + + ten_letters = [] + for word in high_word_list: + if len(word) == 10: + ten_letters.append(word) + winning_word = ten_letters[0] + if len(ten_letters) >= 1: + break + else: + short_word_list = find_shortest_length(high_word_list) + if len(short_word_list) == 1: + winning_word = short_word_list[0] + else: + # When we have more than one word + winning_word = short_word_list[0] + + return winning_word, highest_points + +def get_high_word_list(points_list, word_list): + index_list = [] + high_word_list = [] + for i in range(len(points_list)): + if points_list[i] == max(points_list): + index_list.append(i) + # Need to use index list to get word + for index_value in index_list: + high_word_list.append((word_list[index_value])) + return high_word_list + +def find_shortest_length(high_word_list): + shortest_word = min(high_word_list, key=len) + short_list = [] + + for word in high_word_list: + if len(word) == len(shortest_word): + short_list.append(word) + return short_list From ee0d782c18b50a2f96fdbd47f9d4261ed2e562cb Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sun, 26 Sep 2021 23:05:44 -0400 Subject: [PATCH 9/9] refactored wave 4 with nicole, added docstrings for all 4 waves, passed all tests --- adagrams/game.py | 138 ++++++++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 55 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 211a8daf..b79c144b 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -3,7 +3,11 @@ LONG_WORD_MAX = 10 LONG_WORD_POINTS = 8 +# Wave 1 def build_letter_pool(): + """This function returns a dictionary of the letter pool + by assigning the letter as the key and the distribution of each + corresponding letter as the value.""" letter_pool = { 'A': 9, 'B': 2, @@ -35,6 +39,9 @@ def build_letter_pool(): return letter_pool def build_letter_pool_list(letter_pool): + """This function takes the dictionary of the letter pool and returns + a list of strings that represents the distribution of letters in the + letter pool.""" list_letter_pool = [] for letter, frequency in letter_pool.items(): for i in range(frequency): @@ -42,14 +49,21 @@ def build_letter_pool_list(letter_pool): return list_letter_pool def draw_letters(): + """This function returns an array of ten strings, where each string + represents a letter and the array represents the hand of letters the + player has drawn.""" letter_pool_dict = build_letter_pool() # Convert into a list to ensure weighted probability letter_pool_list = build_letter_pool_list(letter_pool_dict) - # random sample function returns list of K size + # Random sample function returns list of K size player_hand = random.sample(letter_pool_list, k=10) return player_hand +# Wave 2 def uses_available_letters(word, letter_bank): + """This function returns True if every letter in the input word + is available in the right quantities from the letter bank. Otherwise, + it returns False.""" letter_bank_copy = letter_bank.copy() for char in word: if char in letter_bank_copy: @@ -60,6 +74,9 @@ def uses_available_letters(word, letter_bank): def build_score_chart(): + """This function returns a dictionary of the score chart, by + assigning the letter as the key and the number of points for each + corresponding letter as the value.""" score_chart = { "A": 1, "E": 1, @@ -90,8 +107,9 @@ def build_score_chart(): } return score_chart - def score_word(word): + """This function returns an integer that represents the number of points + awarded for a given word""" score_chart = build_score_chart() score = 0 cap_word = word.upper() @@ -106,63 +124,73 @@ def score_word(word): return score + LONG_WORD_POINTS return score - def get_highest_word_score(word_list): - points_list = [] - - for word in word_list: - points = score_word(word) - points_list.append(points) + """This function returns a tuple that represents the data of a + winning word and its respective score given a list of words. This function + also applies tie-breaking logic to determine the winning word and score""" + all_points = get_all_points(word_list) + max_score = max(all_points) + index_value = None + + if not check_if_tie_case(all_points, word_list): + index_value = get_index_from_list(all_points, max_score) + return word_list[index_value], max_score - highest_points = max(points_list) - # Highest_points_index and highest_word are for no tie cases - highest_points_index = points_list.index(highest_points) - highest_word = word_list[highest_points_index] + # All code below refer to tie-breaking cases + # When all words have different lengths but none of the words has 10 letters + if not check_if_same_length(word_list) and not check_if_any_word_is_ten(word_list): + word_lengths = get_word_lengths(word_list) + shortest_word = min(word_lengths) + index_value = get_index_from_list(word_lengths, shortest_word) + return word_list[index_value], max_score + + # When we have a 10-letter word, return the first 10-letter word in the list + # Combines second and third conditions for tie-breaking rules + if check_if_any_word_is_ten(word_list): + word_lengths = get_word_lengths(word_list) + index_value = get_index_from_list(word_lengths, 10) + return word_list[index_value], max_score - # Tie case variable - index_list = [] - high_word_list = [] - winning_word = "" +# Helper functions for wave 4 +def get_all_points(word_list): + """This function returns a list of integers, with + each integer representing the score for each word in the + list of words""" + all_points = [score_word(word) for word in word_list] + return all_points - if points_list.count(highest_points) == 1: - return highest_word,highest_points - else: - high_word_list = get_high_word_list(points_list, word_list) - print(f"{high_word_list=}") - - ten_letters = [] - for word in high_word_list: - if len(word) == 10: - ten_letters.append(word) - winning_word = ten_letters[0] - if len(ten_letters) >= 1: - break - else: - short_word_list = find_shortest_length(high_word_list) - if len(short_word_list) == 1: - winning_word = short_word_list[0] - else: - # When we have more than one word - winning_word = short_word_list[0] - - return winning_word, highest_points +def check_if_tie_case(all_points, word_list): + """This function returns True when there is a tie. + Otherwise, the function returns False.""" + if len(set(all_points)) != len(word_list): + return True + return False + +def get_index_from_list(any_list, any_value): + """This function returns the index of any_value from + any_list""" + for i in range(len(any_list)): + if any_list[i] == any_value: + return i -def get_high_word_list(points_list, word_list): - index_list = [] - high_word_list = [] - for i in range(len(points_list)): - if points_list[i] == max(points_list): - index_list.append(i) - # Need to use index list to get word - for index_value in index_list: - high_word_list.append((word_list[index_value])) - return high_word_list +def get_word_lengths(word_list): + """This function return a list of integers, with each + integer as the length of each word""" + word_lengths = [len(word) for word in word_list] + return word_lengths -def find_shortest_length(high_word_list): - shortest_word = min(high_word_list, key=len) - short_list = [] +def check_if_same_length(word_list): + """Returns true if all words from a list of words are + the same lengths. Otherwise, the function returns False.""" + word_length = get_word_lengths(word_list) + if len(set(word_length)) != len(word_list): + return True + return False - for word in high_word_list: - if len(word) == len(shortest_word): - short_list.append(word) - return short_list +def check_if_any_word_is_ten(word_list): + """ This function returns true if any word from a list of + words has 10 letters""" + word_length = get_word_lengths(word_list) + if 10 in word_length: + return True + return False \ No newline at end of file