Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vectorised unit test for Collatz Conjecture #111

Merged
merged 2 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions exercises/collatz-conjecture/example.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
collatz_step_counter <- function(num) {
collatz_scalar <- function(num) {

if (num <= 0) {
stop("Only positive numbers are allowed")
} else if (num == 1) {
return(0)
return(0)
} else if (num %% 2 == 0) {
return(1 + collatz_step_counter(num / 2))
} else {
return(1 + collatz_step_counter(3 * num + 1))
}

}

collatz_step_counter <- Vectorize(collatz_scalar)
5 changes: 5 additions & 0 deletions exercises/collatz-conjecture/test_collatz-conjecture.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ test_that("Negative input results in an error", {
expect_error(collatz_step_counter(-15))
})

test_that("Answer can accept vector parameter", {
Copy link
Contributor

@katrinleinweber katrinleinweber Oct 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a) Is this message a clear enough to provide a learner (who progressed through our track without having to think about vectorising!) with sufficient guidance, so that they are able to refactor their most-tests-passing solution according to this last test?
b) Should we update the ReadMe as well? Maybe with a note that we start testing for compatibility with the R-idiomatic vector-support here?

As I mentioned in #109 (comment), I'm a novice when it comes to this topic.

expect_equal(collatz_step_counter(
c(1, 16, 12, 1000000)), (c(0, 4, 9, 152)))
})

message("All tests passed for exercise: collatz-conjecture")