-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2177: Find Three Consecutive Integers That Sum to a Given…
… Number
- Loading branch information
Showing
3 changed files
with
52 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
33 changes: 33 additions & 0 deletions
33
src/problem_2177_find_three_consecutive_integers_that_sum_to_a_given_number/divide_by_3.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,33 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn sum_of_three(num: i64) -> Vec<i64> { | ||
let num = num as u64; | ||
|
||
if num % 3 == 0 { | ||
let middle = (num / 3) as i64; | ||
|
||
vec![middle - 1, middle, middle + 1] | ||
} else { | ||
Vec::new() | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn sum_of_three(num: i64) -> Vec<i64> { | ||
Self::sum_of_three(num) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_2177_find_three_consecutive_integers_that_sum_to_a_given_number/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,18 @@ | ||
pub mod divide_by_3; | ||
|
||
pub trait Solution { | ||
fn sum_of_three(num: i64) -> Vec<i64>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(33, &[10_i64, 11, 12] as &[_]), (4, &[])]; | ||
|
||
for (num, expected) in test_cases { | ||
assert_eq!(S::sum_of_three(num), expected); | ||
} | ||
} | ||
} |