Additional Karel Exercises

This page will update occasionally with new exercises that are appropriate for various chapters of the Karel books, independent of the language.

These ask for solutions that are "true" to the structure discussed in the Karel books. They don't depend on things that may be available in a language but are not discussed up to the chapter that the exercises is associated with. In particular, prior to Chapter 9 (in the "Beyond" books) there are no integer variables, nor explicit counting. Solutions with those techniques are generally trivial and give no insight.

In these exercises, when we say "a Robot..." we imply the student will build a subclass of Robot with new methods to carry out the task.

Note that some of these exercises have been designed to have important "edge conditions" that are possible, but possibly not obvious. Carefully consider this in designing solutions.

I will consider exercises suggested by readers for this page.

Chapter 3

3a) As an aid in building future programs, build yourself some infrastructure that you can use in robot programming. This exercise will only get the process started and we will see additions and modifications to it below. But it would be useful to have a robot class that already knows about turnRight, turnAround and several other simple actions. The mechanism for doing this depends on language so see the Java Page or the Python Page for hints on this process and some initial structure. But for now, build turnRight, turnAround, backUp (without changing direction), slideLeft, and slideRight (also keeping the original direction).

Chapter 4

4a) Similar to what we did in Exercise 3a, build a class that contains basic infrastructure for strategies and decorators that include decorators for all of the simple actions that a UrRobot can take: leftTurnDecorator, rightTurnDecorator, pickBeeperDecorator, putBeeperDecorator, moveDecorator and, perhaps a few others. Again, the mechanism depends a bit on language, so see this Java Page or this Python Page for basic structure and hints. This structure can be updated in the future as you learn more and can be used in other robot programs that require strategies.

Chapter 5

5a) A single Robot is in a world without additional walls or beepers. It has no beepers in its beeperBag. Give the robot a method that will return true or false depending on whether it is on the diagonal or not. That is, whether the street and avenue numbers are the same. The robot's final position should be the same as its initial position, though it need not face the same direction at the end as at the beginning.

5b) A single Robot is carrying out some task. Suppose that it is periodically necessary for the robot to face the same direction that it did previously. So if it were facing East at some point then it might need to face that same direction at some future point. Don't assume it it easy to know beforehand which direction, nor when the direction is "set", nor when it is again needed. Build the infrasctrucure to enable this. Note that counter variables aren't used, nor direction variables, etc, other than strategies. Don't forget that the robot might need to do this more than once to carry out its task.

5c) Extend what you wrote for Exercise 3a to add any or all of the following methods and predicates: Method atOrigin returns true if the robot is at the origin or in a corner that is otherwise like the origin with a wall to both the west and south. You can build it so the robot's final direction is the same as its start direction. Method faceWest will turn a robot to the West. Build methods for the other directions as well. Note that the superclass for your class should be changed to Robot from UrRobot, making the new sensing capabilities available.

Chapter 6

6a) A Robot is at (4, 4) facing East with 16 beepers. The world has no intervening walls. The robot wants to lay out the beepers in the shape of a "regular" octagon with edge "length" measured in number of beepers (each edge has the same number of beepers). The "corner" beepers are shared by two edges, of course. Provide a solution that minimizes the number of right turns. Note the symmetry in the problem.

6b) A Robot is at the origin. Somewhere East of it, say at first street and n-th avenue, there is a beeper. Unfortunately, the beeper should have been at n-th street and first avenue. Have the robot correct the error. Assume there are no intervening walls.

6c) Revisit what you wrote for Exercise 5c now that you know about loops. Can you improve any of the methods you wrote there?

6d) Add additional methods to the infrastructure you began in Exercise 3a with methods like goToWall, goToBeeper, toToRobot, emptyBeeperBag, clearCornerOfBeepers.

6e) A robot is somehwhere in the world. Somewhere, directly to it's East, is a beeper. Have the robot fetch the beeper and return to its original location.

6f) A robot is somewhere in the world, remote from walls. It is at one end of a line of beepers facing along it. If the line of beepers has an even length, the robot should end in its starting location without changing anything. It the line of beepers has an odd length, the robot should remove all of the beepers in the line and end at its start location.

6g) A robot is somewhere in the world, remote from walls. It is at one end of a line of beepers, one per corner, facing along it. If the line of beepers has an even length, the robot should end in its starting location without changing anything. It the line of beepers has an odd length, the robot should remove (only) the middle beeper and return to its start location.

6h) Somewhere in the world, remote from walls and beepers there are two robots, without beepers, facing one another, on the same street or avenue. The task is to have them switch positions and end up facing each other.

6i) Somewhere in the world, remote from walls and beepers there are four robots, none with beepers, one each at the corners of a square. Starting with a reference to the robot in the Southwest corner, have the robots walk, one after the other, in a clockwise direction so that the Southwest robot moves to the Northwest corner, the Northwest robot then moves to the Northeast, etc, until the square is complete again. Advanced version: Suppose that it is a rectangle, but maybe not a square. You may assume that the robots initially face the direction in which they should move. (Challenging)

Chapter 7

Some of the following are quite challenging. Most involve recursion and/or strategies. The infrastructure you build for Exercise 4a should help for some.

7a) A Robot requires a method that will determine whether it could move toward the West in its world but that also leave its position and direction unchanged. That is, its final direction is the same as its initial direction and it returns true if and only if it is not blocked to the West. Both recursive and strategy-based solutions are possible.

