Pit Stop
Problem to Solve
Ever changed your car’s tires in less than 3 seconds? In Formula One, that’s the usual!
Formula One is a type of elite, international racing. Races take place all over the world, including destinations like Miami, Florida, and Suzuka, Japan. Besides the racer themselves, each team is composed of a “pit crew” of up to 22 people. The pit crew changes tires, fixes the car, and refuels very quickly. Nowadays, an excellent pit stop can take less than 3 seconds.
In a program called pitstop.R
, in a folder called pitstop
, write a program to analyze pit stop times at Formula One races.
Distribution Code
For this problem, you’ll need to download pitstop.R
, along with several CSV files containing pit stop data.
Download the distribution code
Open RStudio per the linked steps and navigate to the R console:
>
Next execute
getwd()
to print your working directory. Ensure your current working directory is where you’d like to download this problem’s distribution code. If using RStudio through cs50.dev the recommended directory is /workspaces/NUMBER
where NUMBER
is a number unique to your codespace.
If you do not see the right working directory, use setwd
to change it!
Next execute
download.file("https://cdn.cs50.net/r/2024/x/psets/1/pitstop.zip", "pitstop.zip")
in order to download a ZIP called pitstop.zip
into your codespace.
Then execute
unzip("pitstop.zip")
to create a folder called pitstop
. You no longer need the ZIP file, so you can execute
file.remove("pitstop.zip")
Now type
setwd("pitstop")
followed by Enter to move yourself into (i.e., open) that directory. Your working directory should now end with
pitstop/
If all was successful, you should execute
list.files()
and see a file named pitstop.R
as well as several files ending with .csv
. If not, retrace your steps and see if you can determine where you went wrong!
Schema
Before jumping in, it will be helpful to get a sense for the “schema” (i.e., organization!) of the data you’re given.
Learn about this data
Each CSV file contains pit stop data from one Formula One race. For example, miami.csv
contains data from the most recent Miami Grand Prix.
Each row in a CSV file represents one pit stop. In each CSV file, you’ll find the following columns:
- team, which is the team doing the pit stop
- driver, which is the last name of the driver doing the pit stop
- time, which is how long the pit stop took, in seconds
- lap, which is the lap when the pit stop happened
A team usually has 2 drivers in one race, and Formula One races have many laps (i.e., circles around the track).
Specification
In pitstop.R
, write a program that prompts the user to enter a CSV file to analyze. Your program should then print
the following information:
- The total number of pit stops
- The duration of the shortest pit stop
- The duration of the longest pit stop
- The total time spent on pit stops during the race, across all racers
You may assume the user will not enter an invalid filename. All CSV files will have the same column names.
Usage
Assuming pitstop.R
is in your working directory, enter the below in the R console to test your program:
source("pitstop.R")
Assessment
See the feedback page to learn more about how problems like these will be assessed.
Correctness
While check50
is available for this problem (see below), you’re encouraged to test your code on your own, too.
- Run your program with
source("pitstop.R")
. Typemiami.csv
. Your program should output that there were 28 pit stops, with the shortest being 1.94 seconds and the longest being 11.05 seconds. The total time spent on pit stops was 98.53 seconds. - Run your program with
source("pitstop.R")
. Typesuzuka.csv
. Your program should output that there were 36 pit stops, with the shortest being 2.08 seconds and the longest being 5.43 seconds. The total time spent on pit stops was 105.32 seconds.
check50
You can check your code using check50
, a program that CS50 will use to test your code when you submit. But be sure to test it yourself as well!
Run the following command in the R Studio console:
check50("cs50/problems/2025/r/pitstop")
Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
How to Submit
After you submit, be sure to check your autograder results. If you see SUBMISSION ERROR: missing files (0.0/1.0)
, it means your file was not named exactly as prescribed (or you uploaded it to the wrong problem).
Correctness in submissions entails everything from reading the specification, writing code that is compliant with it, and submitting files with the correct name. If you see this error, you should resubmit right away, making sure your submission is fully compliant with the specification. The staff will not adjust your filenames for you after the fact!
In RStudio, select the pitstop.R
file containing your work for this problem, as by checking the box to the left of the file’s name. With the file selected, click on the icon at the top of the file explorer. Choose Export followed by Download.
Go to CSCI E-5a’s Gradescope page.
Click Problem Set 1: Pit Stop.
Drag and drop your .R
file to the area that says Drag & Drop. Be sure that your .R
file is correctly named exactly as prescribed above, lest the autograder fail to run on your submission! Note that your submission is considered incomplete if any of the files are missing—be sure they’re all there!
Click Upload.
You should see a message that says “Problem Set 1: Pit Stop submitted successfully!”
Be sure to double-check your autograder results before moving on!