Skip to content

Commit

Permalink
Add problem 1317: Convert Integer to the Sum of Two No-Zero Integers
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Dec 15, 2024
1 parent 320051e commit 2b1029c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ pub mod problem_1302_deepest_leaves_sum;
pub mod problem_1304_find_n_unique_integers_sum_up_to_zero;
pub mod problem_1310_xor_queries_of_a_subarray;
pub mod problem_1315_sum_of_nodes_with_even_valued_grandparent;
pub mod problem_1317_convert_integer_to_the_sum_of_two_no_zero_integers;
pub mod problem_1324_print_words_vertically;
pub mod problem_1325_delete_leaves_with_a_given_value;
pub mod problem_1333_filter_restaurants_by_vegan_friendly_price_and_distance;
Expand Down
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>();
}
}
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);
}
}
}

0 comments on commit 2b1029c

Please sign in to comment.