mirror of
https://github.com/ArthurDanjou/exercism-rust.git
synced 2026-01-27 18:30:27 +01:00
Initial commit
This commit is contained in:
50
leap/.exercism/config.json
Normal file
50
leap/.exercism/config.json
Normal file
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"blurb": "Given a year, report if it is a leap year.",
|
||||
"authors": [
|
||||
"EduardoBautista"
|
||||
],
|
||||
"contributors": [
|
||||
"andrewclarkson",
|
||||
"andy5995",
|
||||
"ashleygwilliams",
|
||||
"ClashTheBunny",
|
||||
"coriolinus",
|
||||
"cwhakes",
|
||||
"darnuria",
|
||||
"EduardoBautista",
|
||||
"efx",
|
||||
"Emerentius",
|
||||
"ErikSchierboom",
|
||||
"hunger",
|
||||
"IanWhitney",
|
||||
"JIghtuse",
|
||||
"jonasbn",
|
||||
"kytrinyx",
|
||||
"leoyvens",
|
||||
"lutostag",
|
||||
"mkantor",
|
||||
"navossoc",
|
||||
"nfiles",
|
||||
"petertseng",
|
||||
"pminten",
|
||||
"rofrol",
|
||||
"sshine",
|
||||
"stringparser",
|
||||
"xakon",
|
||||
"ZapAnton"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"src/lib.rs",
|
||||
"Cargo.toml"
|
||||
],
|
||||
"test": [
|
||||
"tests/leap.rs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.rs"
|
||||
]
|
||||
},
|
||||
"source": "JavaRanch Cattle Drive, exercise 3",
|
||||
"source_url": "http://www.javaranch.com/leap.jsp"
|
||||
}
|
||||
1
leap/.exercism/metadata.json
Normal file
1
leap/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"rust","exercise":"leap","id":"c35ab9ae8fba4e40ba03e6eaa273e2ed","url":"https://exercism.org/tracks/rust/exercises/leap","handle":"ArthurDanjou","is_requester":true,"auto_approve":false}
|
||||
8
leap/.gitignore
vendored
Normal file
8
leap/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||
Cargo.lock
|
||||
4
leap/Cargo.toml
Normal file
4
leap/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
edition = "2021"
|
||||
name = "leap"
|
||||
version = "1.6.0"
|
||||
85
leap/HELP.md
Normal file
85
leap/HELP.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, open the tests source file which is located in the `tests` directory
|
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||
again. Each separate test is a function with `#[test]` flag above it.
|
||||
Continue, until you pass every test.
|
||||
|
||||
If you wish to run _only ignored_ tests without editing the tests source file, use:
|
||||
|
||||
```bash
|
||||
$ cargo test -- --ignored
|
||||
```
|
||||
|
||||
If you are using Rust 1.51 or later, you can run _all_ tests with
|
||||
|
||||
```bash
|
||||
$ cargo test -- --include-ignored
|
||||
```
|
||||
|
||||
To run a specific test, for example `some_test`, you can use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test
|
||||
```
|
||||
|
||||
If the specific test is ignored, use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test -- --ignored
|
||||
```
|
||||
|
||||
To learn more about Rust tests refer to the online [test documentation][rust-tests].
|
||||
|
||||
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` 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 [Rust track's documentation](https://exercism.org/docs/tracks/rust)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
## Rust Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Submitting the solution
|
||||
|
||||
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
|
||||
|
||||
If you want to know more about Exercism, take a look at the [contribution guide].
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
||||
[help-page]: https://exercism.org/tracks/rust/learning
|
||||
[github]: https://github.com/exercism/rust
|
||||
[contribution guide]: https://exercism.org/docs/community/contributors
|
||||
72
leap/README.md
Normal file
72
leap/README.md
Normal file
@@ -0,0 +1,72 @@
|
||||
# Leap
|
||||
|
||||
Welcome to Leap on Exercism's Rust Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Given a year, report if it is a leap year.
|
||||
|
||||
The tricky thing here is that a leap year in the Gregorian calendar occurs:
|
||||
|
||||
```text
|
||||
on every year that is evenly divisible by 4
|
||||
except every year that is evenly divisible by 100
|
||||
unless the year is also evenly divisible by 400
|
||||
```
|
||||
|
||||
For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap
|
||||
year, but 2000 is.
|
||||
|
||||
## Notes
|
||||
|
||||
Though our exercise adopts some very simple rules, there is more to
|
||||
learn!
|
||||
|
||||
For a delightful, four minute explanation of the whole leap year
|
||||
phenomenon, go watch [this youtube video][video].
|
||||
|
||||
[video]: http://www.youtube.com/watch?v=xX96xng7sAE
|
||||
|
||||
You may use the [`arithmetic remainder` operator](https://doc.rust-lang.org/book/appendix-02-operators.html) to test for divisibility.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @EduardoBautista
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @andrewclarkson
|
||||
- @andy5995
|
||||
- @ashleygwilliams
|
||||
- @ClashTheBunny
|
||||
- @coriolinus
|
||||
- @cwhakes
|
||||
- @darnuria
|
||||
- @EduardoBautista
|
||||
- @efx
|
||||
- @Emerentius
|
||||
- @ErikSchierboom
|
||||
- @hunger
|
||||
- @IanWhitney
|
||||
- @JIghtuse
|
||||
- @jonasbn
|
||||
- @kytrinyx
|
||||
- @leoyvens
|
||||
- @lutostag
|
||||
- @mkantor
|
||||
- @navossoc
|
||||
- @nfiles
|
||||
- @petertseng
|
||||
- @pminten
|
||||
- @rofrol
|
||||
- @sshine
|
||||
- @stringparser
|
||||
- @xakon
|
||||
- @ZapAnton
|
||||
|
||||
### Based on
|
||||
|
||||
JavaRanch Cattle Drive, exercise 3 - http://www.javaranch.com/leap.jsp
|
||||
8
leap/src/lib.rs
Normal file
8
leap/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub fn is_leap_year(year: u64) -> bool {
|
||||
match (year % 400, year % 100, year % 4) {
|
||||
(0, _, _) => true, // Is divisible by 400
|
||||
(_, 0, _) => false, // Is divisible by 100 and 400
|
||||
(_, _, 0) => true, // Is divisible by 4, 100 and 400
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
100
leap/tests/leap.rs
Normal file
100
leap/tests/leap.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
fn process_leapyear_case(year: u64, expected: bool) {
|
||||
assert_eq!(leap::is_leap_year(year), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_year_not_divisible_by_4_common_year() {
|
||||
process_leapyear_case(2015, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_2_not_divisible_by_4_in_common_year() {
|
||||
process_leapyear_case(1970, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_4_not_divisible_by_100_leap_year() {
|
||||
process_leapyear_case(1996, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_4_and_5_is_still_a_leap_year() {
|
||||
process_leapyear_case(1960, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_100_not_divisible_by_400_common_year() {
|
||||
process_leapyear_case(2100, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year() {
|
||||
process_leapyear_case(1900, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_400_leap_year() {
|
||||
process_leapyear_case(2000, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year() {
|
||||
process_leapyear_case(2400, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_year_divisible_by_200_not_divisible_by_400_common_year() {
|
||||
process_leapyear_case(1800, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_any_old_year() {
|
||||
process_leapyear_case(1997, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_early_years() {
|
||||
process_leapyear_case(1, false);
|
||||
process_leapyear_case(4, true);
|
||||
process_leapyear_case(100, false);
|
||||
process_leapyear_case(400, true);
|
||||
process_leapyear_case(900, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_century() {
|
||||
process_leapyear_case(1700, false);
|
||||
process_leapyear_case(1800, false);
|
||||
process_leapyear_case(1900, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_exceptional_centuries() {
|
||||
process_leapyear_case(1600, true);
|
||||
process_leapyear_case(2000, true);
|
||||
process_leapyear_case(2400, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_years_1600_to_1699() {
|
||||
let incorrect_years = (1600..1700)
|
||||
.filter(|&year| leap::is_leap_year(year) != (year % 4 == 0))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if !incorrect_years.is_empty() {
|
||||
panic!("incorrect result for years: {:?}", incorrect_years);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user