# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment

responses = initialize_assignment("1_lab_scrabble", "week_2", "lab", assignment_points = 14.0, assignment_tag = 'week2-lab')

# Initialize Otter
import otter
grader = otter.Notebook("1_lab_scrabble.ipynb")

๐Ÿงช ๐ŸŽฎ Lab 2: Scrabble Game Calculator#

In this laboratory assignment, you will develop a scoring system for competitive word games while learning essential functional programming concepts including map and reduce functions. The scoring system you create will be similar to those used in professional tournament play.

Professional Scrabble Gaming Context#

Professional players utilize sophisticated scoring systems that require rapid calculation while considering multiple factors. The 2004 documentary โ€œWord Warsโ€ demonstrates how championship players have memorized over 120,000 playable words and can calculate optimal scoring positions instantaneously. Your task will be to develop the foundational components of such a scoring system.

Scoring System#

The scoring system assigns point values to letters based on their frequency in English language usage. The official scoring system is structured as follows:

Points

Letters

Strategic Value

1 point

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

Foundation letters for word construction

2 points

D, G

Valuable consonants for word building

3 points

B, C, M, P

Strong word initiators

4 points

F, H, V, W, Y

High-value consonants

5 points

K

Strategic positioning letter

8 points

J, X

Premium scoring opportunities

10 points

Q, Z

Maximum point potential

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")

Testing Your Implementation#

Test your functions with various words to ensure accurate scoring:

# Test cases
test_words = ["PYTHON", "ALGORITHM", "CODE", "QUIZ"]

for word in test_words:
    print(
        f"{word}: {word_score(word)} points (Basic), {word_score(word)} points"
    )

Submitting Assignment#

Please run the following block of code using shift + enter to submit your assignment, you should see your score.

from pykubegrader.submit.submit_assignment import submit_assignment

submit_assignment("week2-lab", "1_lab_scrabble")