 CoCalc Public Filessupport / nbgrader-r-kernel.ipynb
Author: William A. Stein
Description: Jupyter notebook support/2015-06-04-141749-bokeh.ipynb
In [ ]:
foo <- function(a, b) {
# Returns a + b

### BEGIN SOLUTION
return(a + b)
### END SOLUTION
}

In [ ]:
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
}

Test your code from above here(1 point), ID: 3c374e
In [ ]:
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