File tree 2 files changed +46
-11
lines changed
exercism/rust/armstrong-numbers/src
2 files changed +46
-11
lines changed Original file line number Diff line number Diff line change 1
1
repos :
2
- - repo : https://github.com/doublify/pre-commit-rust
3
- rev : v1.0
2
+ # - repo: https://github.com/doublify/pre-commit-rust
3
+ # rev: v1.0
4
+ # hooks:
5
+ # - id: fmt
6
+ # - id: cargo-check
7
+ # - id: clippy
8
+ - repo : https://github.com/pre-commit/pre-commit-hooks
9
+ rev : v4.4.0
4
10
hooks :
5
- - id : fmt
6
- - id : cargo-check
7
- - id : clippy
11
+ - id : end-of-file-fixer
12
+ - id : trailing-whitespace
Original file line number Diff line number Diff line change 1
1
pub fn is_armstrong_number ( num : u32 ) -> bool {
2
- let digits = digits ( num) ;
3
- let exponent = digits. len ( ) as u32 ;
2
+ let digits = Digits :: new ( num) ;
3
+ let exponent = digits. num_digits ;
4
4
5
5
if exponent >= 10 {
6
+ // skip the calculation if the exponent is too large
6
7
return false ;
7
8
}
8
9
9
- num == digits . iter ( ) . map ( |digit| digit. pow ( exponent) ) . sum ( )
10
+ num == Digits :: new ( num ) . map ( |digit| digit. pow ( exponent) ) . sum ( )
10
11
}
11
12
12
- fn digits ( num : u32 ) -> Vec < u32 > {
13
+ struct Digits {
14
+ num : u32 ,
15
+ num_digits : u32 ,
16
+ }
17
+
18
+ impl Digits {
19
+ fn new ( num : u32 ) -> Self {
20
+ Digits {
21
+ num,
22
+ num_digits : number_of_digits ( num) ,
23
+ }
24
+ }
25
+ }
26
+
27
+ impl Iterator for Digits {
28
+ type Item = u32 ;
29
+
30
+ fn next ( & mut self ) -> Option < Self :: Item > {
31
+ if self . num_digits == 0 {
32
+ None
33
+ } else {
34
+ let digit = self . num % 10 ;
35
+ self . num /= 10 ;
36
+ self . num_digits -= 1 ;
37
+ Some ( digit)
38
+ }
39
+ }
40
+ }
41
+
42
+ fn number_of_digits ( num : u32 ) -> u32 {
13
43
if num < 10 {
14
- vec ! [ num ]
44
+ 1
15
45
} else {
16
- [ vec ! [ num % 10 ] , digits ( num / 10 ) ] . concat ( )
46
+ 1 + number_of_digits ( num / 10 )
17
47
}
18
48
}
You can’t perform that action at this time.
0 commit comments