Can You Believe in Machines?
Happy life with the machines
Scattered around the room
Look what they made, they made it for me
Happy technology
Outside, the lions roam
Feeding on remains
We’ll never leave, look at us now
So in love with the way we are
Every night, they rock us to sleep
Digital family
Is it real or is it a dream?
Can you believe in machines?
– The Veldt, deadmau5 feat. Chris James
Problem to Solve
Can you believe in machines? More and more frequently, programmers turn to generative artificial intelligence—perhaps in the form of ChatGPT or CS50’s own cs50.ai—to write code. All the more reason, then, to write good test cases: it’s up to you to ensure the code generated by these “machines” works as you intend!
In files called believe.R
and test-believe.R
, you’ll do just that. First, you’ll write test cases for a function that solves a problem of your choice. Then, you’ll then turn to a large language model to write code for you. Finally, you’ll ensure the code you’re given passes all of your tests.
Getting Started
Create believe.R
and test-believe.R
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 create this problem’s folder. 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! Try typing setwd("..")
if in the working directory of another problem, which will move you one directory higher.
Next execute
dir.create("believe")
in order to create a folder called believe
in your codespace.
Now type
setwd("believe")
followed by Enter to move yourself into (i.e., open) that directory. Your working directory should now end with
believe/
Finally, type
file.create("test-believe.R")
and
file.create("believe.R")
to create files called test-believe.R
and believe.R
inside of the believe
folder.
If all was successful, you should execute
list.files()
and see both files. If not, retrace your steps and see if you can determine where you went wrong!
Specification
You’ll approach this problem in three steps:
- Identify a problem that can be solved by creating a function in R.
- Write test cases for that function in
test-believe.R
, instead of creating the function yourself. - Use a large language model, like ChatGPT or cs50.ai, to implement the function for you. Modify the code as needed to pass your test cases.
Because you’ll be relying on a large language model to generate code for you, it’s most important to focus on writing thorough test cases.
Identify a Problem To Solve
The problem you choose is entirely up to you, subject to these constraints:
- It should be non-trivial (i.e., challenging enough that it would take you some time to write the code on your own).
- It should have clearly defined inputs and outputs.
- It should be suitable for solving with a function in R.
Write Test Cases
In test-believe.R
, write a series of test cases that thoroughly define your function’s intended inputs, outputs, and—if applicable—errors and warnings.
Write Your Function
In believe.R
, turn to a large language model like ChatGPT or cs50.ai to write your function for you. Consider providing, in the prompt you use, the test cases for your function.
If the function passes all test cases, be sure to check if the test cases cover all possible scenarios the function might handle.
If the function does not pass all test cases, modify the implementation until it does.
Usage
Assuming believe.R
is in your working directory, enter the below in the R console to run your program:
source("believe.R")
And assuming test-believe.R
is in your working directory, enter the below in the R console to test your program:
source("test-believe.R")
How to Test
It’ll be up to you to write the test cases this time!
check50
You can also 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 RStudio console:
check50("cs50/problems/2024/r/believe")
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 believe.R
and test-believe.R
files containing your work for this problem, as by checking the box to the left of the files’ names. With the file selected, click on the icon at the top of the file explorer. Choose Export, name your file
believe-solution.zip
, followed by Download.
Go to CSCI E-5a’s Gradescope page.
Click Problem Set 6: Can You Believe in Machines?.
Unzip your believe-solution.zip
file. Open the folder. Drag and drop your .R
files to the area that says Drag & Drop. Be sure that your .R
files are 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 6: Can You Believe in Machines? submitted successfully!”
Be sure to double-check your autograder results before moving on!