Skip to content

Instantly share code, notes, and snippets.

@elipousson
Created November 24, 2024 16:42
Show Gist options
  • Save elipousson/df5f532e1aeef325a8e03b3b9a53015a to your computer and use it in GitHub Desktop.
Save elipousson/df5f532e1aeef325a8e03b3b9a53015a to your computer and use it in GitHub Desktop.
Function to make basic worksheet questions
make_worksheet_questions <- function(range = c(0:25),
ops = c("+", "+", "+", "-", "-", "x"),
n = 30,
allow_neg = FALSE) {
lhs <- sample(range, n, replace = TRUE)
rhs <- sample(range, n, replace = TRUE)
ops <- sample(ops, n, replace = TRUE)
if (!allow_neg) {
for (i in seq(n)) {
if ((lhs[[i]] < rhs[[i]]) & (ops[[i]] == "-")) {
lhs[[i]] <- lhs[[i]] + rhs[[i]]
}
}
}
questions <- paste(lhs, ops, rhs, "=", "____", sep = " ")
questions <- stringr::str_pad(
questions,
max(nchar(questions)),
pad = "_",
side = "right"
)
paste0(questions, "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment