Variations

The Infinite Library

“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

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/variations")

Acknowledgements

Inspired by libraryofbabel.info. Cover photo from libraryofbabel.info/theory4.html.