Scrabble and Spell-Checker assignment
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CIS-024C Fall 2020 - Scrabble Project\n", "\n", "Source: MITEdx Introduction to Computation/Python\n", "\n", "The rules of the game are as follows:\n", "\n", "## Dealing\n", "A player is dealt a hand of n letters chosen at random (assume n=7 for now).\n", "\n", "The player arranges the hand into as many words as they want out of the letters, using each letter at most once.\n", "\n", "Some letters may remain unused (these won't be scored).\n", "\n", "## Scoring\n", "The score for the hand is the sum of the scores for each word formed.\n", "\n", "The score for a word is the sum of the points for letters in the word, multiplied by the length of the word, plus 50 points if all n letters are used on the first word created.\n", "\n", "Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on. We have defined the dictionary SCRABBLE_LETTER_VALUES that maps each lowercase letter to its Scrabble letter value.\n", "\n", "For example, 'weed' would be worth 32 points ((4+1+1+2) for the four letters, then multiply by len('weed') to get (4+1+1+2)*4 = 32). Be sure to check that the hand actually has 1 'w', 2 'e's, and 1 'd' before scoring the word!\n", "\n", "As another example, if n=7 and you make the word 'waybill' on the first try, it would be worth 155 points (the base score for 'waybill' is (4+1+4+3+1+1+1)*7=105, plus an additional 50 point bonus for using all n letters)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Helper Code" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import random\n", "import string\n", "\n", "VOWELS = 'aeiou'\n", "CONSONANTS = 'bcdfghjklmnpqrstvwxyz'\n", "HAND_SIZE = 7\n", "\n", "SCRABBLE_LETTER_VALUES = {\n", " '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\n", "}\n", "\n", "# -----------------------------------\n", "# Helper code\n", "# (you don't need to understand this helper code)\n", "\n", "WORDLIST_FILENAME = \"words.txt\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def loadWords():\n", " \"\"\"\n", " Returns a list of valid words. Words are strings of lowercase letters.\n", " \n", " Depending on the size of the word list, this function may\n", " take a while to finish.\n", " \"\"\"\n", " print(\"Loading word list from file...\")\n", " # inFile: file\n", " inFile = open(WORDLIST_FILENAME, 'r')\n", " # wordList: list of strings\n", " wordList = []\n", " for line in inFile:\n", " wordList.append(line.strip().lower())\n", " print(\" \", len(wordList), \"words loaded.\")\n", " return wordList" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def getFrequencyDict(sequence):\n", " \"\"\"\n", " Returns a dictionary where the keys are elements of the sequence\n", " and the values are integer counts, for the number of times that\n", " an element is repeated in the sequence.\n", "\n", " sequence: string or list\n", " return: dictionary\n", " \"\"\"\n", " # freqs: dictionary (element_type -> int)\n", " freq = {}\n", " for x in sequence:\n", " freq[x] = freq.get(x,0) + 1\n", " return freq\n", "\t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scoring a word" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# 10 points\n", "\n", "def getWordScore(word, n):\n", " \"\"\"\n", " Returns the score for a word. Assumes the word is a valid word.\n", "\n", " The score for a word is the sum of the points for letters in the\n", " word, multiplied by the length of the word, PLUS 50 points if all n\n", " letters are used on the first turn.\n", "\n", " Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is\n", " worth 3, D is worth 2, E is worth 1, and so on (see SCRABBLE_LETTER_VALUES)\n", "\n", " word: string (lowercase letters)\n", " n: integer (HAND_SIZE; i.e., hand size required for additional points)\n", " returns: int >= 0\n", " \"\"\"\n", " # TO DO ... <-- remove="" this="" comment="" when="" you="" code="" this="" function\n"="" ]="" },="" {="" "cell_type":="" "markdown",="" "metadata":="" {},="" "source":="" [="" "###="" displaying="" a="" hand"="" ]="" },="" {="" "cell_type":="" "code",="" "execution_count":="" 16,="" "metadata":="" {},="" "outputs":="" [],="" "source":="" [="" "def="" displayhand(hand):\n",="" "="" \"\"\"\n",="" "="" displays="" the="" letters="" currently="" in="" the="" hand.\n",="" "\n",="" "="" for="" example:\n",="" "="">>> displayHand({'a':1, 'x':2, 'l':3, 'e':1})\n", " Should print out something like:\n", " a x x l l l e\n", " The order of the letters is unimportant.\n", "\n", " hand: dictionary (string -> int)\n", " \"\"\"\n", " for letter in hand.keys():\n", " for j in range(hand[letter]):\n", " print(letter,end='') # print all on the same line\n", " print() # print an empty line\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deal hand" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def dealHand(n):\n", " \"\"\"\n", " Returns a random hand containing n lowercase letters.\n", " At least n/3 the letters in the hand should-->