Problem Set 1

Due: September 9, 2024

Both questions on this first problem set have template files in the repository. You’ll need to grab the assignment from the link below, download, and unzip the contents before proceeding. I would highly suggest opening the entire folder in VSCode, as it will make it very easy to edit different files and helps VSCode find local libraries or files. When you are finished, upload your completed templates back to GitHub. Don’t worry about changing any filenames, you want to override the original template files.

Accept Assignment


Problem 1

Your first computational task is to solve a simple story-problem in Karel’s world. Suppose that Karel has settled into its house, which is the square area in the center of the below image.

Figure 1: Karel’s starting position and house. The goal is to have Karel pick up the ‘newspaper’ and return to its original position and orientation shown here.

Karel starts off in the northwest corner of its house, facing east, as shown in Figure 1. The problem is to program Karel to collect the newspaper–represented by the beeper–from outside the doorway and then to return to its initial position and orientation.

This problem is no more complicated than our initial examples in class, and exists just to get you started and warmed up. You can assume that every part of the world looks just like it does in the above image (Figure 1). The house is exactly this size, the door is always positioned in the center of the east wall as shown, and the beeper is just outside the “door”. Thus, all you have to do is write the sequence of commands necessary to have Karel

  1. move to the newspaper,
  2. pick up the newspaper,
  3. and then return to its original starting point and orientation.

Even though the program only requires a few lines of code, it is still worth getting at least a little practice in decomposition. In your solution, include a helper function for each of the steps listed above, and then use them together to accomplish your goal. You can write all the code in the provided template entitled Karel_Newspaper.py.

Problem 2

In keeping with our story-problems involving Karel and its house, let us now suppose that Karel is wanting to “paint” the outside of its house on the north, west, and south sides. Karel will “paint” by placing beepers along those walls. Karel will always start on the east edge of the north wall, as depicted to the left of Figure 2.

Figure 2: On the left is one possible version of Karel’s starting position and house. On the right is the completed scenario. Note that only the north, west, and south sides of the house are painted, not the east side. And note that Karel has entered the house.

Your goal in this problem is to write a program that paints only the north, west, and south walls, and then moves Karel into the house. The final state would look like the right side of Figure 2.

This would feel very similar to the first problem, except that we want this program to be robust no matter what the dimensions of Karel’s house are. In addition, while Karel will always start on the east edge of the north wall, the direction Karel will initially be facing will not be consistent, and so you will have to figure out your facing direction probably as a first step. You are safe to assume that:

  • Karel has an infinite supply of beepers initially, and so could paint a house of any size without issue.
  • The house will always have at least a 2 avenue/street gap between the sides of the house and the boundaries of the map.
  • There will always be a door somewhere on the east side of the house. But its exact location can vary.

To help you test that your program will correctly complete the task for any general house, there are multiple worlds included in the starting repository that you can load and test your program against. Karel_Painting.w is the default, and matches the world sketched in Figure 2. There are then three different worlds, which have differently sized houses, different directions Karel is initially facing, and different front door locations: Painting1.w, Painting2.w, and Painting3.w.

You can test your program against these worlds by running your program, and then clicking the far left button to open and load in the new world. Then you can press play to test your program against that world. Your same program would successfully work in all of the worlds, without any changes. Proper use of loops and predicate functions will be the key here! A basic template to get you started is in Karel_Painting.py.

Hints
  • Really think of how you want to decompose this problem. I would argue that you are essentially trying to accomplish the same task 3 different times, so that might guide or inspire how you want to break things down.
  • Just because loops will be useful, you don’t need to have all your code inside the loop! Frequently you might need a line or two of code outside a loop to “reposition” Karel a bit.
  • You have lots of potential predicate functions to choose from to determine where the wall is that you are trying to paint or move along. Many of them can work, but some streamline the code more than others.

Overall Checklist

Before you submit the assignment, make sure that: