Variations
“By this art you may contemplate the variations of the 23 letters”
– The Anatomy of Melancholy, published 1621
Problem to Solve
In The Library of Babel, a 1941 short story, Jorge Luis Borges imagined an unending library, filled with infinite books whose pages were each random variations of letters. If such a library existed, it would contain everything from your own life story to the ultimate fate of humanity: the only task, of course, would be to find it in the sea of randomness.
Such a library doesn’t exist, but you can approximate it through R! In a program called variations.R
, in a folder called variations
, write a program to generate random sequences of the 26 letters, a–z. Maybe you’ll get lucky with a coherent phrase!
Demo
Distribution Code
For this problem, you’ll need to download variations.R
.
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! Try typing setwd("..")
if in the working directory of another problem, which will move you one directory higher.
Next execute
download.file("https://cdn.cs50.net/r/2024/x/psets/3/variations.zip", "variations.zip")
in order to download a ZIP called variations.zip
into your codespace.
Then execute
unzip("variations.zip")
to create a folder called variations
. You no longer need the ZIP file, so you can execute
file.remove("variations.zip")
Now type
setwd("variations")
followed by Enter to move yourself into (i.e., open) that directory. Your working directory should now end with
variations/
If all was successful, you should execute
list.files()
and see variations.R
. If not, retrace your steps and see if you can determine where you went wrong!
Specification
In variations.R
, complete the provided functions, random_character
and print_sequence
. Use these functions to write a program that outputs random sequences of the lowercase letters a–z. Your program should:
- Output 20 character-long sequences.
- Wait 0.25 seconds between printing each character, to give the appearance of characters being typed.
You might find these functions of use:
sample
, which “takes a sample of the specified size” from the elements of its first argument.Sys.sleep
, which can “suspend execution for a specified interval.”
You might also like to know that letters
, an object built into R, is a vector that includes all 26 lowercase letters a–z!
Usage
Assuming variations.R
is in your working directory, enter the below in the R console to test your program:
source("variations.R")
How to Test
Here’s how to test your code manually:
- Run your program with
source("variations.R")
. Your program should output a random sequence of 20 characters, lowercase a–z, while waiting a quarter of a second between printed characters.
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/variations")
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 variations.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 3: Variations.
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 3: Variations submitted successfully!”
Be sure to double-check your autograder results before moving on!
Acknowledgements
Inspired by libraryofbabel.info. Cover photo from libraryofbabel.info/theory4.html.