ambo strategy function to return a random play, and tests for when one of the length of plays is less than 3 and otherwise. I have tried running it many times on a single scenario to estimate the probability for each return but the results are somehow confusing. Question: Write a series for the roshambo function below to test that it works as expected. (this could be testthat::expect_equal or testthat::expect_true or testthat::expect_error tests). Thank you. roshambo <- function(plays){ # return random play
So I have the following roshambo strategy function to return a random play, and tests for when one of the length of plays is less than 3 and otherwise. I have tried running it many times on a single scenario to estimate the probability for each return but the results are somehow confusing.
Question: Write a series for the roshambo function below to test that it works as expected. (this could be testthat::expect_equal or testthat::expect_true or testthat::expect_error tests). Thank you.
roshambo <- function(plays){
# return random play
if (length(plays) < 3){
return(sample(c("R", "P", "S"), 1))
}
# else find most common
most_common <- names(which.max(table(plays)))
if (most_common == "S"){
return("R")
} else if (most_common == "R"){
return("P")
} else {
return("S")
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps