# Initialize Otter
import otter
grader = otter.Notebook("lab2-scrabble.ipynb")

πŸ§ͺπŸ–₯ Lab 2: Scrabble Score Calculator#

In this lab you will use the map and reduce functions to calculate scores in the game Scrabble.

Entering Your Information for Credit#

To receive credit for assignments it is important we can identify your work from others. To do this we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

# Please provide your first name, last name, Drexel ID, and Drexel email. Make sure these are provided as strings. "STRINGS ARE TEXT ENCLOSED IN QUOTATION MARKS."

# In the assignments you will see sections of code that you need to fill in that are marked with ... (three dots). Replace the ... with your code.
first_name = ...
last_name = ...
drexel_id = ...
drexel_email = ...
grader.check("q0-Checking-Your-Name")

Intro to Scrabble#

Scrabble is a board game in which players lay tiles containing letters to spell words. Each letter has an associated point value based on the rarity of the letter. For instance, the very common letter E is worth only one point, while the much rarer Q is worth 10 points. The letter scores are shown in the table below:

Score

Letters

1 point

A, E, I, O, U, L, N, S, T, R

2 points

D, G

3 points

B, C, M, P

4 points

F, H, V, W, Y

5 points

K

8 points

J, X

10 points

Q, Z

Task 1: Letter score

Your first task is to complete a function which takes in a letter and outputs the score for that letter. A dictionary can be used to store the information from the scoring table above.

Write python code to do the following:

  • Inside the provided letter_score function, use the provided scores dictionary S to determine the score for the provided letter called letter.

  • Store the score in a variable called score.

Note: you may assume that the input letter will always be uppercase.

Your code replaces the prompt: ...

# this line creates a function called letter_score with one argument: letter
def letter_score(letter):
    # create the scoring dictionary of letter scores
    S = {
        "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,
    }
    ...
    # this line outputs the score from the function
    return score

# this runs the letter score function with input "Q" and prints its output
print(letter_score("Q"))
grader.check("task1-letter-score")

Task 2: Letter scores

Now that you have a function that computes the score for a letter, you can use Python’s built-in map function to apply it to each letter in a word to get a list of letter scores.

First you will need to convert the string word into a list of the individual letters. This can be achieved with list(word).

Next, use the map function to apply the letter_score function to each letter in the list. Recall how the map function is used: If you have a function f and a list L = [a,b,c], you can do map(f,L) which results in the mapped list [f(a), f(b), f(c)].

Write python code to do the following:

  • Inside the provided letter_scores function, first convert the argument string word into a list.

  • Map the letter_score function onto the list to get a list of letter scores.

  • Store the final list of letter scores in a variable called score_list.

Your code replaces the prompt: ...

# this line creates a function called letter_scores with one argument: word
def letter_scores(word):
    ...
    # this line outputs the score list from the function
    return list(score_list)

# this runs the letter_scores function with input "EXAMPLE" and prints out the resulting list of letter scores
print(letter_scores("EXAMPLE"))
grader.check("task2-letter-scores")

Task 3: Word score

The last thing you need to do is sum the list of indivudal letters scores to get the total point value for the entire word.

To do this you should use the sum function. This adds all the elements of a list and returns the sum.

Write python code to do the following:

  • Inside the provided word_score function, first call the letter_scores function on the input word to get the list of individual letter scores.

  • Next use the sum function on the list and return the final sum.

Your code replaces the prompt: ...

def word_score(word):
    ...

# use this to check your results
word_score("ZEBRA")
grader.check("task3-word-score")

Submitting Your Assignment#

To submit your assignment please use the following link the assignment on GitHub classroom.

Use this link to navigate to the assignment on GitHub classroom.

If you need further instructions on submitting your assignment please look at Lab 1.

Viewing your score#

Each .ipynb file you have uploaded will have a file with the name of your file + Grade_Report.md. You can view this file by clicking on the file name. This will show you the results of the autograder.

We have both public and hidden tests. You will be able to see the score of both tests, but not the specific details of why the test passed or failed.

Note

In python and particularly jupyter notebooks it is common that during testing you run cells in a different order, or run cells and modify them. This can cause there to be local variables needed for your solution that would not be recreated on running your code again from scratch. Your assignment will be graded based on running your code from scratch. This means before you submit your assignment you should restart the kernel and run all cells. You can do this by clicking Kernel and selecting Restart and Run All. If you code does not run as expected after restarting the kernel and running all cells it means you have an error in your code.

Fin#