Exercises

Overview

Below you can find some practice problems that you can use to test your understanding of programming logic. For each question, first try to guess what the code will produce. Then see if you are correct by running the code on your own.

Exercises

1: Indexing

accuracy <- c(1, 0, 1, 1, 0)
reaction_times <- c(520, 700, 480, 510, 730)
reaction_times[accuracy == 1]

2: ifelse()

condition <- c("A", "B", "A", "B", "A")
stimulus <- ifelse(condition == "A", "face", "scene")
stimulus

3: If-else

age <- 17

if (age >= 18) {
  status <- "adult"
} else {
  status <- "minor"
}

status

4: Indexing

condition <- c("A", "B", "A", "B", "A")
correct <- c(1, 1, 0, 1, 1)
condition[correct == 1 & condition == "A"]

5: For loops

rt1 <- c(500, 480, 530)
rt2 <- c(520, 490, 500)
diffs <- numeric(length(rt1))
for (i in 1:length(rt1)) {
  diffs[i] <- rt2[i] - rt1[i]
}
diffs

6: While loops

savings <- 100
years <- 0
while (savings < 200) {
  savings <- savings * 1.07
  years <- years + 1
}
years

7: For loops and if-else

rt <- c(480, 610, 700)
labels <- character(length(rt))
for (i in 1:length(rt)) {
  if (rt[i] < 500) {
    labels[i] <- "fast"
  } else if (rt[i] <= 650) {
    labels[i] <- "medium"
  } else {
    labels[i] <- "slow"
  }
}
labels

8: For loops and if-else

rt <- c(350, 560, 720, 450)
flags <- rep(FALSE, length(rt))
for (i in 1:length(rt)) {
  if (rt[i] > 500) {
    if (rt[i] < 700) {
      flags[i] <- TRUE
    }
  }
}
flags

9: Data manipulation

df <- data.frame(
  subject = c(1, 1, 2, 2),
  condition = c("congruent", "incongruent", "congruent", "incongruent"),
  rt = c(450, 600, 470, 620)
) 

df.new <- df |>
  filter(condition == "incongruent")

df.new

10: Data manipulation

df <- data.frame(
  trial = 1:5,
  response = c("old", "new", "old", "old", "new"),
  correct = c(TRUE, TRUE, FALSE, TRUE, FALSE)
)

df.new <- df |>
  mutate(score = ifelse(correct, 1, 0))

sum(df.new$score)

11: Functions

categorize_rt <- function(rt) {
  if (is.na(rt)) {
    return("missing")
  } else if (rt < 500) {
    return("fast")
  } else {
    return("slow")
  }
}

categorize_rt(480)

12: Data manipulation

df <- data.frame(
  trial = 1:5,
  emotion = c("happy", "sad", "angry", "happy", "sad"),
  rating = c(5, 3, 4, 4, 2)
)

df |>
  filter(emotion == "happy") |>
  mutate(adjusted = rating + 1)

13: Data summaries

df <- data.frame(
  subject = c(1, 1, 2, 2),
  condition = c("easy", "hard", "easy", "hard"),
  correct = c(1, 0, 1, 1)
)

df |>
  group_by(condition) |>
  summarize(mean_acc = mean(correct))

14: Data manipulation

df <- data.frame(
  trial = 1:4,
  rt = c(500, 430, 670, 520)
)

df |>
  arrange(rt)

15: Data visualization

df <- data.frame(
  condition = c("A","A","A","A","A","B","B","B","B","B"),
  rt = c(420, 430, 510, 525, 415, 620, 510, 605, 430, 615)
)

ggplot(df, aes(x = condition, y = rt)) +
  geom_boxplot()

16: Data visualization

df <- data.frame(
  subject = c(1,2,3,4,5,1,2,3,4,5),
  condition = c("easy","easy","easy","easy","easy","hard","hard","hard","hard","hard"),
  accuracy = c(.8, 1, .75, .6, .8, .5, .7, .8, .4, .2)
)

ggplot(df, aes(x = condition, y = accuracy, color = factor(subject))) +
  geom_point()

Advanced Exercises

Code Breakers

Turn any normal english text into an encrypted version of the text, and be able to turn any decrypted text back into normal english text. A simple encryption would be to scramble the alphabet such that each letter corresponds to a new randomly chosen (but unique) letter. You should have a function for each, as well as some tests that demonstrate how it works.

Playing Games

Snakes and ladders

Your task here is to write an algorithm that can simulate playing the depicted Snakes and Ladders board depicted here. You should assume that each roll of the dice produces a random number between 1 and 6. After you are able to simulate one played game, you will then write a loop to simulate 1000 games, and estimate the average number of dice rolls needed to successfully complete the game.

Tic Tac Toe

Imagine that two players make completely random choices when playing tic-tac-toe. Each game will either end in a draw or one of the two players will win. Create a simulation of this “random” version of tic-tac-toe. Out of 10,000 simulations, what proportion of the time is the game won versus drawn?

Sudoku

Sudoku is a logic puzzle in which the goal is to fill a 9 x 9 grid with numbers 1 to 9. The board has 9 rows, 9 columns, and is divided into 9 3x3 boxes. The catch is that each number must appear only once per row, column, and box. Start with a Sudoku puzzle that has some numbers filled in - here is an example, but you can use any starting puzzle. Write some code to solve the puzzle. To do so, you will probably want to write a function to make sure a number “works” for a particular spot, and then keep trying to add numbers until you fill in the entire puzzle.

Find the Missing Number

You have a vector listing numbers from 1 to 100. You shuffle it randomly, and then— oh no, one of the numbers has gone missing! Write a function that will generate a randomized vector that is missing one value. Then write a function that can find the missing number. The second function should generalize to work any vector that includes consecutive numbers but is missing one value.

Wordle Thought Experiment

Thought experiment: How would you build a Wordle solver? Write out the algorithm in pseudo code. Think about what functions you’d like to be able to use (whether they already exist or not), and how you would chain them together to solve the puzzle.

Advanced Exercises were adapted from this list of Practice Problems and daily.dev blog.