From 1ad72cfada4a9e63accaf4286e76f4a6cab1fa6c Mon Sep 17 00:00:00 2001 From: nelasunitha <80846358+nelasunitha@users.noreply.github.com> Date: Fri, 20 Sep 2024 00:49:16 -0700 Subject: [PATCH 1/3] Completed Adagrams --- adagrams/game.py | 121 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 4 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..3a82a0c0 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,11 +1,124 @@ +import random def draw_letters(): - pass + 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 + } + #taking an empty list to store drawn letters + drawn_letters =[] + while len(drawn_letters) < 10: + + #extracting all the keys from dictionary + letter_pool_keys = letter_pool.keys() + letter = random.choice(list(letter_pool_keys)) + if letter_pool[letter] > 0: + drawn_letters.append(letter) + + #adding random letter to the list and decreasing the frequency of the letter + letter_pool[letter]-= 1 + return drawn_letters def uses_available_letters(word, letter_bank): - pass +# converting all the letters in letter bank to upper case + letter_bank = [letter.upper() for letter in letter_bank] +# making a dictionary for a letter_bank to check the frequency of letters + lookup = {} + for letter in letter_bank: + if letter in lookup: + lookup[letter] += 1 + else: + lookup[letter] = 1 + #converting all the letters of word into uppercase and checking the frequebcy of the letter, if found decrementing the value or else returning false + + for letter in word.upper(): + if letter not in lookup or lookup[letter] == 0: + return False + else: + lookup[letter] -= 1 + return True + +#Approaching as per draw_letters function with nested loops + +# def score_word(word): +# # converting all the letters in letter bank to upper case +# word = [letter.upper() for letter in word] +# total_score = 0 +# # making a dictionary with letters and it's score value +# score_value = { +# 'AEIOULNRST': 1,'DG': 2, 'BCMP': 3, 'FHVWY': 4, 'K': 5, 'JX': 8, 'QZ': 10 +# } +# if len(word) > 6 and len(word) <=10: +# total_score = 8 +# for letter in word: +# for scoring_letter_group, score in score_value.items(): +# if letter in scoring_letter_group: +# total_score+= score +# return total_score + + + +# optimal solution where dictionary is created for individual letter and score value without nested loops def score_word(word): - pass + score_value = { + '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 + } + word = word.upper() + total_score = 0 + if len(word) > 6 and len(word) <=10: + total_score = 8 + total_score+= sum(score_value.get(letter, 0) for letter in word ) + return total_score + + def get_highest_word_score(word_list): - pass \ No newline at end of file + max_word = word_list[0] + max_word_score =score_word(max_word) + #iterating from the second word + for word in word_list[1:]: + word_score = score_word(word) + if word_score > max_word_score: + max_word = word + max_word_score = word_score + + # words with same score + elif word_score == max_word_score: + # Prefer the word with the fewest letters, unless one has 10 letters + if len(word) == 10 and len(max_word) != 10: + max_word = word + elif len(word) < len(max_word) and len(max_word) != 10: + max_word = word + + max_word_score = max(max_word_score, word_score) + return (max_word,max_word_score ) From d8b517db4dc3c9b3ce5c9fa6fa7497f6822179b2 Mon Sep 17 00:00:00 2001 From: nelasunitha <80846358+nelasunitha@users.noreply.github.com> Date: Fri, 20 Sep 2024 12:32:38 -0700 Subject: [PATCH 2/3] Changed wave1 --- adagrams/game.py | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index 3a82a0c0..fd2ef273 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -30,18 +30,40 @@ def draw_letters(): } #taking an empty list to store drawn letters drawn_letters =[] +# using random.choice +# while len(drawn_letters) < 10: + +# #extracting all the keys from dictionary +# letter_pool_keys = letter_pool.keys() +# letter = random.choice(list(letter_pool_keys)) +# if letter_pool[letter] > 0: +# drawn_letters.append(letter) + +# #adding random letter to the list and decreasing the frequency of the letter +# letter_pool[letter]-= 1 +# return drawn_letter + + + total_letters = sum(letter_pool.values()) + while len(drawn_letters) < 10: + # Generating a random number between 1 and the total number of letters + rand_num = random.randint(1, total_letters) + + # Finding the corresponding letter + cumulative_count = 0 + for letter, frequency in letter_pool.items(): + cumulative_count += frequency + if rand_num <= cumulative_count: + drawn_letters.append(letter) + letter_pool[letter] -= 1 + total_letters -= 1 + break - #extracting all the keys from dictionary - letter_pool_keys = letter_pool.keys() - letter = random.choice(list(letter_pool_keys)) - if letter_pool[letter] > 0: - drawn_letters.append(letter) - #adding random letter to the list and decreasing the frequency of the letter - letter_pool[letter]-= 1 return drawn_letters + def uses_available_letters(word, letter_bank): # converting all the letters in letter bank to upper case @@ -72,12 +94,13 @@ def uses_available_letters(word, letter_bank): # score_value = { # 'AEIOULNRST': 1,'DG': 2, 'BCMP': 3, 'FHVWY': 4, 'K': 5, 'JX': 8, 'QZ': 10 # } -# if len(word) > 6 and len(word) <=10: -# total_score = 8 + # for letter in word: # for scoring_letter_group, score in score_value.items(): # if letter in scoring_letter_group: # total_score+= score +# if len(word) > 6 and len(word) <=10: +# total_score += 8 # return total_score @@ -95,9 +118,12 @@ def score_word(word): } word = word.upper() total_score = 0 - if len(word) > 6 and len(word) <=10: - total_score = 8 + total_score+= sum(score_value.get(letter, 0) for letter in word ) + + if len(word) > 6 and len(word) <=10: + total_score += 8 + return total_score From 2858c79cab5e927c604fd05d9e7d1bf88ade0b7e Mon Sep 17 00:00:00 2001 From: nelasunitha <80846358+nelasunitha@users.noreply.github.com> Date: Mon, 23 Sep 2024 23:36:07 -0700 Subject: [PATCH 3/3] changes implemented per feedback --- adagrams/game.py | 82 +++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 57 deletions(-) diff --git a/adagrams/game.py b/adagrams/game.py index fd2ef273..35dc34f6 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,6 +1,7 @@ import random + def draw_letters(): - letter_pool = { + Letter_POOL = { 'A': 9, 'B': 2, 'C': 2, @@ -30,37 +31,23 @@ def draw_letters(): } #taking an empty list to store drawn letters drawn_letters =[] -# using random.choice -# while len(drawn_letters) < 10: - -# #extracting all the keys from dictionary -# letter_pool_keys = letter_pool.keys() -# letter = random.choice(list(letter_pool_keys)) -# if letter_pool[letter] > 0: -# drawn_letters.append(letter) - -# #adding random letter to the list and decreasing the frequency of the letter -# letter_pool[letter]-= 1 -# return drawn_letter - + total_letters = sum(Letter_POOL.values()) - total_letters = sum(letter_pool.values()) - - while len(drawn_letters) < 10: + HAND_SIZE = 10 + while len(drawn_letters) < HAND_SIZE: # Generating a random number between 1 and the total number of letters - rand_num = random.randint(1, total_letters) + rand_index = random.randint(1, total_letters) # Finding the corresponding letter cumulative_count = 0 - for letter, frequency in letter_pool.items(): + for letter, frequency in Letter_POOL.items(): cumulative_count += frequency - if rand_num <= cumulative_count: + if rand_index <= cumulative_count: drawn_letters.append(letter) - letter_pool[letter] -= 1 + Letter_POOL[letter] -= 1 total_letters -= 1 break - return drawn_letters @@ -69,45 +56,23 @@ def uses_available_letters(word, letter_bank): # converting all the letters in letter bank to upper case letter_bank = [letter.upper() for letter in letter_bank] # making a dictionary for a letter_bank to check the frequency of letters - lookup = {} + letter_frequency_dict = {} for letter in letter_bank: - if letter in lookup: - lookup[letter] += 1 + if letter in letter_frequency_dict: + letter_frequency_dict[letter] += 1 else: - lookup[letter] = 1 + letter_frequency_dict[letter] = 1 #converting all the letters of word into uppercase and checking the frequebcy of the letter, if found decrementing the value or else returning false for letter in word.upper(): - if letter not in lookup or lookup[letter] == 0: + if letter not in letter_frequency_dict or letter_frequency_dict[letter] == 0: return False else: - lookup[letter] -= 1 + letter_frequency_dict[letter] -= 1 return True -#Approaching as per draw_letters function with nested loops - -# def score_word(word): -# # converting all the letters in letter bank to upper case -# word = [letter.upper() for letter in word] -# total_score = 0 -# # making a dictionary with letters and it's score value -# score_value = { -# 'AEIOULNRST': 1,'DG': 2, 'BCMP': 3, 'FHVWY': 4, 'K': 5, 'JX': 8, 'QZ': 10 -# } - -# for letter in word: -# for scoring_letter_group, score in score_value.items(): -# if letter in scoring_letter_group: -# total_score+= score -# if len(word) > 6 and len(word) <=10: -# total_score += 8 -# return total_score - - - -# optimal solution where dictionary is created for individual letter and score value without nested loops def score_word(word): - score_value = { + 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, @@ -119,18 +84,21 @@ def score_word(word): word = word.upper() total_score = 0 - total_score+= sum(score_value.get(letter, 0) for letter in word ) + total_score += sum(LETTER_SCORES.get(letter, 0) for letter in word ) - if len(word) > 6 and len(word) <=10: - total_score += 8 + BONUS_LENGTH_MIN = 7 + BONUS_LENGTH_MAX = 10 + BONUS_POINTS = 8 - return total_score + if BONUS_LENGTH_MIN <= len(word) <= BONUS_LENGTH_MAX: + total_score += BONUS_POINTS + return total_score def get_highest_word_score(word_list): max_word = word_list[0] - max_word_score =score_word(max_word) + max_word_score = score_word(max_word) #iterating from the second word for word in word_list[1:]: word_score = score_word(word) @@ -147,4 +115,4 @@ def get_highest_word_score(word_list): max_word = word max_word_score = max(max_word_score, word_score) - return (max_word,max_word_score ) + return (max_word, max_word_score )