From 45530375a9515ac2b4091ffe277221f9380e68f2 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 31 May 2024 23:42:26 +0200 Subject: Sovle grains --- rust/grains/.exercism/config.json | 37 ++++++++++++++++ rust/grains/.exercism/metadata.json | 1 + rust/grains/.gitignore | 8 ++++ rust/grains/Cargo.toml | 4 ++ rust/grains/HELP.md | 86 +++++++++++++++++++++++++++++++++++++ rust/grains/README.md | 48 +++++++++++++++++++++ rust/grains/src/lib.rs | 10 +++++ rust/grains/tests/grains.rs | 55 ++++++++++++++++++++++++ 8 files changed, 249 insertions(+) create mode 100644 rust/grains/.exercism/config.json create mode 100644 rust/grains/.exercism/metadata.json create mode 100644 rust/grains/.gitignore create mode 100644 rust/grains/Cargo.toml create mode 100644 rust/grains/HELP.md create mode 100644 rust/grains/README.md create mode 100644 rust/grains/src/lib.rs create mode 100644 rust/grains/tests/grains.rs diff --git a/rust/grains/.exercism/config.json b/rust/grains/.exercism/config.json new file mode 100644 index 0000000..21f042d --- /dev/null +++ b/rust/grains/.exercism/config.json @@ -0,0 +1,37 @@ +{ + "authors": [ + "IanWhitney" + ], + "contributors": [ + "ClashTheBunny", + "coriolinus", + "cwhakes", + "efx", + "ErikSchierboom", + "krodyrobi", + "lutostag", + "navossoc", + "nfiles", + "petertseng", + "rofrol", + "stringparser", + "TheDarkula", + "xakon", + "ZapAnton" + ], + "files": { + "solution": [ + "src/lib.rs", + "Cargo.toml" + ], + "test": [ + "tests/grains.rs" + ], + "example": [ + ".meta/example.rs" + ] + }, + "blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", + "source": "The CodeRanch Cattle Drive, Assignment 6", + "source_url": "https://coderanch.com/wiki/718824/Grains" +} diff --git a/rust/grains/.exercism/metadata.json b/rust/grains/.exercism/metadata.json new file mode 100644 index 0000000..ef52490 --- /dev/null +++ b/rust/grains/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"grains","id":"592aff3d25c74b5ea831250990bee416","url":"https://exercism.org/tracks/rust/exercises/grains","handle":"dim13","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/grains/.gitignore b/rust/grains/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/grains/.gitignore @@ -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 diff --git a/rust/grains/Cargo.toml b/rust/grains/Cargo.toml new file mode 100644 index 0000000..077822d --- /dev/null +++ b/rust/grains/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2021" +name = "grains" +version = "1.2.0" diff --git a/rust/grains/HELP.md b/rust/grains/HELP.md new file mode 100644 index 0000000..67add76 --- /dev/null +++ b/rust/grains/HELP.md @@ -0,0 +1,86 @@ +# 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) +- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust) +- [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. + +## 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 \ No newline at end of file diff --git a/rust/grains/README.md b/rust/grains/README.md new file mode 100644 index 0000000..0c3eee2 --- /dev/null +++ b/rust/grains/README.md @@ -0,0 +1,48 @@ +# Grains + +Welcome to Grains on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. + +There once was a wise servant who saved the life of a prince. +The king promised to pay whatever the servant could dream up. +Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. +One grain on the first square of a chess board, with the number of grains doubling on each successive square. + +There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). + +Write code that shows: + +- how many grains were on a given square, and +- the total number of grains on the chessboard + +## Source + +### Created by + +- @IanWhitney + +### Contributed to by + +- @ClashTheBunny +- @coriolinus +- @cwhakes +- @efx +- @ErikSchierboom +- @krodyrobi +- @lutostag +- @navossoc +- @nfiles +- @petertseng +- @rofrol +- @stringparser +- @TheDarkula +- @xakon +- @ZapAnton + +### Based on + +The CodeRanch Cattle Drive, Assignment 6 - https://coderanch.com/wiki/718824/Grains \ No newline at end of file diff --git a/rust/grains/src/lib.rs b/rust/grains/src/lib.rs new file mode 100644 index 0000000..9caa693 --- /dev/null +++ b/rust/grains/src/lib.rs @@ -0,0 +1,10 @@ +pub fn square(s: u32) -> u64 { + match s { + 1..=64 => 2_u64.pow(s-1), + _ => panic!("Square must be between 1 and 64") + } +} + +pub fn total() -> u64 { + (1..=64).map(square).sum() +} diff --git a/rust/grains/tests/grains.rs b/rust/grains/tests/grains.rs new file mode 100644 index 0000000..a2a4ba9 --- /dev/null +++ b/rust/grains/tests/grains.rs @@ -0,0 +1,55 @@ +fn process_square_case(input: u32, expected: u64) { + assert_eq!(grains::square(input), expected); +} + +#[test] +fn one() { + process_square_case(1, 1); +} + +#[test] +fn two() { + process_square_case(2, 2); +} + +#[test] +fn three() { + process_square_case(3, 4); +} + +#[test] +fn four() { + process_square_case(4, 8); +} + +#[test] +fn sixteen() { + process_square_case(16, 32_768); +} + +#[test] +fn thirty_two() { + process_square_case(32, 2_147_483_648); +} + +#[test] +fn sixty_four() { + process_square_case(64, 9_223_372_036_854_775_808); +} + +#[test] +#[should_panic(expected = "Square must be between 1 and 64")] +fn square_0_raises_an_exception() { + grains::square(0); +} + +#[test] +#[should_panic(expected = "Square must be between 1 and 64")] +fn square_greater_than_64_raises_an_exception() { + grains::square(65); +} + +#[test] +fn returns_the_total_number_of_grains_on_the_board() { + assert_eq!(grains::total(), 18_446_744_073_709_551_615); +} -- cgit v1.2.3