aboutsummaryrefslogtreecommitdiff
path: root/grains
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-12-26 17:48:38 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch)
tree8e39798fcaf27931d91c2088423fd4e97adcfc2d /grains
parent4e2052c4d792540c2f742b2c2a081b11117ed41d (diff)
downloadexercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz
exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip
Move Rust exercises in a subdirectory
Diffstat (limited to 'grains')
-rw-r--r--grains/.exercism/metadata.json1
-rw-r--r--grains/.gitignore8
-rw-r--r--grains/Cargo.toml4
-rw-r--r--grains/README.md107
-rw-r--r--grains/src/lib.rs23
-rw-r--r--grains/tests/grains.rs65
6 files changed, 0 insertions, 208 deletions
diff --git a/grains/.exercism/metadata.json b/grains/.exercism/metadata.json
deleted file mode 100644
index c20eb3a..0000000
--- a/grains/.exercism/metadata.json
+++ /dev/null
@@ -1 +0,0 @@
1{"track":"rust","exercise":"grains","id":"cfeb306e73c0483aa2a6e1fcc8d1029b","url":"https://exercism.io/my/solutions/cfeb306e73c0483aa2a6e1fcc8d1029b","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/grains/.gitignore b/grains/.gitignore
deleted file mode 100644
index db7f315..0000000
--- a/grains/.gitignore
+++ /dev/null
@@ -1,8 +0,0 @@
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/grains/Cargo.toml b/grains/Cargo.toml
deleted file mode 100644
index 6bf141e..0000000
--- a/grains/Cargo.toml
+++ /dev/null
@@ -1,4 +0,0 @@
1[package]
2edition = "2018"
3name = "grains"
4version = "1.2.0"
diff --git a/grains/README.md b/grains/README.md
deleted file mode 100644
index e54de2c..0000000
--- a/grains/README.md
+++ /dev/null
@@ -1,107 +0,0 @@
1# Grains
2
3Calculate the number of grains of wheat on a chessboard given that the number
4on each square doubles.
5
6There once was a wise servant who saved the life of a prince. The king
7promised to pay whatever the servant could dream up. Knowing that the
8king loved chess, the servant told the king he would like to have grains
9of wheat. One grain on the first square of a chess board, with the number
10of grains doubling on each successive square.
11
12There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
13
14Write code that shows:
15- how many grains were on a given square, and
16- the total number of grains on the chessboard
17
18## For bonus points
19
20Did you get the tests passing and the code clean? If you want to, these
21are some additional things you could try:
22
23- Optimize for speed.
24- Optimize for readability.
25
26Then please share your thoughts in a comment on the submission. Did this
27experiment make the code better? Worse? Did you learn anything from it?
28
29## Rust Installation
30
31Refer to the [exercism help page][help-page] for Rust installation and learning
32resources.
33
34## Writing the Code
35
36Execute the tests with:
37
38```bash
39$ cargo test
40```
41
42All but the first test have been ignored. After you get the first test to
43pass, open the tests source file which is located in the `tests` directory
44and remove the `#[ignore]` flag from the next test and get the tests to pass
45again. Each separate test is a function with `#[test]` flag above it.
46Continue, until you pass every test.
47
48If you wish to run all ignored tests without editing the tests source file, use:
49
50```bash
51$ cargo test -- --ignored
52```
53
54To run a specific test, for example `some_test`, you can use:
55
56```bash
57$ cargo test some_test
58```
59
60If the specific test is ignored use:
61
62```bash
63$ cargo test some_test -- --ignored
64```
65
66To learn more about Rust tests refer to the [online test documentation][rust-tests]
67
68Make sure to read the [Modules][modules] chapter if you
69haven't already, it will help you with organizing your files.
70
71## Further improvements
72
73After 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.
74
75To format your solution, inside the solution directory use
76
77```bash
78cargo fmt
79```
80
81To see, if your solution contains some common ineffective use cases, inside the solution directory use
82
83```bash
84cargo clippy --all-targets
85```
86
87## Submitting the solution
88
89Generally 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.
90
91## Feedback, Issues, Pull Requests
92
93The [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!
94
95If 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).
96
97[help-page]: https://exercism.io/tracks/rust/learning
98[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
99[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
100[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
101
102## Source
103
104JavaRanch Cattle Drive, exercise 6 [http://www.javaranch.com/grains.jsp](http://www.javaranch.com/grains.jsp)
105
106## Submitting Incomplete Solutions
107It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/grains/src/lib.rs b/grains/src/lib.rs
deleted file mode 100644
index 05449be..0000000
--- a/grains/src/lib.rs
+++ /dev/null
@@ -1,23 +0,0 @@
1pub fn square(s: u32) -> u64 {
2 assert!(s > 0 && s < 65,"Square must be between 1 and 64");
3 2_u64.pow(s-1)
4}
5
6/// This version uses the already defined `square(s)` function.
7///
8/// There is also a faster way to compute this according to
9/// [Wolfram|Alpha](https://www.wolframalpha.com/input/?i=sum_0%5En+2%5En)
10///
11/// 2_u64.pow(65) - 1 == 18_446_744_073_709_551_615
12///
13/// this solution however doesn't work because
14///
15/// 2_u64.pow(65) == 18_446_744_073_709_551_616
16///
17/// which overflows since
18///
19/// std::u64::MAX == 18_446_744_073_709_551_615
20///
21pub fn total() -> u64 {
22 (1..65).map(square).sum() // std::u64::MAX
23}
diff --git a/grains/tests/grains.rs b/grains/tests/grains.rs
deleted file mode 100644
index 405eb4d..0000000
--- a/grains/tests/grains.rs
+++ /dev/null
@@ -1,65 +0,0 @@
1use grains;
2
3fn process_square_case(input: u32, expected: u64) {
4 assert_eq!(grains::square(input), expected);
5}
6
7#[test]
8/// 1
9fn test_1() {
10 process_square_case(1, 1);
11}
12
13#[test]
14/// 2
15fn test_2() {
16 process_square_case(2, 2);
17}
18
19#[test]
20/// 3
21fn test_3() {
22 process_square_case(3, 4);
23}
24
25#[test]
26/// 4
27fn test_4() {
28 process_square_case(4, 8);
29}
30
31//NEW
32#[test]
33/// 16
34fn test_16() {
35 process_square_case(16, 32_768);
36}
37
38#[test]
39/// 32
40fn test_32() {
41 process_square_case(32, 2_147_483_648);
42}
43
44#[test]
45/// 64
46fn test_64() {
47 process_square_case(64, 9_223_372_036_854_775_808);
48}
49
50#[test]
51#[should_panic(expected = "Square must be between 1 and 64")]
52fn test_square_0_raises_an_exception() {
53 grains::square(0);
54}
55
56#[test]
57#[should_panic(expected = "Square must be between 1 and 64")]
58fn test_square_greater_than_64_raises_an_exception() {
59 grains::square(65);
60}
61
62#[test]
63fn test_returns_the_total_number_of_grains_on_the_board() {
64 assert_eq!(grains::total(), 18_446_744_073_709_551_615);
65}