-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a few Codility solutions in Rust
- Loading branch information
1 parent
9620bb5
commit aa9b38a
Showing
5 changed files
with
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod binary_gap; | ||
pub mod rotate_slice; | ||
pub mod smallest_pos_int; |
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,44 @@ | ||
use std::{cmp, fmt}; | ||
|
||
use num::Integer; | ||
|
||
pub fn binary_gap<I>(num: I) -> usize | ||
where | ||
I: Integer + fmt::Binary, | ||
{ | ||
let binary = format!("{num:b}"); | ||
binary | ||
.chars() | ||
.fold((0, 0), |(current, min), ch| { | ||
if ch == '1' { | ||
(0, cmp::max(current, min)) | ||
} else { | ||
assert_eq!(ch, '0'); | ||
(current + 1, min) | ||
} | ||
}) | ||
.1 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::binary_gap; | ||
|
||
#[test] | ||
fn zero_case_binary_gap() { | ||
let gap = binary_gap(0); | ||
assert_eq!(0, gap); | ||
} | ||
|
||
#[test] | ||
fn gaps_to_end() { | ||
let gap = binary_gap(9); | ||
assert_eq!(2, gap); | ||
} | ||
|
||
#[test] | ||
fn i32_max_gap() { | ||
let gap = binary_gap(i32::MAX); | ||
assert_eq!(0, gap); | ||
} | ||
} |
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,46 @@ | ||
pub fn rotate_slice<'slice, T: Clone>( | ||
slice: &'slice [T], | ||
by: usize, | ||
) -> impl Iterator<Item = T> + 'slice { | ||
slice | ||
.iter() | ||
.cloned() | ||
.cycle() | ||
.skip(slice.len().abs_diff(by) % slice.len()) | ||
.take(slice.len()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::rotate_slice; | ||
|
||
#[test] | ||
fn simple_shift_works() { | ||
let slice = [4, 9, 11, 6]; | ||
|
||
let actual = rotate_slice(&slice, 1); | ||
let expected = [6, 4, 9, 11]; | ||
|
||
assert!(actual.eq(expected)); | ||
} | ||
|
||
#[test] | ||
fn zero_shift_works() { | ||
let slice = [6, 7, 8, 9]; | ||
|
||
let actual = rotate_slice(&slice, 0); | ||
let expected = slice; | ||
|
||
assert!(actual.eq(expected)); | ||
} | ||
|
||
#[test] | ||
fn over_shift_works() { | ||
let slice = [3, 4, 5, 6]; | ||
|
||
let actual = rotate_slice(&slice, 6); | ||
let expected = [5, 6, 3, 4]; | ||
|
||
assert!(actual.eq(expected)); | ||
} | ||
} |
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,14 @@ | ||
use std::{collections::BTreeSet, iter}; | ||
|
||
use num::{CheckedAdd, FromPrimitive, Integer}; | ||
|
||
pub fn smallest_pos_int<I>(haystack: &[I]) -> Option<I> | ||
where | ||
I: Integer + CheckedAdd + FromPrimitive + Copy, | ||
{ | ||
let hayset = BTreeSet::from_iter(haystack.iter().copied()); | ||
iter::successors(I::from_u8(1), |next| { | ||
next.checked_add(&I::from_u8(1).unwrap()) | ||
}) | ||
.find(|val| !hayset.contains(val)) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod codility; | ||
pub mod neet; | ||
pub mod search; |