7b) A Robot without any beepers requires two methods. The first, setStreetToAvenue, will leave it on the same avenue but on the diagonal. The other, setAvenueToStreet, will leave it on the same street, but on the diagonal with street and aveune equal. Assume the world has no additional walls or beepers. There is no guarantee that the final direction is the same as the initial direction.

7c) Discuss how using strategies and decorators can often be used in place of recursion in programming.

7d) A Robot is at the origin facing East. Along first street there is a continuous (no missing avenues) set of beepers from first avenue to n'th avenue. Write a recursive program (without loops) to have the robot pick up all of the beepers. This can be done with chapter 5 material if recursion has been introduced.

7e) As a variation on problem 7d, have the robot find the end of the set of beepers but only pick them up on its return to the origin. Again, do this without loops, using only recursion and the necessary IF statements.

7f) Discuss tail resursion in the context of problems 7d and 7e. Suppose 7d has an additional requirement that the robot return to the origin (again, no loops).

7g) A Robot is somewhere in the world, facing West. Its task is to place beepers on each corner from its current location until it hits a wall, one beeper per corner. If it reaches a wall its task is finished. However, if it runs out of beepers before reaching a wall, then it should retrieve all of the beepers it has placed, returning to its original location. The final direction is unspecified. Carefully consider all proper starting configurations (edge conditions).

7h) Consider problem 7g again in which the robot isn't facing West (and doesn't turn to the West). Under what conditions is the solution of 7g still valid? All?

7i) A Robot is at the origin with a difficult task. Somewhere to its North-East there is a line of beepers of some undetermined length, n. Unfortunately, that line of beepers has been placed in the wrong spot. It should have been place directly North of where it is, but exactly n blocks North. So, if it is on 3rd Street and is six blocks long then it should have been place on 9th street instead. The robot needs to move the line to its proper location. The new line should start on the same avenue as the original.

   Note that this is easier to do if the robot starts with no beepers and, after the robot moves to the Westernmost beeper, can be solved with two recursive methods. Perhaps you can do it with one recursive method and when the robot possibly starts with some indeterminate number of beepers. No WHILE or FOR loops are needed in the solution.

7j) A "Super Robot", able to leap tall buildings is trying to fly East from somewhere along first Avenue (think of it as an altitude, here) over to eighth Avenue, ending at the same altitude. The initial altitude (street) isn't specified, but the ending altidude should be the same. Unfortunately there are several "buildings" in the way, represnted by walls between (some of the) avenues. At no time should the robot occupy a street "lower" than its initial street. So, if it starts on 3rd street then it can't be on first or second during the operation. Design a Robot that can solve this problem. See this sample configuration.

7k) Somewhere in the world there are four beepers forming a square, such as at (3, 4), (3, 7), (6, 4), (6, 7) - (street, avenue) pairs. A robot wants to move all but the beeper farthest to the South-West so that the square has sides that are twice as long as the original. The robot starts with no beepers and there are no intervening walls. The sample set of beepers had a side lengthe of 3 so the result should be anchored at (3, 4) with a side of 6. The robot should be able to handle squares of any size.

7l) A robot starts at the origin with an infinite number of beepers. Somewhere in the world is a beeper. The robot needs to "compute" the traveling distance to that beeper (from the origin) and place a number of beepers at the origin equal to that distance. The "traveling distance" is the length of the minimum path that a robot needs to follow along streets and avenues with a unit distance between streets and between avenues.

7m) Similar to problem 7l, a robot starting at the origin needs to place at the origin a number of beepers equal to the traveling distance between the only two beepers in the world.

7n) There is a stack of beepers, one per street, along first avenue, say k beepers. Design a class so that its robot, starting with an infinite number of beepers places a number of beepers, equal to the integer quotient of the length of that stack divided by five on first street, second avenue. The original stack is left unchanged. The integer quotent of 17 when divided by 5 is 3. This is also called 17 div 5.

7o) There is a stack of beepers, one per street, along first avenue, say k beepers. On first Street, second avenue there are several beepers, say n Design a class so that its robot, starting with no beepers places on first street third avenue a number of beepers equal to the integer quotient k div n. The original stack may be modified as needed. Can you also place beepers representing the remainder? The remainder, 17 mod 5 is 2.

7p) There are beepers on first street and on avenues 1 through 100 (or whatever). Have a robot remove beepers from the non-prime avenues using the Seive of Eratosthenes process. At the end of the process there will be beepers only on avenues 2, 3, 5, etc. For ease of termination, you can place a blocking wall at the end of the line of beepers.

7q) There is a line of beepers along some street in a world without additional walls. There is also a line of beepers along some avenue. If the lines intersect at some corner, have a robot, that starts at the origin, remove all beepers from the intersecting corner (if any) and return them to the origin, placing them there. You may assume that there are only finitely many beepers in this problem. Are there initial conditions in which the problem is ill defined with no solution possible?

7r) A Robot is somewhere in the world. There is a single beeper at k-th street and n-th avenue. Unfortunately it should have been placed at n-th street and k-th avenue. Design a robot that can solve this problem by finding and moving the beeper. (Challenging) Note that a program can add decorators to a given strategy prior to execution of the doIt method, but it can also remove them if a decorator provides access to its "decorated".

7s) There is a beeper on first street, say at (1, k), and another on first avenue, say at (n, 1). Design a robot that will place its single beeper at (n, k).

7t) Solve problem 6e again, but with recursion.


Last Updated:September 9, 2023

Back to Joseph Bergin's Home Page.