Created
February 12, 2020 02:35
-
-
Save selva86/e5959cf78fba6c99d0f9fc3dcedb7942 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
# 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