lab07 : Scrabble word finder: Python lists, dictionaries and file I/O

num ready? description assigned due
lab07 true Scrabble word finder: Python lists, dictionaries and file I/O Tue 11/21 08:00AM Tue 11/28 05:00PM

You may pair or work alone on this lab.

If you choose to pair, please start by registering your pair on submit.cs in the normal way. Go to, log on, navigate to the link for lab07, then follow the links to invite your pair partner to form a group. Make sure your pair partner logs in and accepts your invitation.

Goals for this lab

The goals for this lab are:

Getting started

The terminal command for this is “idle3 &”. When you have the IDLE window up, you are ready for some programming!

What to program?

In this lab assignment, you are going to make your own Scrabble word finder function, scrabbleWords(). In the end, you will simply input a string of letters and the program will print out (to the screen and to a file) a list of all the possible words you can make along with their point values in descending order (neglecting things like triple letter, double word squares, etc. in the real game of Scrabble). For example, if I input ‘bouni’ as my string of letters, this is what I get:

>>> scrabbleWords('buoni')
obi      5
nub      5
nob      5
nib      5
bun      5
bio      5
bin      5
bi       4
uni      3
ion      3
on       2
nu       2
no       2
in       2
u        1
i        1

So, how did our program know which letter combinations were valid words?……We have to specify a file of words, which you can find here: wordlist.txt . This file must be downloaded (right click and “save as”) and put into your lab07 directory before you begin, so do that now. Note that this file contains a fairly complete list of English words, so beware that there may be some expletive and/or raunchy words - please don’t hold me personally responsible if you are offended. Perhaps this will be motivation for some of you to complete the assignment.

You can choose to start from scratch or use the starter code we have provided here:

Functions to Implement:

  1. createWordList(filename) - return L
  2. canWeMakeIt(myWord, myLetters) - return True or False
  3. getWordPoints(myWord, letterPoints) - return points
  4. outputWordPointPairs(pointWordList, myLetters, toFile) - NO return (just prints a formatted list or writes it to file).
  5. scrabbleWords(myLetters) - NO return (just calls other functions)

Function Details:

  1. createWordList(filename) - return L. Write a function which reads the file filename and returns a list L containing all the words in the file. Note that the last character of every line of the file is the invisible “new line” character ‘\n’ and needs to be sliced off.

  2. canWeMakeIt(myWord, myLetters) - return True or False. Write a function which answers the question: Can I form the word myWord from the string of letters myLetters? The function should return a boolean True or False. Converting myLetters to a list and using the pop() or remove() method may come in handy. You do not need to use all the letters in myLetters. It’s possible that myLetters will contain multiples of the same letters. In the example above if myLetters = “buoni” and myWord=”boon” it should return False. If the input is not the correct type then return False Try to write an algorithm on paper first before attempting to write the code. Think about the list functions at your disposal and the tools you’ve learned up till now.

  3. getWordPoints(myWord, letterPoints) - return points. Write a function that calculates and returns the total point value of myWord given the Python dictionary object letterPoints which consists of letter:pointValue pairs. If a character in myWord is not a key in the provided dictionary then its score value is 0. If any of the input is incorrect type then return 0. Note that you do not need to create the letterPoints dictionary in this step - it is a parameter to our function and will be created in scrabbleWords().

  4. outputWordPointPairs(pointWordList, myLetters, toFile) - NO return (just prints a formatted list or writes it to file).

Putting it all together:

scrabbleWords(myLetters) - NO return (just calls the helper functions above). Here you will call upon your “helper functions” created in steps 1-4 to form a list of all the words (from wordlist.txt) that can be formed from the set of letters contained in myLetters:

letter points

Write test code in

You must write test code using pytest for the following functions:

Write the test code before you implement the functions. This is a way of demonstrating that you understand what each function is supposed to do.

You should test the other two functions manually, although you are welcome to write test code for them as well.

Put your test code in and submit it along with your This test code is worth some amount of points for this lab. We recommend writing at least 3-5 test cases per function, but feel free to write more until you’re confident with your solution.

Note that the submit system will only give you a partial score (similar to lab exam 01). We will show the outcome of some of the instructor test cases but not all!

What should look like

#!/usr/bin/env python3
import pytest
import sys
"other import statements"

def createWordList(filename):
  #Your code

def canWeMakeIt(myWord, myLetters):
  #Your code

def getWordPoints(myWord, letterPoints):  
  #Your code

def outputWordPointPairs(pointWordList, myLetters, toFile):
  #Your code

def scrabbleWords(myLetters):
  #Your code

if __name__=="__main__":
  if len(sys.argv) >= 2:
    print("Invalid input please try again")

What should look like

#!/usr/bin/env python3
import pytest
from lab07 import createWordList

def test_createWordList_0():
  #Your test code

def test_createWordList_1():

from lab07 import canWeMakeIt

def test_canWeMakeIt_0():
  assert(canWeMakeIt('ape','pae') == True)

from lab07 import getWordPoints
letterPoints = {'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 test_getWordPoints_0():
  assert(getWordPoints('ape',letterPoints) == 5)

Running the final product

Just like in lab04, in order to run the program from the terminal you need to include the shebang as the very first line of the file.

#!/usr/bin/env python3

After doing so, make an executable by typing the following into the terminal.

chmod u+x

Now you can run the program from terminal by typing or other similar commands:

./ buoni

Make sure you have this main function at the bottom of the file for this to work.

if __name__=="__main__":
  if len(sys.argv) >= 2:
    print("Invalid input please try again")

you’re finished! Now have fun and experiment with the word finder ;-).


The page for submitting lab07 is here:

Navigate to that page, and upload your and files.

Submission from CSIL command line

If you are working on the ECI/CSIL/lab linux systems, you can also submit at the command line with this command:

~submit/submit -p 874 ~/cs8/lab07/

Notes on using the command line version of submit:

Thanks to Matt Buoni for this lab!