r - Referring values in a matrix (extended) -
i have r code friend takes numeric vector, , each number checks if number same upside down.
numbers = (0:9) upside_down_numbers = c(0,1,na,na,na,na,9,na,8,6) ref_table = rbind(numbers,upside_down_numbers) invertible = function(n){ as.numeric(paste0( upside_down_numbers[ 1+rev(as.numeric(strsplit(as.character(n),"")[[1]])) ],collapse=""))}
i want return true if number entered same number returned example:
invertible(69) [1] 69
and false if number entered different number returned or returns na example:
invertible(99) [66]
i want code able work on multiple numbers example:
invertible(c(1, 96, 99, 123, 1691)) [1] true true false false true
really stuck @ point, appreciate advice. thank you
this unusual task... work you.
invertible_test <- function(n) { inverted <- suppresswarnings(invertible(n)) if (is.na(inverted)) { return(false) } return(n == inverted) } invertible_test_vector <- function(v) { vapply(v, invertible_test, c(t)) }
here passes test:
invertible_test_vector(c(1, 96, 99, 123, 1691)) [1] true true false false true
Comments
Post a Comment