Skip to content

Instantly share code, notes, and snippets.

@selva86
Created February 12, 2020 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selva86/e5959cf78fba6c99d0f9fc3dcedb7942 to your computer and use it in GitHub Desktop.
Save selva86/e5959cf78fba6c99d0f9fc3dcedb7942 to your computer and use it in GitHub Desktop.
# Add error handling to `largest_hypotenuse()` so it ignores incorrect cases and captures largest
# hypotenuse for eligible cases.
hypotenuse <- function(side1, side2){
side1 <- as.numeric(side1); side2 <- as.numeric(side2)
sqrt(side1^2 + side2^2)
}
largest_hypotenuse <- function(first, second){
for(i in 1:15){
h <- hypotenuse(first[i], second[i])
if(h > largest_val){
largest_val <<- h
}
}
}
set.seed(100)
first <- sample(10:20, 15, replace = T)
second <- sample(c(10:20, letters), 15, replace = T)
# Call largest_hypotenuse -> errors out
largest_val <- 0
largest_hypotenuse(first, second)
largest_val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment