Created
February 22, 2020 11:34
-
-
Save selva86/134dab2f4840f46e3fd46f6acc0a8121 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. | |
source('https://bit.ly/2w2WA9k') | |
# OR | |
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