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 Wed 11/28 08:00AM Tue 12/04 11:59PM

In this lab, you’ll get more practice with:

This lab may be done solo, or in pairs.

Before you begin working on the lab, please decide if you will work solo or with a partner.

A reminder about working with a pair programming partner:

Once you and your partner are in agreement, choose an initial driver and navigator, and have the driver log into their account.

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 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 (you may need to refresh the page if the links do not load immediately):

Functions to Implement:

  1. createWordList(filename) - return a list of strings
  2. canWeMakeIt(myWord, myLetters) - return True or False
  3. getWordPoints(myWord, letterPoints) - return an integer representing the point value for a word
  4. outputWordPointPairs(pointWordList, myLetters, toFile) - NO return (just prints a formatted list or writes it to a file).
  5. scrabbleWords(myLetters) - NO return (just calls other functions)

Function Details:

  1. createWordList(filename) - return a list of strings. Write a function which reads the file filename and returns a list 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 an int representing the points for myWord. 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, filename, toFile) - NO return (just prints a formatted list or writes it to file).

NOTE: Your function should add the .txt extension to the filename before opening a file with that name.

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 lab07_student_tests.py

You must write your own tests 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 lab07_student_test.py and submit it along with your lab07.py file. 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 Gradescope will only give you a partial score. Gradescope will use test cases different from the tests that you will wrote in lab07_student_test.py.

What lab07.py should look like

import pytest
#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__":
  print("Manual test cases can be done here and/or in IDLE's command line")
  # manual tests

What lab07_student_tests.py should look like

import pytest
from lab07 import createWordList

def test_createWordList_0():
  #Your test code


def test_createWordList_1():
  #Your test code
....


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

You can load your lab07.py and run scrabbleWords in IDLE’s interactive shell. In scrabbleWords you must make one call to print to the console with outputWordPointPairs where toFile = True, and another call to write to a file with outputWordPointPairs where toFile = False. Gradescope test cases will fail if you forget to write your output to a file.

Upload lab07.py and lab07_tests.py to Gradescope.

Once you’re done with writing your functions, navigate to the Lab assignment “lab07” on Gradescope and upload your lab07.py and lab07_student_tests.py files. Remember to add your partner to Groups Members for this submission on Gradescope if applicable. At this point, if you worked in a pair, it is a good idea for both partners to log into Gradescope and see if you can see the uploaded files for lab07.