From 3c53904968cd10d3634cac4cd16877807f4b459a Mon Sep 17 00:00:00 2001 From: Leidy Date: Tue, 17 Sep 2024 20:26:20 -0400 Subject: [PATCH 1/7] Finished wave 3 --- adagrams/game.py | 117 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 3 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..6ed11f8c 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,11 +1,122 @@ +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(): - pass + draw = [] + letter_pool_copy = LETTER_POOL.copy() + #Place the key values (letters) into a list to be able to acces indexes + letters_list = list(letter_pool_copy.keys()) + while len(draw) < 10: + #Take a random number and get the letter in that index + random_letter = letters_list[random.randint(0,len(letter_pool_copy)-1)] + #Check if the letter is availabe in the letter pool and add it to the hand + if letter_pool_copy[random_letter] >= 1: + draw.append(random_letter) + #reduce the frequency in lettr pool, for each letter added to the hand + letter_pool_copy[random_letter] -= 1 + return draw def uses_available_letters(word, letter_bank): - pass + # char_frequency = {} + + # letter_bank_copy = letter_bank.copy() + # word = word.upper() + + # while True: + # for char in word: + # if char in letter_bank_copy: + # letter_bank_copy.remove(char) + # else: + # return False + # return True + + letter_bank_frequency = {} + word_frequency = {} + word = word.upper() + #create the dicts with letter and frequencies + + for letter in letter_bank: + if letter in letter_bank_frequency: + letter_bank_frequency[letter] += 1 + else: + letter_bank_frequency[letter]=1 + if word: + for word_letter in word: + if word_letter in word_frequency: + word_frequency[word_letter] +=1 + else: + word_frequency[word_letter] = 1 + #Compare the dicts values giving a key + for char in word: + if word_frequency[char] > letter_bank_frequency.get(char,0): + return False + return True +# score_chart = { +# 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'] +# } +SCORE_CHART = [ + [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']] +] def score_word(word): - pass + total_points = 0 + points = 0 + word = word.upper() + for i in range(len(SCORE_CHART)-1): + for score_letters_list in SCORE_CHART[i][1]: + for char in word: + if char in score_letters_list: + points =SCORE_CHART[i][0] + total_points += points + else: + points = 0 + + if len(word) >= 7: + total_points += 8 + + return total_points + + + def get_highest_word_score(word_list): pass \ No newline at end of file From 7460537743e98bf0b77b481357d7b2d74c187d8e Mon Sep 17 00:00:00 2001 From: Leidy Date: Fri, 20 Sep 2024 00:14:17 -0400 Subject: [PATCH 2/7] All test passed --- adagrams/game.py | 134 +++++++++++++++++++++++++----------------- tests/test_wave_03.py | 1 - tests/test_wave_04.py | 1 - 3 files changed, 80 insertions(+), 56 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 6ed11f8c..5bff4f87 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,4 +1,5 @@ import random + LETTER_POOL = { 'A': 9, 'B': 2, @@ -29,65 +30,38 @@ } def draw_letters(): + #Create a set of 10 letters that matches the letter frequency distribution# draw = [] letter_pool_copy = LETTER_POOL.copy() - #Place the key values (letters) into a list to be able to acces indexes letters_list = list(letter_pool_copy.keys()) - while len(draw) < 10: - #Take a random number and get the letter in that index + + while len(draw) < 10: + #Take a random number within the len of letter pool and get the letter in that index random_letter = letters_list[random.randint(0,len(letter_pool_copy)-1)] - #Check if the letter is availabe in the letter pool and add it to the hand + + #Check if the letter is still availabe in the letter pool and add it to the hand if letter_pool_copy[random_letter] >= 1: draw.append(random_letter) - #reduce the frequency in lettr pool, for each letter added to the hand + + #reduce the frequency in letter pool, for each letter added to the hand letter_pool_copy[random_letter] -= 1 return draw def uses_available_letters(word, letter_bank): - # char_frequency = {} + #check if the word is an anagram of some or all of the given letters in the hand - # letter_bank_copy = letter_bank.copy() - # word = word.upper() - - # while True: - # for char in word: - # if char in letter_bank_copy: - # letter_bank_copy.remove(char) - # else: - # return False - # return True - - letter_bank_frequency = {} - word_frequency = {} - word = word.upper() - #create the dicts with letter and frequencies - - for letter in letter_bank: - if letter in letter_bank_frequency: - letter_bank_frequency[letter] += 1 - else: - letter_bank_frequency[letter]=1 - if word: - for word_letter in word: - if word_letter in word_frequency: - word_frequency[word_letter] +=1 - else: - word_frequency[word_letter] = 1 - #Compare the dicts values giving a key + letter_bank_copy = letter_bank.copy() + word = word.upper() for char in word: - if word_frequency[char] > letter_bank_frequency.get(char,0): + #Take one of the letters from the hand and make unavailable in the bank copy + if char in letter_bank_copy: + letter_bank_copy.remove(char) + else: return False + return True -# score_chart = { -# 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'] -# } +#-------- Score-Word Func Using Nested Lists --------# SCORE_CHART = [ [1,['A', 'E','I','O','U','L','N','R','S','T']], [2,['D','G']], @@ -98,25 +72,77 @@ def uses_available_letters(word, letter_bank): [10,['Q','Z']] ] def score_word(word): + #Returns the score of a given word as defined by the SCORE_CHART + total_points = 0 points = 0 word = word.upper() - for i in range(len(SCORE_CHART)-1): - for score_letters_list in SCORE_CHART[i][1]: - for char in word: - if char in score_letters_list: - points =SCORE_CHART[i][0] - total_points += points - else: - points = 0 - + for i in range(len(SCORE_CHART)): + for char in word: + if char in SCORE_CHART[i][1]: #check for char in each letter in list rows + points =SCORE_CHART[i][0] #If found, access the index that has the score + total_points += points + else: + points = 0 + + #Extra points if word has 7 or more characters if len(word) >= 7: total_points += 8 return total_points +# #------Score_Word Func Using Dictionaries --------# +# SCORE_CHART = { +# 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'] + +# } +# def score_word(word): +# #Returns the score of a given word as defined by the SCORE_CHART + +# points = 0 +# word = word.upper() +# #access the lists in the dict (values)as letters +# for score, letters in SCORE_CHART.items(): +# for i in range(len(letters)): +# for char in word: +# if char in letters[i]: #check char in +# points += score +# #Extra points if word has 7 or more characters +# if len(word) >= 7: +# points += 8 + +# return points def get_highest_word_score(word_list): - pass \ No newline at end of file + highest_score = 0 + highest_word = " " + + #calculate score for word in word_list + for word in word_list: + current_score = score_word(word) + + #choose the word with highest score + if current_score > highest_score: + highest_word= word + highest_score = current_score + + #If a tie and no word is 10 char, pick the shortest word + if current_score == highest_score \ + and len(word) < len(highest_word) \ + and len(highest_word) != 10: + highest_word = word + + #If a tie and one word is 10 char, pick that word + elif len(word) == 10 \ + and len(highest_word) != 10: + highest_word = word + + return (highest_word, highest_score) \ No newline at end of file diff --git a/tests/test_wave_03.py b/tests/test_wave_03.py index 5473db39..56402068 100644 --- a/tests/test_wave_03.py +++ b/tests/test_wave_03.py @@ -23,4 +23,3 @@ def test_score_extra_points_for_seven_or_longer(): assert score_word("XXXXXXX") == 64 assert score_word("XXXXXXXX") == 72 assert score_word("XXXXXXXXX") == 80 - \ No newline at end of file diff --git a/tests/test_wave_04.py b/tests/test_wave_04.py index e586621c..f41d2786 100644 --- a/tests/test_wave_04.py +++ b/tests/test_wave_04.py @@ -1,5 +1,4 @@ import pytest - from adagrams.game import score_word, get_highest_word_score def test_get_highest_word_score_accurate(): From 53f98005f5c1f99bb968d1a00e63237dff270839 Mon Sep 17 00:00:00 2001 From: Leidy Date: Fri, 20 Sep 2024 00:22:14 -0400 Subject: [PATCH 3/7] Add info about func to practice --- adagrams/game.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adagrams/game.py b/adagrams/game.py index 5bff4f87..dd7879bc 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -122,6 +122,9 @@ def score_word(word): # return points def get_highest_word_score(word_list): + #Calculate which word from the list has the highest score + # applying tie-breaking logic + highest_score = 0 highest_word = " " From 566dbd707cf4bbfb6ad6f37fc5b4380a4d6e093e Mon Sep 17 00:00:00 2001 From: Leidy Date: Fri, 20 Sep 2024 15:09:20 -0400 Subject: [PATCH 4/7] Fixed draw_letters func to get frequencies --- adagrams/game.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index dd7879bc..ac2ca145 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -32,19 +32,26 @@ def draw_letters(): #Create a set of 10 letters that matches the letter frequency distribution# draw = [] - letter_pool_copy = LETTER_POOL.copy() - letters_list = list(letter_pool_copy.keys()) + letter_pool_copy = [] + + #Calculate the weigths for each letter + for letter, frequency in LETTER_POOL.items(): + weights = letter*frequency + #add the weight letters to a new list + for char in weights: + letter_pool_copy.append(char) while len(draw) < 10: #Take a random number within the len of letter pool and get the letter in that index - random_letter = letters_list[random.randint(0,len(letter_pool_copy)-1)] + random_letter = letter_pool_copy[random.randint(0,len(letter_pool_copy)-1)] #Check if the letter is still availabe in the letter pool and add it to the hand - if letter_pool_copy[random_letter] >= 1: + if random_letter in letter_pool_copy: draw.append(random_letter) #reduce the frequency in letter pool, for each letter added to the hand - letter_pool_copy[random_letter] -= 1 + letter_pool_copy.remove(random_letter) + return draw def uses_available_letters(word, letter_bank): From 3b42cd0b13f6b0ada014777d017d52b3c8b0889e Mon Sep 17 00:00:00 2001 From: Leidy Date: Fri, 20 Sep 2024 15:48:08 -0400 Subject: [PATCH 5/7] Used Casefold in uses_available_letter func for case matching --- adagrams/game.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index ac2ca145..32304423 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -57,8 +57,13 @@ def draw_letters(): def uses_available_letters(word, letter_bank): #check if the word is an anagram of some or all of the given letters in the hand - letter_bank_copy = letter_bank.copy() - word = word.upper() + word = word.casefold() + letter_bank_copy = [] + + #Ensure letter matching in letter_bank and word + for letter in letter_bank: + letter_bank_copy.append(letter.casefold()) + for char in word: #Take one of the letters from the hand and make unavailable in the bank copy if char in letter_bank_copy: From 33561ac0c720b430afac6fc8144a745214b84ed0 Mon Sep 17 00:00:00 2001 From: Leidy Date: Fri, 20 Sep 2024 16:00:16 -0400 Subject: [PATCH 6/7] Changed second if to elif in get_highest_word_score for resourcing --- adagrams/game.py | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 32304423..d4b7ac50 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -57,14 +57,14 @@ def draw_letters(): def uses_available_letters(word, letter_bank): #check if the word is an anagram of some or all of the given letters in the hand - word = word.casefold() + casefold_word = word.casefold() letter_bank_copy = [] - + #Ensure letter matching in letter_bank and word for letter in letter_bank: letter_bank_copy.append(letter.casefold()) - for char in word: + for char in casefold_word: #Take one of the letters from the hand and make unavailable in the bank copy if char in letter_bank_copy: letter_bank_copy.remove(char) @@ -98,41 +98,11 @@ def score_word(word): points = 0 #Extra points if word has 7 or more characters - if len(word) >= 7: + if len(word) > 6: total_points += 8 return total_points -# #------Score_Word Func Using Dictionaries --------# -# SCORE_CHART = { -# 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'] - -# } -# def score_word(word): -# #Returns the score of a given word as defined by the SCORE_CHART - -# points = 0 -# word = word.upper() - -# #access the lists in the dict (values)as letters -# for score, letters in SCORE_CHART.items(): -# for i in range(len(letters)): -# for char in word: -# if char in letters[i]: #check char in -# points += score - -# #Extra points if word has 7 or more characters -# if len(word) >= 7: -# points += 8 - -# return points - def get_highest_word_score(word_list): #Calculate which word from the list has the highest score # applying tie-breaking logic @@ -150,7 +120,7 @@ def get_highest_word_score(word_list): highest_score = current_score #If a tie and no word is 10 char, pick the shortest word - if current_score == highest_score \ + elif current_score == highest_score \ and len(word) < len(highest_word) \ and len(highest_word) != 10: highest_word = word From cb37ffd7141a29105a1590ec064d8c42f976ec3c Mon Sep 17 00:00:00 2001 From: Leidy Date: Fri, 22 Nov 2024 22:33:18 -0500 Subject: [PATCH 7/7] modified score word func --- adagrams/game.py | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index d4b7ac50..125c2861 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -74,34 +74,24 @@ def uses_available_letters(word, letter_bank): return True #-------- Score-Word Func Using Nested Lists --------# -SCORE_CHART = [ - [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']] -] +SCORE_DICT = { '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 score_word(word): - #Returns the score of a given word as defined by the SCORE_CHART - - total_points = 0 - points = 0 - word = word.upper() - for i in range(len(SCORE_CHART)): - for char in word: - if char in SCORE_CHART[i][1]: #check for char in each letter in list rows - points =SCORE_CHART[i][0] #If found, access the index that has the score - total_points += points - else: - points = 0 - - #Extra points if word has 7 or more characters - if len(word) > 6: - total_points += 8 - - return total_points + score = 0 + + casefold_word = word.casefold() + + for letter in casefold_word: + print (letter) + score += SCORE_DICT.get(letter) + + if len(casefold_word) > 6: + score += 8 + + return score def get_highest_word_score(word_list): #Calculate which word from the list has the highest score