This lab is an important junction for two reasons: it's where those aiming for 200 should start ramping up to harder problems, and it's our last Pyret lab before we start Python. As a result, this lab is structured with some options for you to choose from.
Everyone will do the first set of problems. Further down, you'll see two options:
In lecture, we have done a couple of problems where we give you a problem scenario and ask you to figure out how to use tables, lists, and datatypes to capture the data. And now we have trees too!
The ability to figure out the structure of data needed for a problem is a critical skill in computing. Even if you never write code again after you leave CS111, you may need to think about a collection of data for a project and what shape it has (with someone else writing the code to process that shape). The problems in this section have you practice this skill (which you can expect to be tested on the the final exam).
You are not writing any functions for this section.
When we are matching a data structure to a scenario, questions we ask ourselves include:
Scenario 1:
Name | Age | Breed |
---|---|---|
Bowser | 4 | Bulldog |
Crowley | 2 | Ferret |
Spot | 10 | Lab |
Scenario 2:
Task 1: For each of the above three questions, write out which data structure you should use in each case of the question. When you are done, check your work against the summary in the expandable section below.
NOTE: Both partners should be prepared to explain their work for all checkpoints.
You'll read some scenarios for this part of the lab. For each of these scenarios, complete the following two tasks:
Task 2: Which combination of tables, lists, datatypes, and trees might you use to capture the data in the scenario? You might find it useful to sketch a picture of the scenario to help you think through this.
Task 3: Expand the section marked "Data Structure Proposals" to see some specific datatype proposals for the scenario. Talk through an evaluation of whether the data structure makes sense, citing strengths and weaknesses of the proposal.
A datatype "makes sense" if it:
Consider a tournament that is played in elimination rounds: Some number of teams (usually a power of 2 such as 2, 4, 8, 16, …) enters the tournament. Teams are paired up for round 1. The winner from each round advances to the next, playing another winner from that same round. Each team plays at most once per round. The tournament finishes after a round with only two teams (which gives the overall winner).
Now consider a round-robin tournament, in which every team plays every other team, and the team with the best statistics (combination of points scored, score differences, etc) is the winner.
Imagine a guessing game, in which a player is asked to think of an object/item/etc. The computer then asks a series of yes/no questions before making a guess as to what the object might be. Here are two sample sequences of questions and answers.
The guessing game program needs a data structure to capture the questions that the computer will ask, including the different "next" questions depending on the answer (yes or no). The question sequences could be long.
Imagine a similar guessing game, but now the questions are multiple choice, with different numbers of answers per question. For example, now the computer might ask "How many legs does it have", with answers 0, 2, 4, and 100 all being acceptable answers.
Consider the connections between friends in a social network. Each person has some number of friends, who in turn have other friends, and so on. There may be multiple "friendship chains" that connect one person to another.
These are all examples of real problems (or types of problems) that programmers work on, and choosing the appropriate data structure is a big part of approaching the programming tasks for these problems. If you go on to take CS200, you will get more practice programming with these sorts of data structures.
The trees for the guessing game are examples of what are called decision trees: a series of questions about characteristics of an object or situation are asked, working down the tree until a decision (or guess) is reached at the bottom of the tree.
Some real-world ad-matching systems are based on decision trees. When a user is logged into a site, a program asks the site (e.g. Google) the questions in the tree about the user (e.g., "what gender are they", "what age-range are they", "where do they live", etc): the bottom of the tree contains the ads to show with users with traits from the answers.
In theory, we have shown you enough programming at this point to write an ad-matcher based on decision trees. In practice, that program would be a bit harder than what you're prepared for. Those who come to CS200, however, will do a course project that constructs a decision-tree from a table of data about ad-like information.
Consider the following datatype, which represents information about a single course:
For now, we're just considering lecture-based courses, where each course contains the list of names of students who are taking the course.
Task 4: Copy this datatype into your file and make a couple of example courses.
Task 5: Write a function that takes a Course
and a student name and returns a course. The returned course should have the student name added to the roster.
Task 6: Some years, it feels like certain students' first names appear many times. Write a function count-name
which takes a Course
and a name and returns the number of students with that first name who are in the course roster. Assume every name in the roster is formatted like this 'FIRSTNAME LASTNAME'.
Do this with explicit recursion, rather than built-ins.
Hint: The question here is how to write a recursive function to process a list that is inside of a datatype. You will need two functions here: a recursive one on a list of strings, and a non-recursive one on the datatype that calls your recursive one.
Task 7: Try to write down an explanation of why you needed two functions here. Why can't the count-name
function that takes the datatype as input be recursive?
Task 8: If you have time, change the datatype so that the items in the roster aren't strings, but a new Student
datatype with a student name and semester level.
Task 9: Where in your count-name
function would you need to make edits in order to accommodate this change in datatype? You don't need to write the code (unless you want the practice). The point is to think about what parts of your original code would change or stay the same with this extended definition.
You will write several tree programs as part of the bridge work for CS200. For this lab, we want to make sure that you understand how recursion works on trees before you start writing programs to process them.
Consider the following program, which returns the name of one person with the given eye color from the given tree. Your goal is to understand which name gets returned and why.
Task 10: Look at the program and the where
cases. Without running the code, predict what the code will produce on each case.
Task 11: In lecture, we showed using an execution diagram to track the evolution of recursive function calls. Draw an execution diagram for the program execution from the first where
example, until the point when the if
-expression evaluates to true. Indicate the order in which the functions cals get called and in which order they finish running and return a value. (If you color-code your diagram, this is easier to list off).
has-horse
from class
The function calls get called in the order: red, black, blue. The functions finish in the order: black, blue, red. If Princequillo had parents, we would have had to draw the recursive calls the code would make on those parents' parents, and we would have to be careful in remembering the order in which function calls get made and finish.
Use this image as a starting point:
Task 12: What is the sequence of values for p1
across the calls to in-tree
, assuming we search for a name that is not in the tree? Assume we start with the call in-tree(anna-tree, "X")
. Make a list of the calls to in-tree
that occur in order, in a form like:
in-tree(anna-tree, "X")
in-tree(susan-tree, "X")
Then annotate each call with the value that p1
will have in the directory at the time that the call gets made. What do you notice about that sequence after you've finished it?
Task 13: Write a program all-blue-eyed
that takes an AncTree
and returns a list of the names of all people with blue eyes.
Task 14: If you have time, make a copy of all-blue-eyed
and modify it to take a predicate (a function that returns a boolean) that decides which people to include in the output list (like a form of filter on trees). Try this function on a couple of examples.
We start working in Python soon. If you never did the Python setup part of lab 0, please go back and do that now.
Brown University CSCI 0111 (Fall 2023)
Do you have feedback? Fill out this form.