From c2e9f5be9543f92d910a2fa32bfefbb8ec188239 Mon Sep 17 00:00:00 2001 From: Elly Wong Date: Mon, 20 Sep 2021 11:57:22 -0700 Subject: [PATCH 1/6] Finished Wave 1 --- adagrams/game.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..38e85dc8 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,5 +1,9 @@ +import random + def draw_letters(): - pass + list_letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] + return random.choices(list_letters, weights = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1], k = 10) + def uses_available_letters(word, letter_bank): pass @@ -8,4 +12,7 @@ def score_word(word): pass def get_highest_word_score(word_list): - pass \ No newline at end of file + pass + + +print(draw_letters()) \ No newline at end of file From 733b82d0e46e3dd308d84011409c90adc7516d85 Mon Sep 17 00:00:00 2001 From: Elly Wong Date: Mon, 20 Sep 2021 19:15:10 -0700 Subject: [PATCH 2/6] Finished wave 1 --- adagrams/game.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 38e85dc8..99fd4350 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,8 +1,82 @@ import random +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 +} + def draw_letters(): - list_letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] - return random.choices(list_letters, weights = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1], k = 10) + list_letters_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"] + list_letters_drawn = [] + for i in range (10): + random_letter = random.choice(list_letters_pool) + list_letters_drawn.append(random_letter) + list_letters_pool.remove(random_letter) + return list_letters_drawn + + + # Alternative Approach to Wave 1 (not yet finished) + # random_list_letters = random.choices(list_letters, weights = [9, 2, 2, 4, 12, 2, 3, 2, 9, + # 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, + # 2, 2, 1, 2, 1], k = 10) + # random_dict_letter_occurence = {} + + # for each_letter in random_list_letters: + # if each_letter not in random_dict_letter_occurence: + # random_dict_letter_occurence[each_letter] = 1 + # else: + # random_dict_letter_occurence[each_letter] += 1 + + def uses_available_letters(word, letter_bank): @@ -15,4 +89,3 @@ def get_highest_word_score(word_list): pass -print(draw_letters()) \ No newline at end of file From f89873eeb0b32d1d14439ca23160a15ef4a73f49 Mon Sep 17 00:00:00 2001 From: Elly Wong Date: Mon, 20 Sep 2021 23:23:20 -0700 Subject: [PATCH 3/6] Finished and passed all tests, waiting for refactoring --- adagrams/game.py | 129 ++++++++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 51 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 99fd4350..fc0bfdc5 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,35 +1,6 @@ import random -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 -} - -def draw_letters(): +def draw_letters(): # Time complexity: O(1)? Space complexity: O(1)? list_letters_pool = ["A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "C", "C", @@ -60,32 +31,88 @@ def draw_letters(): for i in range (10): random_letter = random.choice(list_letters_pool) list_letters_drawn.append(random_letter) - list_letters_pool.remove(random_letter) + list_letters_pool.remove(random_letter) # remove the drawn letter from the original list of letter pool after each random draw return list_letters_drawn - - # Alternative Approach to Wave 1 (not yet finished) - # random_list_letters = random.choices(list_letters, weights = [9, 2, 2, 4, 12, 2, 3, 2, 9, - # 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, - # 2, 2, 1, 2, 1], k = 10) - # random_dict_letter_occurence = {} - - # for each_letter in random_list_letters: - # if each_letter not in random_dict_letter_occurence: - # random_dict_letter_occurence[each_letter] = 1 - # else: - # random_dict_letter_occurence[each_letter] += 1 +def uses_available_letters(word, letter_bank): # Time complexity: O(2n) Space complexity: O(n) + # This portion of function is to count the frequency of each letter drawn and store it in dict + letter_bank_dict = {} + for each_letter in letter_bank: + if each_letter not in letter_bank_dict: + letter_bank_dict[each_letter] = 1 + else: + letter_bank_dict[each_letter] += 1 + # This portion of function is to split each char in the word and count the frequency of each char. + # If the char is in the dictionary and its count is not over the its corresponding count of being drawn. + # The loop will skip and continue to check the next char. Once there is a char does not fit any of these two conditions, it will return False. + for each_char in word: + char_count = word.count(each_char) + if each_char in letter_bank_dict and char_count <= letter_bank_dict[each_char]: + continue + else: + return False + return True - - -def uses_available_letters(word, letter_bank): - pass - -def score_word(word): - pass +def score_word(word): # Time complexity: O(n) Space complexity: O(1) + dict_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, + } + score = 0 + for each_char in word.upper(): # unify the case of each char + if each_char in dict_score_chart: + score += dict_score_chart[each_char] + if len(word) >= 7: # add additional score if length over 7 + score += 8 + return score def get_highest_word_score(word_list): - pass + # calculate the score of each word in the list and put them in word-score pair to dict + dict_word_score = {} + for each_word in word_list: + dict_word_score[each_word] = score_word(each_word) + + # find out the max score by looping through the values of dict + max_score = max([each_score for each_score in dict_word_score.values()]) + # looping through the keys(words) of the dict, compare if its value equals max_score, + # append the key(word) to an empty if it is True + list_word_with_max_score = [word for word in dict_word_score.keys()if dict_word_score[word] == max_score] + + max_length = max([len(each_word)for each_word in list_word_with_max_score]) + # if max_length > 10, it loops through the keys(words) of the dict, compare if its length equals max_length, + # if it does, return a tuple (each_word, max_score) + if max_length >= 10: + return (each_word, max_score for each_word in dict_word_score.keys() if len(each_word) == max_length) + + else: + # if max_length < 10, it finds out the min length. It loops through the keys(words) of the dict, compare if its length equals min_length, + # if it does, return a tuple (each_word, max_score) + min_length = min([len(each_word)for each_word in list_word_with_max_score]) + return (each_word, max_score for each_word in dict_word_score.keys()if len(each_word) == min_length) From 902a8d8f7b92d60ee10ac58842b1776b077790c1 Mon Sep 17 00:00:00 2001 From: Elly Wong Date: Thu, 23 Sep 2021 19:22:24 -0700 Subject: [PATCH 4/6] Passed all tests --- adagrams/game.py | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index fc0bfdc5..7e541b23 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,6 +1,6 @@ import random -def draw_letters(): # Time complexity: O(1)? Space complexity: O(1)? +def draw_letters(): # Time complexity: O(1) Space complexity: O(1) list_letters_pool = ["A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "C", "C", @@ -43,12 +43,16 @@ def uses_available_letters(word, letter_bank): # Time complexity: O(2n) Space else: letter_bank_dict[each_letter] += 1 - # This portion of function is to split each char in the word and count the frequency of each char. - # If the char is in the dictionary and its count is not over the its corresponding count of being drawn. - # The loop will skip and continue to check the next char. Once there is a char does not fit any of these two conditions, it will return False. + # This portion of function is to split each char in the word and count the + # frequency of each char. + # If the char is in the dictionary and its count is not over the its + # corresponding count of being drawn. + # The loop will skip and continue to check the next char. Once there is a + # char does not fit any of these two conditions, it will return False. for each_char in word: char_count = word.count(each_char) - if each_char in letter_bank_dict and char_count <= letter_bank_dict[each_char]: + if each_char in letter_bank_dict and char_count <= \ + letter_bank_dict[each_char]: continue else: return False @@ -91,28 +95,41 @@ def score_word(word): # Time complexity: O(n) Space complexity: O(1) score += 8 return score -def get_highest_word_score(word_list): +def get_highest_word_score(word_list): # calculate the score of each word in the list and put them in word-score pair to dict dict_word_score = {} - for each_word in word_list: + for each_word in word_list: dict_word_score[each_word] = score_word(each_word) # find out the max score by looping through the values of dict - max_score = max([each_score for each_score in dict_word_score.values()]) + max_score = max([each_word_score for each_word_score in dict_word_score.values()]) # looping through the keys(words) of the dict, compare if its value equals max_score, # append the key(word) to an empty if it is True - list_word_with_max_score = [word for word in dict_word_score.keys()if dict_word_score[word] == max_score] + list_word_with_max_score = [word for word in dict_word_score.keys()if + dict_word_score[word] == max_score] max_length = max([len(each_word)for each_word in list_word_with_max_score]) # if max_length > 10, it loops through the keys(words) of the dict, compare if its length equals max_length, # if it does, return a tuple (each_word, max_score) if max_length >= 10: - return (each_word, max_score for each_word in dict_word_score.keys() if len(each_word) == max_length) - + for each_word in dict_word_score.keys(): + if len(each_word) == max_length: + return (each_word, max_score) else: - # if max_length < 10, it finds out the min length. It loops through the keys(words) of the dict, compare if its length equals min_length, - # if it does, return a tuple (each_word, max_score) + # if max_length < 10, it finds out the min length. It loops through the keys(words) of the dict, compare if its length equals min_length, + # if it does, return a tuple (each_word, max_score) min_length = min([len(each_word)for each_word in list_word_with_max_score]) - return (each_word, max_score for each_word in dict_word_score.keys()if len(each_word) == min_length) + + for each_word in dict_word_score.keys(): + if len(each_word) == min_length: + return (each_word, max_score) + + + + + + + + From 52a3ad557151fd89fa446a5d2123c3eacbd924b0 Mon Sep 17 00:00:00 2001 From: Elly Wong Date: Fri, 24 Sep 2021 06:58:23 -0700 Subject: [PATCH 5/6] Ready to submit --- adagrams/game.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 7e541b23..59e4c32e 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -31,11 +31,15 @@ def draw_letters(): # Time complexity: O(1) Space complexity: O(1) for i in range (10): random_letter = random.choice(list_letters_pool) list_letters_drawn.append(random_letter) - list_letters_pool.remove(random_letter) # remove the drawn letter from the original list of letter pool after each random draw + # remove the drawn letter from the original list of letter pool after + # each random draw + list_letters_pool.remove(random_letter) return list_letters_drawn -def uses_available_letters(word, letter_bank): # Time complexity: O(2n) Space complexity: O(n) - # This portion of function is to count the frequency of each letter drawn and store it in dict +def uses_available_letters(word, letter_bank): + # Time complexity: O(n) Space complexity: O(n) + # This portion of function is to count the frequency of each letter drawn + # and store it in dict letter_bank_dict = {} for each_letter in letter_bank: if each_letter not in letter_bank_dict: @@ -58,7 +62,8 @@ def uses_available_letters(word, letter_bank): # Time complexity: O(2n) Space return False return True -def score_word(word): # Time complexity: O(n) Space complexity: O(1) +def score_word(word): + # Time complexity: O(n) Space complexity: O(1) dict_score_chart = { "A" : 1, "E" : 1, @@ -96,30 +101,24 @@ def score_word(word): # Time complexity: O(n) Space complexity: O(1) return score def get_highest_word_score(word_list): - # calculate the score of each word in the list and put them in word-score pair to dict + # Time complexity: O(n) Space complexity: O(1) dict_word_score = {} for each_word in word_list: dict_word_score[each_word] = score_word(each_word) - # find out the max score by looping through the values of dict - max_score = max([each_word_score for each_word_score in dict_word_score.values()]) + max_score = max([each_word_score for each_word_score in + dict_word_score.values()]) - # looping through the keys(words) of the dict, compare if its value equals max_score, - # append the key(word) to an empty if it is True list_word_with_max_score = [word for word in dict_word_score.keys()if dict_word_score[word] == max_score] - max_length = max([len(each_word)for each_word in list_word_with_max_score]) + max_length = max([len(each_word)for each_word in list_word_with_max_score]) - # if max_length > 10, it loops through the keys(words) of the dict, compare if its length equals max_length, - # if it does, return a tuple (each_word, max_score) if max_length >= 10: for each_word in dict_word_score.keys(): if len(each_word) == max_length: return (each_word, max_score) else: - # if max_length < 10, it finds out the min length. It loops through the keys(words) of the dict, compare if its length equals min_length, - # if it does, return a tuple (each_word, max_score) min_length = min([len(each_word)for each_word in list_word_with_max_score]) for each_word in dict_word_score.keys(): From 94aca7233aa0d8f30c7a98c99f8b0c0aaf4d87bf Mon Sep 17 00:00:00 2001 From: Elly Wong Date: Mon, 27 Sep 2021 14:54:44 -0700 Subject: [PATCH 6/6] refactored code with helper func --- adagrams/game.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 59e4c32e..25980030 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -112,22 +112,22 @@ def get_highest_word_score(word_list): list_word_with_max_score = [word for word in dict_word_score.keys()if dict_word_score[word] == max_score] - max_length = max([len(each_word)for each_word in list_word_with_max_score]) + max_length = max((length_of_each_word_in_list_word_with_max_score(list_word_with_max_score))) if max_length >= 10: - for each_word in dict_word_score.keys(): - if len(each_word) == max_length: - return (each_word, max_score) - else: - min_length = min([len(each_word)for each_word in list_word_with_max_score]) - - for each_word in dict_word_score.keys(): - if len(each_word) == min_length: - return (each_word, max_score) - + return helper_func_return_tuple_with_word_max_score(dict_word_score, max_length, max_score) + else: + min_length = min((length_of_each_word_in_list_word_with_max_score(list_word_with_max_score))) + return helper_func_return_tuple_with_word_max_score(dict_word_score, min_length, max_score) +def helper_func_return_tuple_with_word_max_score(dict_word_score, length, max_score): # max or min length + for each_word in dict_word_score.keys(): + if len(each_word) == length: + return (each_word, max_score) +def length_of_each_word_in_list_word_with_max_score(list_word_with_max_score): + return [len(each_word)for each_word in list_word_with_max_score]