-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1317: Convert Integer to the Sum of Two No-Zero Integers
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/problem_1317_convert_integer_to_the_sum_of_two_no_zero_integers/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn get_no_zero_integers(n: i32) -> Vec<i32> { | ||
fn helper(n: i32) -> bool { | ||
let mut n = n; | ||
while n != 0 { | ||
if n % 10 == 0 { | ||
return false; | ||
} | ||
n /= 10; | ||
} | ||
true | ||
} | ||
for x in 1..=n / 2 { | ||
if helper(x) && helper(n - x) { | ||
return vec![x, n - x]; | ||
} | ||
} | ||
unreachable!() | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn get_no_zero_integers(n: i32) -> Vec<i32> { | ||
Self::get_no_zero_integers(n) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_1317_convert_integer_to_the_sum_of_two_no_zero_integers/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn get_no_zero_integers(n: i32) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | ||
|
||
for n in test_cases { | ||
let [a, b]: [_; 2] = S::get_no_zero_integers(n).try_into().unwrap(); | ||
|
||
assert!(a > 0); | ||
assert!(b > 0); | ||
assert_eq!(a + b, n); | ||
} | ||
} | ||
} |