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 lab before we start Python (on Monday). As a result, this lab is structured with some options for you to choose from.
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 second quiz or 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: 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.
For each of these scenarios, complete the following two tasks:
Task: 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: Expand the section marked "Data Structure Proposals" to see some specific datatype proposals for the scenario. Write an evaluation of whether the data structure makes sense, citing strengths and weaknesses of the proposal.
A datatype "makes sense" if it:
You will put your answers into a Google Form. We're doing this to get a sense of how the class is doing, and so we can review how to talk about data structure choices in a future lecture.
Here's the form for your answers: https://forms.gle/DZShgYLS86RsJ1mX7
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.
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: Copy this datatype into your file and make a couple of example courses.
Task: 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: 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: 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: If you have time (while still giving yourself time to get Pycharm installed), 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: 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: Look at the program and the where
cases. Without running the code, predict what the code will produce on each case.
Task: As you did two weeks ago in the recursion lab, draw a nested-box diagram for the program execution from the first where
example, until the point when the if
-expression evaluates to true.
As you did last week, upload a picture of your diagram to this form.
Note: The biggest mistake that students made on the nested-box diagrams in the recursion lab was not writing out the whole expression that surrounds the function call in the box. In Wednesday's class, when we wrote out the boxes for in-tree, notice that the pending use of or
before the inner box and the pending call to in-tree(parent-2, name)
after the inner box are both in the picture. Many students omitted these details last time.
Task: What is the sequence of values for parent-1
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 parent-1
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: Write a program all-blue-eyed
that takes an AncTree
and returns a list of the names of all people with blue eyes.
Task: If you have time, make a copy of all-blue-eyed
and modify it to take a predicate (function that returns boolean) that indicates which people to include in the output list (like a form of filter on trees).
The course switches to Python on Monday. If you would like help getting Pycharm installed on your computer, feel free to use lab time to get set up. Follow our Pycharm setup guide.
Do we have to use Pycharm? If you are already comfortable with a different Python programming environment, you are welcome to use it, as long as you are able to set it up to work with pytest
, the Python testing framework. Course staff will only support Pycharm, however, so if you decide to use another tool, you have to be able to get it working on your own.