diff --git a/README.md b/README.md index 41a9049e..87b3e48f 100644 --- a/README.md +++ b/README.md @@ -253,3 +253,5 @@ Implement a function called `get_highest_word_score` in `game.py`. This method s - prefer the word with the fewest letters... - ...unless one word has 10 letters. If the top score is tied between multiple words and one is 10 letters long, choose the one with 10 letters over the one with fewer tiles - If the there are multiple words that are the same score and the same length, pick the first one in the supplied list + + diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..47400e36 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,11 +1,100 @@ +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 +} + +SCORE_CHART = { + 'A': 1, + 'B': 3, + 'C': 3, + 'D': 2, + 'E': 1, + 'F': 4, + 'G': 2, + 'H': 4, + 'I': 1, + 'J': 8, + 'K': 5, + 'L': 1, + 'M': 3, + 'N': 1, + 'O': 1, + 'P': 3, + 'Q': 10, + 'R': 1, + 'S': 1, + 'T': 1, + 'U': 1, + 'V': 4, + 'W': 4, + 'X': 8, + 'Y': 4, + 'Z': 10 +} + def draw_letters(): - pass + letters = [] + pool = LETTER_POOL.copy() + while len(letters) < 10: + hand = random.choice(list(pool)) + if pool[hand] <= 0: + continue + pool[hand] -= 1 + letters.append(hand) + return letters def uses_available_letters(word, letter_bank): - pass + bank = letter_bank.copy() + for character in word: + character = character.upper() + if character in bank: + bank.remove(character) + else: + return False + return True def score_word(word): - pass + score = 0 + for character in word: + character = character.upper() + score += SCORE_CHART[character] + if len(word) >= 7: + score += 8 + return score def get_highest_word_score(word_list): - pass \ No newline at end of file + candidates = [(w, score_word(w)) for w in word_list] + highest_score = max(candidates, key = lambda c: c[1]) + ties = [c for c in candidates if c[1] == highest_score[1]] + if len(ties) == 1: + return ties[0] + for c in candidates: + if len(c[0]) == 10: + return c + return min(ties, key=lambda t: len(t[0])) \ No newline at end of file diff --git a/nb.ipynb b/nb.ipynb new file mode 100644 index 00000000..6fe31688 --- /dev/null +++ b/nb.ipynb @@ -0,0 +1,287 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "LETTER_POOL = {\n", + " 'A': 9, \n", + " 'B': 2, \n", + " 'C': 2, \n", + " 'D': 4, \n", + " 'E': 12, \n", + " 'F': 2, \n", + " 'G': 3, \n", + " 'H': 2, \n", + " 'I': 9, \n", + " 'J': 1, \n", + " 'K': 1, \n", + " 'L': 4, \n", + " 'M': 2, \n", + " 'N': 6, \n", + " 'O': 8, \n", + " 'P': 2, \n", + " 'Q': 1, \n", + " 'R': 6, \n", + " 'S': 4, \n", + " 'T': 6, \n", + " 'U': 4, \n", + " 'V': 2, \n", + " 'W': 2, \n", + " 'X': 1, \n", + " 'Y': 2, \n", + " 'Z': 1\n", + "}\n", + "\n", + "\n", + "SCORE_CHART = {\n", + " 'A': 1, \n", + " 'B': 3, \n", + " 'C': 3, \n", + " 'D': 2, \n", + " 'E': 1, \n", + " 'F': 4, \n", + " 'G': 2, \n", + " 'H': 4, \n", + " 'I': 1, \n", + " 'J': 8, \n", + " 'K': 5, \n", + " 'L': 1, \n", + " 'M': 3, \n", + " 'N': 1, \n", + " 'O': 1, \n", + " 'P': 3, \n", + " 'Q': 10, \n", + " 'R': 1, \n", + " 'S': 1, \n", + " 'T': 1, \n", + " 'U': 1, \n", + " 'V': 4, \n", + " 'W': 4, \n", + " 'X': 8, \n", + " 'Y': 4, \n", + " 'Z': 10\n", + "}\n", + "\n", + "def draw_letters():\n", + " # create letters list\n", + " letters = []\n", + " # create pool dictionary by copying LETTER_POOL dicitonary\n", + " pool = LETTER_POOL.copy()\n", + " # set loop to iterate while the number of letters in letters list is less than 10\n", + " while len(letters) < 10:\n", + " # create hand list \n", + " # turn pool dictionary into a list\n", + " # get random character string from pool list\n", + " hand = random.choice(list(pool))\n", + " # reference pool dictionary value at a random index in the hand list\n", + " # iterate through the rest of the loop if the pool value is <= 0\n", + " if pool[hand] <= 0:\n", + " # exclude pool values <= 0 from being appended to letters list\n", + " continue\n", + " # subtract 1 from the key value in pool dictionary at the random index\n", + " pool[hand] -= 1\n", + " # create string array by appending the letters list to the hand list\n", + " letters.append(hand)\n", + " # return the letters list which is now a string array\n", + " return letters\n", + "\n", + "def uses_available_letters(word, letter_bank):\n", + " # make bank dictionary by copying letter_bank dictionary\n", + " bank = letter_bank.copy()\n", + " # declare character variable in for loop to represent a letter\n", + " for character in word:\n", + " # make character uppercase\n", + " character = character.upper()\n", + " if character in bank:\n", + " # when there is a letter in the bank dicitonary remove that character from the bank\n", + " bank.remove(character)\n", + " else:\n", + " # return false if the character is not in bank\n", + " return False\n", + " # return true if the the word meets the function condition (is in bank)\n", + " return True \n", + "\n", + "\n", + "def score_word(word):\n", + " # set score to counter to 0\n", + " score = 0\n", + " for character in word:\n", + " # make the letter uppercase if it's in word\n", + " character = character.upper() \n", + " # add the value of the letter by referencing the SCORE_CHART dictionary at that letter\n", + " score += SCORE_CHART[character] \n", + " # check if the length of word is >= 7\n", + " if len(word) >= 7:\n", + " # add an extra 8 to the score if length of word >= 7\n", + " score += 8\n", + " # return the score\n", + " return score\n", + "\n", + "\n", + "def get_highest_word_score(word_list):\n", + " # candidate = (None, 0)\n", + " # for word in word_list:\n", + " # score_word(word)\n", + "\n", + " candidates = [(w, score_word(w)) for w in word_list]\n", + "\n", + " scores = [score_word(w) for w in word_list]\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "# def get_highest_word_score(word_list):\n", + "# # create word list\n", + "# word_scores = []\n", + "# # check word in word_list\n", + "# for word in word_list:\n", + "# # add the word and the score of the word to the empty word_scores list\n", + "# # access the score of the word by calling score word function at word \n", + "# word_scores.append((word, score_word(word)))\n", + "# # sort tuple integer values from highest to lowest \n", + "# word_scores.sort(key=lambda x: x[1], reverse = True)\n", + "\n", + "# # create a variable for tied highest score\n", + "# # find tied highest score by is found and saved to\n", + "# # tied highest score variable\n", + "# # by indexing the word scores list strings and integers\n", + "# tied_highest_score = word_scores[0][1]\n", + "# # create empty list for tied highest scores\n", + "# tied_highest_scores = []\n", + "# # check if the word score is a tied highest score\n", + "# for word_score in tied_highest_score:\n", + "# # if tied highest score is the same as the word score (the value, indicated by indexing the interger value of the string at [1])\n", + "# if tied_highest_score == word_score[1]:\n", + "# # add that word score to the tied_highest_scores list\n", + "# tied_highest_scores.append(word_score)\n", + "# # if the there is just one highest score in the tied_highest_scores list\n", + "# if len(tied_highest_scores) == 1:\n", + "# # return tuple string and tuple value\n", + "# return word_scores[0]\n", + "\n", + "# # tied_highest_scores.sort(key=lambda x: len(x[0]), reverse = True)\n", + "# # if len(tied_highest_scores[0][0]) == 1:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "word_list = [\"JQ\", \"aslfjkdaa\", \"giraffe\", \"AAAAAAAAAA\", \"BBBBBB\", \"TTTTTTTTTT\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "[(w, score_word(w)) for w in word_list]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "[{\"score\" : score_word(w), \"word\" : w} for w in word_list]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scores = []\n", + "for w in word_list:\n", + " scores.append((w,score_word(w)))\n", + "print(scores)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + " # candidates = [(w, score_word(w)) for w in word_list]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "word_list = [\"JQ\", \"asdasa\", \"AAAAAAAAAA\", \"BBBBBB\", \"TTTTTTTTTT\"]\n", + "candidates = [(w, score_word(w)) for w in word_list]\n", + "highest_score = max(candidates, key=lambda c : c[1])\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(candidates)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(highest_score)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}