Skip to content

Commit

Permalink
Add problem 2160: Minimum Sum of Four Digit Number After Splitting Di…
Browse files Browse the repository at this point in the history
…gits
  • Loading branch information
EFanZh committed Mar 2, 2024
1 parent ef649da commit 72266a0
Show file tree
Hide file tree
Showing 3 changed files with 62 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 @@ -1465,6 +1465,7 @@ pub mod problem_2149_rearrange_array_elements_by_sign;
pub mod problem_2150_find_all_lonely_numbers_in_the_array;
pub mod problem_2154_keep_multiplying_found_values_by_two;
pub mod problem_2155_all_divisions_with_the_highest_score_of_a_binary_array;
pub mod problem_2160_minimum_sum_of_four_digit_number_after_splitting_digits;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn minimum_sum(num: i32) -> i32 {
let mut num = num as u16;
let a = (num % 10) as u8;

num /= 10;

let b = (num % 10) as u8;

num /= 10;

let c = (num % 10) as u8;
let d = (num / 10) as u8;

let mut digits = [a, b, c, d];

digits.sort_unstable();

let [a, b, c, d] = digits;

i32::from((a + b) * 10 + c + d)
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn minimum_sum(num: i32) -> i32 {
Self::minimum_sum(num)
}
}

#[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,18 @@
pub mod mathematical;

pub trait Solution {
fn minimum_sum(num: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [(2932, 52), (4009, 13)];

for (num, expected) in test_cases {
assert_eq!(S::minimum_sum(num), expected);
}
}
}

0 comments on commit 72266a0

Please sign in to comment.