-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
23 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,21 @@ | ||
pub fn search(numbers: Vec<i32>, target: i32) -> i32 { | ||
match numbers.binary_search(&target) { | ||
Ok(i) => i as i32, | ||
Err(_) => -1, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn finds_element_index_when_it_exists() { | ||
assert_eq!(4, search(vec![-1, 0, 3, 5, 9, 12], 9)) | ||
} | ||
|
||
#[test] | ||
fn negative_one_when_element_does_not_exist() { | ||
assert_eq!(-1, search(vec![-1, 0, 3, 5, 9, 12], 20)) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -151,3 +151,4 @@ mod xor_subarray; | |
mod zero_or_one; | ||
mod zigzag_conversion; | ||
mod valid_sudoku; | ||
mod binary_search; |