aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico I <git@federicoigne.com>2020-03-14 22:50:23 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:49:42 +0000
commit06bacfaeebe3949952cc9b8d9ec219dc9df8ff29 (patch)
tree636cef80fba5224933e84a81c2a22df2a1af24ab
parentdede893978d55ec6ee0751808b44a0f647988110 (diff)
downloadexercism-06bacfaeebe3949952cc9b8d9ec219dc9df8ff29.tar.gz
exercism-06bacfaeebe3949952cc9b8d9ec219dc9df8ff29.zip
[rust] Leap
-rw-r--r--leap/.exercism/metadata.json1
-rw-r--r--leap/.gitignore8
-rw-r--r--leap/Cargo.toml4
-rw-r--r--leap/README.md105
-rw-r--r--leap/src/lib.rs3
-rw-r--r--leap/tests/leap.rs89
6 files changed, 210 insertions, 0 deletions
diff --git a/leap/.exercism/metadata.json b/leap/.exercism/metadata.json
new file mode 100644
index 0000000..1ec33f3
--- /dev/null
+++ b/leap/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"leap","id":"bc3b9538660b445abd5c2ee1398141db","url":"https://exercism.io/my/solutions/bc3b9538660b445abd5c2ee1398141db","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/leap/.gitignore b/leap/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/leap/.gitignore
@@ -0,0 +1,8 @@
1# Generated by Cargo
2# will have compiled files and executables
3/target/
4**/*.rs.bk
5
6# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8Cargo.lock
diff --git a/leap/Cargo.toml b/leap/Cargo.toml
new file mode 100644
index 0000000..c125e67
--- /dev/null
+++ b/leap/Cargo.toml
@@ -0,0 +1,4 @@
1[package]
2edition = "2018"
3name = "leap"
4version = "1.6.0"
diff --git a/leap/README.md b/leap/README.md
new file mode 100644
index 0000000..6a6bac4
--- /dev/null
+++ b/leap/README.md
@@ -0,0 +1,105 @@
1# Leap Given a year, report if it is a leap year.
2
3The tricky thing here is that a leap year in the Gregorian calendar occurs:
4
5```text
6on every year that is evenly divisible by 4
7except every year that is evenly divisible by 100
8 unless the year is also evenly divisible by 400
9```
10
11For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap
12year, but 2000 is.
13
14## Notes
15
16Though our exercise adopts some very simple rules, there is more to
17learn!
18
19For a delightful, four minute explanation of the whole leap year
20phenomenon, go watch [this youtube video][video].
21
22[video]: http://www.youtube.com/watch?v=xX96xng7sAE
23
24You may use the [`arithmetic remainder` operator](https://doc.rust-lang.org/book/appendix-02-operators.html) to test for divisibility.
25
26
27## Rust Installation
28
29Refer to the [exercism help page][help-page] for Rust installation and learning
30resources.
31
32## Writing the Code
33
34Execute the tests with:
35
36```bash
37$ cargo test
38```
39
40All but the first test have been ignored. After you get the first test to
41pass, open the tests source file which is located in the `tests` directory
42and remove the `#[ignore]` flag from the next test and get the tests to pass
43again. Each separate test is a function with `#[test]` flag above it.
44Continue, until you pass every test.
45
46If you wish to run all ignored tests without editing the tests source file, use:
47
48```bash
49$ cargo test -- --ignored
50```
51
52To run a specific test, for example `some_test`, you can use:
53
54```bash
55$ cargo test some_test
56```
57
58If the specific test is ignored use:
59
60```bash
61$ cargo test some_test -- --ignored
62```
63
64To learn more about Rust tests refer to the [online test documentation][rust-tests]
65
66Make sure to read the [Modules][modules] chapter if you
67haven't already, it will help you with organizing your files.
68
69## Further improvements
70
71After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
72
73To format your solution, inside the solution directory use
74
75```bash
76cargo fmt
77```
78
79To see, if your solution contains some common ineffective use cases, inside the solution directory use
80
81```bash
82cargo clippy --all-targets
83```
84
85## Submitting the solution
86
87Generally 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.
88
89## Feedback, Issues, Pull Requests
90
91The [exercism/rust](https://github.com/exercism/rust) repository on 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!
92
93If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
94
95[help-page]: https://exercism.io/tracks/rust/learning
96[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
97[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
98[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
99
100## Source
101
102JavaRanch Cattle Drive, exercise 3 [http://www.javaranch.com/leap.jsp](http://www.javaranch.com/leap.jsp)
103
104## Submitting Incomplete Solutions
105It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/leap/src/lib.rs b/leap/src/lib.rs
new file mode 100644
index 0000000..1b40300
--- /dev/null
+++ b/leap/src/lib.rs
@@ -0,0 +1,3 @@
1pub fn is_leap_year(year: u64) -> bool {
2 year % 4 == 0 && year % 100 != 0 || year % 400 == 0
3}
diff --git a/leap/tests/leap.rs b/leap/tests/leap.rs
new file mode 100644
index 0000000..88d2c77
--- /dev/null
+++ b/leap/tests/leap.rs
@@ -0,0 +1,89 @@
1use leap;
2
3fn process_leapyear_case(year: u64, expected: bool) {
4 assert_eq!(leap::is_leap_year(year), expected);
5}
6
7#[test]
8fn test_year_not_divisible_by_4_common_year() {
9 process_leapyear_case(2015, false);
10}
11
12#[test]
13fn test_year_divisible_by_2_not_divisible_by_4_in_common_year() {
14 process_leapyear_case(1970, false);
15}
16
17#[test]
18fn test_year_divisible_by_4_not_divisible_by_100_leap_year() {
19 process_leapyear_case(1996, true);
20}
21
22#[test]
23fn test_year_divisible_by_4_and_5_is_still_a_leap_year() {
24 process_leapyear_case(1960, true);
25}
26
27#[test]
28fn test_year_divisible_by_100_not_divisible_by_400_common_year() {
29 process_leapyear_case(2100, false);
30}
31
32#[test]
33fn test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year() {
34 process_leapyear_case(1900, false);
35}
36
37#[test]
38fn test_year_divisible_by_400_leap_year() {
39 process_leapyear_case(2000, true);
40}
41
42#[test]
43fn test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year() {
44 process_leapyear_case(2400, true);
45}
46
47#[test]
48fn test_year_divisible_by_200_not_divisible_by_400_common_year() {
49 process_leapyear_case(1800, false);
50}
51
52#[test]
53fn test_any_old_year() {
54 assert_eq!(leap::is_leap_year(1997), false);
55}
56
57#[test]
58fn test_early_years() {
59 assert_eq!(leap::is_leap_year(1), false);
60 assert_eq!(leap::is_leap_year(4), true);
61 assert_eq!(leap::is_leap_year(100), false);
62 assert_eq!(leap::is_leap_year(400), true);
63 assert_eq!(leap::is_leap_year(900), false);
64}
65
66#[test]
67fn test_century() {
68 assert_eq!(leap::is_leap_year(1700), false);
69 assert_eq!(leap::is_leap_year(1800), false);
70 assert_eq!(leap::is_leap_year(1900), false);
71}
72
73#[test]
74fn test_exceptional_centuries() {
75 assert_eq!(leap::is_leap_year(1600), true);
76 assert_eq!(leap::is_leap_year(2000), true);
77 assert_eq!(leap::is_leap_year(2400), true);
78}
79
80#[test]
81fn test_years_1600_to_1699() {
82 let incorrect_years = (1600..1700)
83 .filter(|&year| leap::is_leap_year(year) != (year % 4 == 0))
84 .collect::<Vec<_>>();
85
86 if !incorrect_years.is_empty() {
87 panic!("incorrect result for years: {:?}", incorrect_years);
88 }
89}