diff --git a/r/README.md b/r/README.md index 7b37aa6d..4686b722 100644 --- a/r/README.md +++ b/r/README.md @@ -14,3 +14,4 @@ ## [Exercises](https://exercism.org/tracks/r/exercises) - [raindrops](./raindrops/README.md) +- [leap](./leap/README.md) diff --git a/r/leap/.exercism/config.json b/r/leap/.exercism/config.json new file mode 100644 index 00000000..481e6145 --- /dev/null +++ b/r/leap/.exercism/config.json @@ -0,0 +1,24 @@ +{ + "authors": [ + "jonmcalder" + ], + "contributors": [ + "jonboiser", + "katrinleinweber", + "zacchaeusluke" + ], + "files": { + "solution": [ + "leap.R" + ], + "test": [ + "test_leap.R" + ], + "example": [ + ".meta/example.R" + ] + }, + "blurb": "Determine whether a given year is a leap year.", + "source": "CodeRanch Cattle Drive, Assignment 3", + "source_url": "https://coderanch.com/t/718816/Leap" +} diff --git a/r/leap/.exercism/metadata.json b/r/leap/.exercism/metadata.json new file mode 100644 index 00000000..f0701656 --- /dev/null +++ b/r/leap/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"r","exercise":"leap","id":"f0cd17767f7b4b34ae508898ddceec37","url":"https://exercism.org/tracks/r/exercises/leap","handle":"vpayno","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/r/leap/HELP.md b/r/leap/HELP.md new file mode 100644 index 00000000..c1592db2 --- /dev/null +++ b/r/leap/HELP.md @@ -0,0 +1,38 @@ +# Help + +## Running the tests + +Tests require the `{testthat}` package to be installed in R. +To run the tests for an exercise, simply execute the `test_.R` script from within the exercise's directory. + +This can be conveniently done with [testthat's `auto_test` function](https://testthat.r-lib.org/reference/auto_test.html). Because exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_.R`. + +See the [tests page](https://exercism.org/docs/tracks/r/tests) for more information. + +## Submitting your solution + +You can submit your solution using the `exercism submit leap.R` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [R track's documentation](https://exercism.org/docs/tracks/r) +- The [R track's programming category on the forum](https://forum.exercism.org/c/programming/r) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can try one of the following resources: + +- [StackOverflow](https://stackoverflow.com/questions/tagged/r) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +- [#rstats](https://twitter.com/search?q=%23rstats) is the hashtag to use if you are asking for help or guidance on Twitter. The R community is very active on Twitter and always try to help those who are new to R. +- [/r/rstats](https://www.reddit.com/r/rstats) is the R subreddit. +- [RStudio Community](https://community.rstudio.com/) is another active and helpful R community. \ No newline at end of file diff --git a/r/leap/README.md b/r/leap/README.md new file mode 100644 index 00000000..4451c3d5 --- /dev/null +++ b/r/leap/README.md @@ -0,0 +1,46 @@ +# Leap + +Welcome to Leap on Exercism's R Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +A leap year (in the Gregorian calendar) occurs: + +- In every year that is evenly divisible by 4. +- Unless the year is evenly divisible by 100, in which case it's only a leap year if the year is also evenly divisible by 400. + +Some examples: + +- 1997 was not a leap year as it's not divisible by 4. +- 1900 was not a leap year as it's not divisible by 400. +- 2000 was a leap year! + +~~~~exercism/note +For a delightful, four-minute explanation of the whole phenomenon of leap years, check out [this YouTube video](https://www.youtube.com/watch?v=xX96xng7sAE). +~~~~ + +## Instructions + +Your task is to determine whether a given year is a leap year. + +## Source + +### Created by + +- @jonmcalder + +### Contributed to by + +- @jonboiser +- @katrinleinweber +- @zacchaeusluke + +### Based on + +CodeRanch Cattle Drive, Assignment 3 - https://coderanch.com/t/718816/Leap + +### My Solution + +- [leap.R](./leap.R) +- [run-tests](./run-tests-r.txt) diff --git a/r/leap/leap.R b/r/leap/leap.R new file mode 100644 index 00000000..e0874fed --- /dev/null +++ b/r/leap/leap.R @@ -0,0 +1,15 @@ +leap <- function(year) { + if (year %% 400 == 0) { + return(TRUE) + } + + if (year %% 100 == 0) { + return(FALSE) + } + + if (year %% 4 == 0) { + return(TRUE) + } + + return(FALSE) +} diff --git a/r/leap/run-tests-r.txt b/r/leap/run-tests-r.txt new file mode 100644 index 00000000..aec670cd --- /dev/null +++ b/r/leap/run-tests-r.txt @@ -0,0 +1,65 @@ +Running automated test file(s): + + +=============================================================================== + +Running: ../../.github/citools/r/r-test + +Running R Tests + +R versions: + + R version 4.3.3 (2024-02-29) -- "Angel Food Cake" + Copyright (C) 2024 The R Foundation for Statistical Computing + Platform: x86_64-pc-linux-gnu (64-bit) + + R is free software and comes with ABSOLUTELY NO WARRANTY. + You are welcome to redistribute it under the terms of the + GNU General Public License versions 2 or 3. + For more information about these matters see + https://www.gnu.org/licenses/. + + + Rscript (R) version 4.3.3 (2024-02-29) + + + ============================================================================== + +Running: Rscript test_leap.R + +Test passed 🎉 +Test passed 🎉 +Test passed 🎉 +Test passed 🎉 + +real 0m0.586s +user 0m0.525s +sys 0m0.063s + + + ============================================================================== + +Exit code: 0 + +real 0m0.790s +user 0m0.619s +sys 0m0.185s + +real 0m0.794s +user 0m0.622s +sys 0m0.187s + +=============================================================================== + +Running: misspell ./leap.R ./test_leap.R + +real 0m0.018s +user 0m0.014s +sys 0m0.012s + +=============================================================================== + +/home/vpayno/git_vpayno/exercism-workspace/r + +=============================================================================== + diff --git a/r/leap/test_leap.R b/r/leap/test_leap.R new file mode 100644 index 00000000..1fb20a43 --- /dev/null +++ b/r/leap/test_leap.R @@ -0,0 +1,22 @@ +source("./leap.R") +library(testthat) + +test_that("year not divisible by 4: common year", { + year <- 2015 + expect_equal(leap(year), FALSE) +}) + +test_that("year divisible by 4, not divisible by 100: leap year", { + year <- 2016 + expect_equal(leap(year), TRUE) +}) + +test_that("year divisible by 100, not divisible by 400: common year", { + year <- 2100 + expect_equal(leap(year), FALSE) +}) + +test_that("year divisible by 400: leap year", { + year <- 2000 + expect_equal(leap(year), TRUE) +})