| Hosted by CoCalc | Download
Kernel: R (R-Project)
foo <- function(a, b) { # Returns a + b ### BEGIN SOLUTION return(a + b) ### END SOLUTION }
squares <- function(n) { # Compute the squares of the numbers from 1 to n. ### BEGIN SOLUTION # Put correct code here. This code is removed for the student version, but is # used to confirm that your tests are valid. if (n <= 0) { stop("n must be positive") } x <- 1:n return(x * x) ### END SOLUTION }
testthat::test_that("squares function works as expected", { # test the result is an integer vector of length 10 testthat::expect_vector(squares(10), ptype = integer(), size = 10) # check for a specific n=3 testthat::expect_equal(squares(3), c(1, 4, 9)) # use 'tolerance' when there are slight floating point errors testthat::expect_equal(squares(2), c(1, 4.000001), tolerance = 0.002) }) # make sure the error contains the word 'positive' for a negative n testthat::test_that("squares function raises errors", { testthat::expect_error(squares(-1), "*positive*", ignore.case = TRUE) }) ### BEGIN HIDDEN TESTS # students will NOT see this extra test testthat::expect_equal(squares(10), c(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)) ### END HIDDEN TESTS

Describe the task here, e.g., "Process the data and create a plot to illustrate your results."

=== BEGIN MARK SCHEME ===

Describe how you will grade the task here.

=== END MARK SCHEME ===