This assignment will be closed on March 19, 2025 (23:59:59).
You must be authenticated to submit your files

Pale Machine CSE101, Groups 5+

G. Pogudin

Objectives

What we will evaluate today: Everything!

Setup: Before you start

Launch Spyder (from the Applications/Programming menu). If you see a “Spyder update” message, just click OK and ignore it.

Before you start: create a new project, called PracticalExam_1 (using “New Project” from the “Projects” menu). This will ensure that files from last week (and future weeks) do not get mixed up together. Download files dict1.txt and dict2.txt from Moodle and put them in the project folder.

Now create a new file named wordgame.py. This is the file in which you will be writing your code today.

Introduction

In this exam you will implement the following word game. The game starts with a fixed relatively long word, for example, mathematics. Then the goal is to use the letters of this word (each letter can be used as many times as it appears in the original word) to construct several smaller words (the letters can be arranged in any order). For mathematics, one could have cat and ham or cast and ham. Note that it is not possible to have cat, ham, and cast at the same time since they contain two c’s and three a’s which is more then the original word (mathematics). The total score of the player is the total length of the words he/she constructed.

Exercises

Exercise 1: Is this a word?

We start with a function which checks if a given word is “allowed” by looking this word up in a provided dictionary (represented as a list of words). We define a function is_in_dictionary which takes two arguments:

  • word is a word to look up;
  • dictionary is a list of “allowed” words.

The function return True if the word appears in the dictionary and False otherwise. The comparison is assumed to be case-sensive, that is, cat is not the same as CaT.

Copy the following function stub into your file and complete it:

def is_in_dictionary(word, dictionary):
    """checks if the word appears in the dictionary 
    """
    pass  # remove this line and replace with your own code

Testing

Test your function is_in_dictionary() in the console, for example as follows:

In [1]: is_in_dictionary("cat", ["dog", "cat", "cow"])
Out[1]: True

In [2]: is_in_dictionary("cat", ["dog", "catastrophy", "cow"])
Out[2]: False

In [3]: is_in_dictionary("COW", ["dog", "catastrophy", "cow"])
Out[3]: False

Note: It is important to test your function in the console as there is no grader feedback.

Upload form is only available when connected

Exercise 2: Reading dictionary from file

Of course, it is not interesting to play with a dictionary like ["dog", "cat", "cow"], and it is definitely tedious to type an actual dictionary by hand. The goal of the exercise is to create a function read_dictionary(filename) reading a dictionary from file filename. The file filename is assumed to have at most one word per line (with possibly starting and trailing empty spaces) as well as empty lines. The words are guaranteed to be distinct. The function should return a list containing all the words. Here is the starter code:

def read_dictionary(filename):
    """Reads a dictionary from file.
    """
    pass  # remove this line and replace with your own code

Testing

When you have finished your function, test it in the console. You should see the following behavior:

In [4]: read_dictionary("dict1.txt")
Out[4]: ['dog', 'cat', 'cow']

In [5]: read_dictionary("dict2.txt")[:5]
Out[5]: ['people', 'history', 'way', 'art', 'world']
Upload form is only available when connected

Exercise 3: Checking if a word is constructable

Now we would like to check if a given word can be constructed using the letters of our original word. This will be done by a function is_constructable(target, source), where target and source are words. The function should return True if target can be constructed using the letters of source and False otherwise. Here is the starter code:

def is_constructable(target, source):
    """Checks if the word target can be constructed from the letters of 
    the word source.
    """
    pass  # remove this line and replace with your own code

Hint

You may want to use the count method which may be familiar from TD2 (try "mama".count('m') and "abaca".count('c')).

Testing

When you have finished your function is_constructable, test it in the console. You should be able to reproduce the following behavior:

In [6]: is_constructable("cat", "mathematics")
Out[6]: True

In [7]: is_constructable("catch", "mathematics")
Out[7]: False

In [8]: is_constructable("mamama", "mathematics")
Out[8]: False

In [9]: is_constructable("ratio", "algorithm")
Out[9]: True
Upload form is only available when connected

Exercise 4: Removing a letter

Once we have a word which can be constructed from the letters of the original word, we would like to remove the used letters. In this exercise you will implement a function which removes one letter. Write a function remove_letter() which takes as input a word word and a letter c, which is guaranteed to be contained in word, and returns word with the first occurence of c being removed. Here is the starter code:

def remove_letter(word, c):
    """Removes the first occurence of c from the word
    """
    pass  # remove this line and replace with your own code

Testing

When you have finished your function remove_letter, test it in the console. You should be able to reproduce the following behavior:

In [10]: remove_letter('space', 's')
Out[10]: 'pace'

In [11]: remove_letter('abracadabra', 'r')
Out[11]: 'abacadabra'
Upload form is only available when connected

