CS 2 (Winter 2025) Lab 02: Restaurants

This lab focuses on implementing algorithms efficiently. It also illustrates a scenario in which it is convenient to use the Map data structure.

Lab Coordinates

For all remaining CS 2 labs (including this one), you will have one lab session to complete the lab. You will have the option to work solo or in a pair:

Setup

As always, register for the lab using the registration tool: https://register.caltech.codes. Then, follow the same instructions as last time to clone the repository and use IntelliJ.

Goals and Outcomes

By the end of this lab, you will…

An Interesting Data Structures Problem

The Problem

Pasadena has more restaurants per capita than New York City. With so many choices, it’s hard to get a large group of friends to agree on which one to eat at for dinner.


Approval Voting

To decide where to go, every friends will vote on restaurants they approve of (i.e., “are okay going to”). Each person can approve of an unlimited number of choices. The restaurant with the highest number of approvals wins and is selected; ties are broken by alphabetical order.


Example

Inputs

  • The three friends choosing where to eat are Aiden, Bailey, and Classic.
  • The four available restaurants to vote for are Nine & Nine, BCD Tofu House, Main Chick, Zankou Chicken.
  • Their approval votes are as follows:
    • Aiden approves of \(\{\text{BCD Tofu House}, \text{Nine & Nine}\}\)
    • Bailey approves of \(\{\text{Nine & Nine}, \text{Zankou Chicken}\}\)
    • Classic approves of \(\{\text{BCD Tofu House}, \text{Main Chick}, \text{Nine & Nine}\}\)

Output

The winner is Nine & Nine (with 3 votes) since every other option has two or fewer votes.

Data Structures to the Rescue!

As we’ve discussed in class, a large part of solving algorithmic problems is choosing the right data structure. In this lab, you’ll implement one version with Lists (the “wrong” data structure), and another version with Maps (the “right” data structure). Both versions will use a two-part algorithm: (1) record the votes for each restaurant in a temporary data structure, and (2) use the temporary data structure to solve the actual problem.

Attempt 1: “bag of restaurants”

This attempt will look very similar to our IntBag from the first lecture. We will make a List by adding each restaurant one time per approval vote. Then, to process the list, we will count the number of occurrences of each restaurant to find the winner. Each of the two steps of the algorithm will be its own function:

public static List<String> processVotes(List<Set<String>> input)

Constructs and returns a List of restaurants where each one appears the number of times it occurred in the input.

public static String findMostVoted(List<String> processed)

Finds the winner for the processed set of votes.


To do this, you should:

  • Construct a list of restaurants
  • For each restaurant:
    • Count how many votes it got
    • Update the winner so far if necessary

Attempt 2: “tally map”

This attempt will use a “tally map” which uses resturants as keys and vote counts as values. Each of the two steps of the algorithm will, again, be their own functions:

public static Map<String, Integer> processVotes(List<Set<String>> input)

Constructs and returns a Map from restaurants to counts representing where the number of times that restaurant occurred in the input.

public static String findMostVoted(Map<String, Integer> processed)

Finds the winner for the processed set of votes.


Hint: You will likely want to use the keySet method discussed in lecture.

Submitting Your Code for Credit

Your code will be autograded when you submit it to gitlab. You may submit to gitlab as many times as you like, and only the last submission will be counted.

Committing and Pushing to the Repository

A “commit” is version control language for a chunk of changes that mean something when grouped together. Importantly, commits will only stay locally on your machine until you push them to the gitlab server.

Checking the Results on gitlab

When you go to https://gitlab.caltech.edu, you should see your lab02 repository listed. Click on it. All the way on the right, there should be a red “X”, an orange “pause” symbol, a blue “time” symbol, or a green “check” symbol. This symbol indicates the status of the automated tests which will check the correctness of your code. Click on it. On the new page, you should see a table, and under “status”, there should be another symbol. Click on it. The new page will list the “categories” of tests for this repository. These will be some combination of “A”, “B”, “C”, and “D”. These letters indicate what grade you earned on the assessment for passing those tests. If you click on one of them, it will detail for you which sub-tests you passed and failed which might help you debug.

It is good practice to always check gitlab after submitting a project or lab in this course.