0% found this document useful (0 votes)
32 views

11 Stepwise Refinement

This document describes a programming problem where Karel is tasked with decorating barren tree trunks represented as vertical wall sections on a grid with clusters of beepers to symbolize new spring leaves. Karel must climb each tree and place 4 beepers in a square at the top. The trees may vary in height and spacing but Karel has just enough beepers and should end facing east at the bottom of the last tree. The reader is prompted to break the problem down into simpler subproblems, considering first the special case of a single tree to help solve the general case of multiple trees.

Uploaded by

Jimmy Dgebuadze
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

11 Stepwise Refinement

This document describes a programming problem where Karel is tasked with decorating barren tree trunks represented as vertical wall sections on a grid with clusters of beepers to symbolize new spring leaves. Karel must climb each tree and place 4 beepers in a square at the top. The trees may vary in height and spacing but Karel has just enough beepers and should end facing east at the bottom of the last tree. The reader is prompted to break the problem down into simpler subproblems, considering first the special case of a single tree to help solve the general case of multiple trees.

Uploaded by

Jimmy Dgebuadze
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS106A

Handout #11 June 27, 2012

An Example of Stepwise Refinement


Thanks to Eric Roberts for basically this entire handout

In our class example today, well have Karel act out the coming of springtime to a winter world that looks something like this:

6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14

In this sample world, the vertical wall sections represent barren tree trunks. In an attempt to add some semblance of life back to the world, Karel has taken on the mission of adorning the barren trees with a new set of leaves represented by beepers. Karels plan is to climb each of the trees and adorn the top of each tree with a cluster of four leaves arranged in a square like this:

Thus, when Karel is done, the winter scene will look like this:
6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14

2 As in most Karel problems, the situation that Karel faces need not match exactly the one shown in the diagram. There may be more trees; Karel simply continues the process until there are no beepers left in the beeper bag. The trees may also be of different heights or spaced differently than the ones shown in the diagram. Your task is to design a program that is general enough to solve any such problem, subject to the following assumptions: Karel starts at the origin facing east, somewhere west of the first tree. The trees are always separated by at least two corners, so that the leaves at the top dont interfere with one another. The trees always end at least two corners below the top, so that the leaf cluster will not run into the top wall. Karel has just enough beepers to outfit all of the trees. The original number of beepers must therefore be four times the number of trees. Karel should finish facing east at the bottom of the last tree. Think hard about what the parts of this program are and how you could break it down into simpler subproblems. What if there was only one tree? How does that simplify the problem, and how can you use the one-tree solution to help solve the more general case? The solutions will be posted after class or tomorrow as Handout #11A.

You might also like