Last active
October 14, 2017 11:01
-
-
Save piratejon/253630b3bfe137c49de0dffbf6970b79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# time to next event | |
# | |
test.vectors <- list( | |
list( | |
input = c(1,0,0,0,2,3,4,0,0,0), | |
output = c(4,3,2,1,1,1,Inf, Inf, Inf, Inf) | |
), | |
list( | |
input = c(1,0,0,0,2,3,0,0,4,0,0,0), | |
output = c(4,3,2,1,1,3,2,1,Inf, Inf, Inf, Inf) | |
) | |
) | |
time_to_next_event <- function (x) { | |
state <- list( | |
output = c(), | |
counter = 1 | |
) | |
lapply(tail(x, -1), function (a) { | |
if (a == 0) { | |
state$counter <<- state$counter + 1 | |
} else { | |
state$output <<- list(state$output, state$counter:1) | |
state$counter <<- 1 | |
} | |
}) | |
state$output <- unlist(state$output) | |
unlist(list(state$output, rep(Inf, length(x) - length(state$output)))) | |
} | |
run_tests <- function(fn, cases) { | |
for (case in cases) { | |
actual.output <- fn(case$input) | |
strs = list( | |
actual = paste(actual.output, collapse = ','), | |
expected = paste(case$output, collapse = ','), | |
input = paste(case$input, collapse = ',') | |
) | |
if (all(actual.output == case$output)) { | |
print(paste("OK! input:", strs$input, "expected:", strs$expected, "actual:", strs$actual)) | |
} else { | |
print(paste("WRONG! input:", strs$input, "expected:", strs$expected, "actual:", strs$actual)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment