From 0fa3d7fe5a27b87bcffb89fb30ffd294a0c2c40a Mon Sep 17 00:00:00 2001 From: Federico I Date: Sun, 29 Mar 2020 21:14:32 +0100 Subject: [rust] Armstrong Numbers --- armstrong-numbers/.exercism/metadata.json | 1 + armstrong-numbers/.gitignore | 8 +++ armstrong-numbers/Cargo.toml | 4 ++ armstrong-numbers/README.md | 94 ++++++++++++++++++++++++++++ armstrong-numbers/src/lib.rs | 15 +++++ armstrong-numbers/tests/armstrong-numbers.rs | 46 ++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 armstrong-numbers/.exercism/metadata.json create mode 100644 armstrong-numbers/.gitignore create mode 100644 armstrong-numbers/Cargo.toml create mode 100644 armstrong-numbers/README.md create mode 100644 armstrong-numbers/src/lib.rs create mode 100644 armstrong-numbers/tests/armstrong-numbers.rs diff --git a/armstrong-numbers/.exercism/metadata.json b/armstrong-numbers/.exercism/metadata.json new file mode 100644 index 0000000..cfda14e --- /dev/null +++ b/armstrong-numbers/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"armstrong-numbers","id":"f53447212bdd4e1c96e26911eb961199","url":"https://exercism.io/my/solutions/f53447212bdd4e1c96e26911eb961199","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/armstrong-numbers/.gitignore b/armstrong-numbers/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/armstrong-numbers/.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/armstrong-numbers/Cargo.toml b/armstrong-numbers/Cargo.toml new file mode 100644 index 0000000..22e66de --- /dev/null +++ b/armstrong-numbers/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2018" +name = "armstrong_numbers" +version = "1.1.0" diff --git a/armstrong-numbers/README.md b/armstrong-numbers/README.md new file mode 100644 index 0000000..6437ffe --- /dev/null +++ b/armstrong-numbers/README.md @@ -0,0 +1,94 @@ +# Armstrong Numbers + +An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) +is a number that is the sum of its own digits each raised to the power +of the number of digits. + +For example: + +- 9 is an Armstrong number, because `9 = 9^1 = 9` +- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1` +- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153` +- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` + +Write some code to determine whether a number is an Armstrong number. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +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 all ignored tests without editing the tests source file, use: + +```bash +$ cargo test -- --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] + +Make sure to read the [Modules][modules] chapter if you +haven't already, it will help you with organizing your files. + +## Further improvements + +After 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. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## 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 [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! + +If 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). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Source + +Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/armstrong-numbers/src/lib.rs b/armstrong-numbers/src/lib.rs new file mode 100644 index 0000000..41b0824 --- /dev/null +++ b/armstrong-numbers/src/lib.rs @@ -0,0 +1,15 @@ + +fn digits(mut n: u32) -> (usize,Vec) { + let mut v = vec![]; + while n > 9 { + v.push(n % 10); + n /= 10; + } + v.push(n); + (v.len(),v) +} + +pub fn is_armstrong_number(num: u32) -> bool { + let (l,v) = digits(num); + v.iter().map(|x| x.pow(l as u32)).sum::() == num +} diff --git a/armstrong-numbers/tests/armstrong-numbers.rs b/armstrong-numbers/tests/armstrong-numbers.rs new file mode 100644 index 0000000..9ac51d0 --- /dev/null +++ b/armstrong-numbers/tests/armstrong-numbers.rs @@ -0,0 +1,46 @@ +use armstrong_numbers::*; + +#[test] +fn test_zero_is_an_armstrong_number() { + assert!(is_armstrong_number(0)) +} + +#[test] +fn test_single_digit_numbers_are_armstrong_numbers() { + assert!(is_armstrong_number(5)) +} + +#[test] +fn test_there_are_no_2_digit_armstrong_numbers() { + assert!(!is_armstrong_number(10)) +} + +#[test] +fn test_three_digit_armstrong_number() { + assert!(is_armstrong_number(153)) +} + +#[test] +fn test_three_digit_non_armstrong_number() { + assert!(!is_armstrong_number(100)) +} + +#[test] +fn test_four_digit_armstrong_number() { + assert!(is_armstrong_number(9474)) +} + +#[test] +fn test_four_digit_non_armstrong_number() { + assert!(!is_armstrong_number(9475)) +} + +#[test] +fn test_seven_digit_armstrong_number() { + assert!(is_armstrong_number(9_926_315)) +} + +#[test] +fn test_seven_digit_non_armstrong_number() { + assert!(!is_armstrong_number(9_926_316)) +} -- cgit v1.2.3