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
You can submit your code using submit50
.
Keeping in mind the course’s policy on academic honesty, run the following command in the RStudio console:
submit50("cs50/problems/2024/r/believe")