-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathleap-year.r
42 lines (32 loc) · 846 Bytes
/
leap-year.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
is_leap_year <- function(year) {
if (year %% 4 != 0) {
return(FALSE)
}
return (year %% 100 != 0 || year %% 400 == 0)
}
test_leap_year <- function() {
leap_years <- c(1804, 1820, 1844, 1852, 1940, 1984, 2000, 2008, 2020, 2024, 2252)
for (year in leap_years) {
if (!is_leap_year(year)) {
cat("Wrong answer for:", year, "\n")
return(FALSE)
}
def_not_leap_year <- year + 1
if (is_leap_year(def_not_leap_year)) {
cat("Wrong answer for:", def_not_leap_year, "\n")
return(FALSE)
}
}
return(TRUE)
}
main <- function() {
if (!test_leap_year()) {
stop("Test failed!")
}
years_from_example <- c(1900, 2000, 2023, 2024)
for (year in years_from_example) {
ans <- ifelse(is_leap_year(year), "Yes!", "No.")
cat("Is", year, "a leap year?", ans, "\n")
}
}
main()