-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added maximum positive or negative integer count solution.
- Loading branch information
Showing
3 changed files
with
27 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
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,25 @@ | ||
use std::cmp::Ordering; | ||
|
||
pub fn maximum_count(numbers: Vec<i32>) -> i32 { | ||
let counts = numbers.into_iter().fold((0, 0), |mut acc, curr| { | ||
match curr.cmp(&0) { | ||
Ordering::Greater => acc.1 += 1, | ||
Ordering::Less => acc.0 += 1, | ||
Ordering::Equal => {} | ||
} | ||
acc | ||
}); | ||
counts.0.max(counts.1) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::maximum_count; | ||
|
||
#[test] | ||
fn maximum_positive_negative_count_is_provided() { | ||
let numbers = vec![-1, -2, 0, 2, 5, -3]; | ||
let actual = maximum_count(numbers); | ||
assert_eq!(3, actual) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -51,3 +51,4 @@ mod freq_sort; | |
mod sort_jumbled_numbers; | ||
mod max_binary_number; | ||
mod custom_sort; | ||
mod max_positive_negative; |