Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R leap #352

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions r/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
## [Exercises](https://exercism.org/tracks/r/exercises)

- [raindrops](./raindrops/README.md)
- [leap](./leap/README.md)
24 changes: 24 additions & 0 deletions r/leap/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions r/leap/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -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}
38 changes: 38 additions & 0 deletions r/leap/HELP.md
Original file line number Diff line number Diff line change
@@ -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_<exercise_name>.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_<exercise_name>.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.
46 changes: 46 additions & 0 deletions r/leap/README.md
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions r/leap/leap.R
Original file line number Diff line number Diff line change
@@ -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)
}
65 changes: 65 additions & 0 deletions r/leap/run-tests-r.txt
Original file line number Diff line number Diff line change
@@ -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

===============================================================================

22 changes: 22 additions & 0 deletions r/leap/test_leap.R
Original file line number Diff line number Diff line change
@@ -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)
})
Loading