Skip to content

Commit

Permalink
Fix find_next_vector_element
Browse files Browse the repository at this point in the history
Add some tests
  • Loading branch information
Smit-tay committed Jan 29, 2024
1 parent 1d421fe commit 5e11e4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 10 additions & 6 deletions R/cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
#' find_next_vector_element(value = 5, vector = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
#' }
#'
find_next_vector_element <- function(value, vector){
greater_than <- vector[vector > value]
find_next_vector_element <- function(value, vector) {
# Find the elements in the vector that are greater than the specified value
greater_than_value <- vector[vector > value]

if(length(greater_than) == 0){
return(NA)
# If there are no elements greater than the specified value, return NULL or a default value
if (length(greater_than_value) == 0) {
return(max(vector)) # or return a default value as needed
}

next_one <- min(greater_than, na.rm = T)
# Find the minimum value among the elements greater than the specified value
next_element <- min(greater_than_value)

return(next_one)
return(next_element)
}


#' @title Find the previous element of the vector before a value
#' @description Find the previous element of the vector before a value
#' @param value A value of numeric values
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ test_that("Next element after integer in vector works as intended",
{
expect_equal(find_next_vector_element(10, 1:12), 11)
expect_equal(find_next_vector_element(4, seq(1, 120, 5)), 6)
expect_equal(find_next_vector_element(120, seq(1, 120, 5)), 116)
})


test_that("Previous element before integer in vector works as intended",
{
expect_equal(find_previous_vector_element(10, 1:12), 9)
expect_equal(find_previous_vector_element(4, seq(1, 120, 5)), 1)
expect_equal(find_previous_vector_element(1, seq(1, 120, 5)), 1)
})


Expand Down

0 comments on commit 5e11e4c

Please sign in to comment.