Exercise 5: Updating the word

Now we will use the function from the previous exercise to create a function update_word() which takes as input words target and source such that target is constructable from the letters of source, and returns source with all the letters of target removed as in the previous exercise (that is, the first occurrence of each letter is removed at every step). Here is the starter code:

def update_word(target, source):
    """Removes the letter of target from the source
    """
    pass  # remove this line and replace with your own code

Testing

When you have finished your function update_word, test it in the console. You should be able to reproduce the following behavior:

In [12]: update_word("cat", "mathematics")
Out[12]: 'mhematis'

In [13]: update_word("mami", "mathematics")
Out[13]: 'theatcs'

In [14]: update_word("card", "abracadabra")
Out[14]: 'baaabra'

In [15]: update_word("cat", "cat")
Out[15]: ''
Upload form is only available when connected

Exercise 6: Playing one round

Now we will write a function one_round() for playing one round of the game which takes 3 parameters:

  • current_word is a word from which the other words should be constructed in this round;
  • starting_word is the starting word of the game (so, for the first round, we will have starting_word = current_word);
  • dictionary is a dictionary.

The function should prompt the player Please construct a new word from the letters of the word _current_word_:. Once the player types the word, one of the following should happen:

  • if the player types ! (meaning he/she wants to stop the game), the function should return None;
  • if the player types a word equal to starting_word, the function should print Let's try something more interesting! and return current_word;
  • if the player types a word which is not present in the dictionary, the function should print I do not know this word! and return current_word;
  • if the player types a word which cannot be constructed from the letters of the current_word, the function should print Not enough letters! and return current_word;
  • if the player types a word which can be constructed and is present in the dictionary, the function should print Good job, you have earned # points, where # is the length of the word; the function should return the updated (using update_word()) current_word.
def one_round(current_word, starting_word, dictionary):
    """plays one round of the game
    """
    pass  # remove this line and replace with your own code

Testing

Now, test your function one_round() in the console, for example as follows.

In [16]: dictionary = read_dictionary("dict2.txt")

In [17]: one_round("mathematics", "mathematics", dictionary)

Please construct a new word from the letters of the word mathematics:cat
Good job, you have earned 3 points

Out[17]: 'mhematis'

In [18]: one_round("algorithm", "algorithm", dictionary)

Please construct a new word from the letters of the word algorithm:girl
Good job, you have earned 4 points

Out[18]: 'aothm'

In [19]: one_round("aothm", "algorithm", dictionary)

Please construct a new word from the letters of the word aothm:math
Good job, you have earned 4 points

Out[19]: 'o'

In [20]: one_round("mathematics", "mathematics", dictionary)

Please construct a new word from the letters of the word mathematics:mathematics
Let's try something more interesting!

Out[20]: 'mathematics'

In [21]: one_round("algorithm", "algorithm", dictionary)

Please construct a new word from the letters of the word algorithm:ohlala
I do not know this word!

Out[21]: 'algorithm'

In [22]: one_round("algorithm", "algorithm", dictionary)

Please construct a new word from the letters of the word algorithm:!
Upload form is only available when connected

Exercise 7: Playing the game!

Now we use the one_round() function to write a play_game() function which takes starting_word and dictionary. It should ask the player for a new word until the player types ! thus choosing to stop. Once the game is stopped, the function should print: Total score: followed by the total score. Do not forget update the word between the rounds.

def play_game(starting_word, dictionary):
    """Playing the whole game
    """
    pass  # remove this line and replace with your own code

Testing

When you have finished your function play_game, test it in the console. You should be able to reproduce the following behavior:

In [23]: dictionary = read_dictionary("dict2.txt")

In [24]: play_game("algorithm", dictionary)

Please construct a new word from the letters of the word algorithm:girl
Good job, you have earned 4 points
Please construct a new word from the letters of the word aothm:math
Good job, you have earned 4 points
Please construct a new word from the letters of the word o:!
Total score:  8

In [25]: play_game("mathematics", dictionary)

Please construct a new word from the letters of the word mathematics:tea
Good job, you have earned 3 points
Please construct a new word from the letters of the word mhmatics:cat
Good job, you have earned 3 points
Please construct a new word from the letters of the word mhmis:math
Not enough letters!
Please construct a new word from the letters of the word mhmis:him
I do not know this word!
Please construct a new word from the letters of the word mhmis:hm
I do not know this word!
Please construct a new word from the letters of the word mhmis:!
Total score:  6

In [26]: play_game("catcat", dictionary)
Please construct a new word from the letters of the word catcat:cat
Good job, you have earned 3 points
Please construct a new word from the letters of the word cat:cat
Good job, you have earned 3 points
Please construct a new word from the letters of the word :!
Total score:  6
Upload form is only available when connected