aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--armstrong-numbers/.exercism/metadata.json1
-rw-r--r--armstrong-numbers/.gitignore8
-rw-r--r--armstrong-numbers/Cargo.toml4
-rw-r--r--armstrong-numbers/README.md94
-rw-r--r--armstrong-numbers/src/lib.rs15
-rw-r--r--armstrong-numbers/tests/armstrong-numbers.rs46
6 files changed, 168 insertions, 0 deletions
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 @@
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/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 @@
1[package]
2edition = "2018"
3name = "armstrong_numbers"
4version = "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 @@
1# Armstrong Numbers
2
3An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number)
4is a number that is the sum of its own digits each raised to the power
5of the number of digits.
6
7For example:
8
9- 9 is an Armstrong number, because `9 = 9^1 = 9`
10- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
11- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
12- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
13
14Write some code to determine whether a number is an Armstrong number.
15
16## Rust Installation
17
18Refer to the [exercism help page][help-page] for Rust installation and learning
19resources.
20
21## Writing the Code
22
23Execute the tests with:
24
25```bash
26$ cargo test
27```
28
29All but the first test have been ignored. After you get the first test to
30pass, open the tests source file which is located in the `tests` directory
31and remove the `#[ignore]` flag from the next test and get the tests to pass
32again. Each separate test is a function with `#[test]` flag above it.
33Continue, until you pass every test.
34
35If you wish to run all ignored tests without editing the tests source file, use:
36
37```bash
38$ cargo test -- --ignored
39```
40
41To run a specific test, for example `some_test`, you can use:
42
43```bash
44$ cargo test some_test
45```
46
47If the specific test is ignored use:
48
49```bash
50$ cargo test some_test -- --ignored
51```
52
53To learn more about Rust tests refer to the [online test documentation][rust-tests]
54
55Make sure to read the [Modules][modules] chapter if you
56haven't already, it will help you with organizing your files.
57
58## Further improvements
59
60After 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.
61
62To format your solution, inside the solution directory use
63
64```bash
65cargo fmt
66```
67
68To see, if your solution contains some common ineffective use cases, inside the solution directory use
69
70```bash
71cargo clippy --all-targets
72```
73
74## Submitting the solution
75
76Generally 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.
77
78## Feedback, Issues, Pull Requests
79
80The [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!
81
82If 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).
83
84[help-page]: https://exercism.io/tracks/rust/learning
85[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
86[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
87[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
88
89## Source
90
91Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
92
93## Submitting Incomplete Solutions
94It'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 @@
1
2fn digits(mut n: u32) -> (usize,Vec<u32>) {
3 let mut v = vec![];
4 while n > 9 {
5 v.push(n % 10);
6 n /= 10;
7 }
8 v.push(n);
9 (v.len(),v)
10}
11
12pub fn is_armstrong_number(num: u32) -> bool {
13 let (l,v) = digits(num);
14 v.iter().map(|x| x.pow(l as u32)).sum::<u32>() == num
15}
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 @@
1use armstrong_numbers::*;
2
3#[test]
4fn test_zero_is_an_armstrong_number() {
5 assert!(is_armstrong_number(0))
6}
7
8#[test]
9fn test_single_digit_numbers_are_armstrong_numbers() {
10 assert!(is_armstrong_number(5))
11}
12
13#[test]
14fn test_there_are_no_2_digit_armstrong_numbers() {
15 assert!(!is_armstrong_number(10))
16}
17
18#[test]
19fn test_three_digit_armstrong_number() {
20 assert!(is_armstrong_number(153))
21}
22
23#[test]
24fn test_three_digit_non_armstrong_number() {
25 assert!(!is_armstrong_number(100))
26}
27
28#[test]
29fn test_four_digit_armstrong_number() {
30 assert!(is_armstrong_number(9474))
31}
32
33#[test]
34fn test_four_digit_non_armstrong_number() {
35 assert!(!is_armstrong_number(9475))
36}
37
38#[test]
39fn test_seven_digit_armstrong_number() {
40 assert!(is_armstrong_number(9_926_315))
41}
42
43#[test]
44fn test_seven_digit_non_armstrong_number() {
45 assert!(!is_armstrong_number(9_926_316))
46}