-
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 odd binary number solution.
- Loading branch information
Showing
3 changed files
with
19 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,17 @@ | ||
pub fn maximum_odd_binary_number(s: String) -> String { | ||
let one_count = s.chars().filter(|c| *c == '1').count(); | ||
let ones: String = "1".repeat(one_count - 1); | ||
let zeroes: String = "0".repeat(s.len() - one_count); | ||
format!("{ones}{zeroes}1") | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::maximum_odd_binary_number; | ||
|
||
#[test] | ||
fn test_maximum_odd_binary_is_produced() { | ||
let actual = maximum_odd_binary_number("101101".to_string()); | ||
assert_eq!("111001", 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 |
---|---|---|
|
@@ -49,3 +49,4 @@ mod sort_people; | |
mod max_repeating_substr; | ||
mod freq_sort; | ||
mod sort_jumbled_numbers; | ||
mod max_binary_number; |