Skip to content

Commit

Permalink
add solution of binary_search
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaaa committed Sep 3, 2024
1 parent b531efc commit 01c4a46
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
nursery = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
needless_pass_by_value = "allow"
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
must_use_candidate = "allow"

[workspace]
members = [
"crates/binary_search",
"crates/contains_duplicate",
"crates/is_palindrome",
"crates/two_sum",
Expand Down
9 changes: 9 additions & 0 deletions crates/binary_search/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "binary_search"
version = "0.1.0"
edition = "2021"

[dependencies]

[lints]
workspace = true
47 changes: 47 additions & 0 deletions crates/binary_search/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::cmp::Ordering;

pub fn binary_search(nums: Vec<i32>, target: i32) -> i32 {
let mut left_pointer = 0;
let mut right_pointer = nums.len();

while left_pointer < right_pointer {
let middle = left_pointer + (right_pointer - left_pointer) / 2;

match target.cmp(&nums[middle]) {
Ordering::Equal => return middle as i32,
Ordering::Less => right_pointer = middle,
Ordering::Greater => left_pointer = middle + 1,
}
}

-1
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn case_target_in_middle() {
let result = binary_search(vec![1, 3, 5, 7, 9], 5);
assert_eq!(result, 2);
}

#[test]
fn case_target_in_start() {
let result = binary_search(vec![1, 3, 5, 7, 9, 11], 11);
assert_eq!(result, 5);
}

#[test]
fn case_target_in_end() {
let result = binary_search(vec![1, 3, 5, 7, 9], 9);
assert_eq!(result, 4);
}

#[test]
fn case_not_found() {
let result = binary_search(vec![1, 3, 5, 7, 9, 11], 0);
assert_eq!(result, -1);
}
}
1 change: 0 additions & 1 deletion crates/contains_duplicate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashSet;

#[must_use]
pub fn contains_duplicate(numbers: Vec<i32>) -> bool {
let mut set = HashSet::new();

Expand Down
1 change: 0 additions & 1 deletion crates/is_palindrome/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[must_use]
pub fn is_palindrome(s: String) -> bool {
if s.trim().is_empty() {
return true;
Expand Down
2 changes: 0 additions & 2 deletions crates/two_sum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::collections::HashMap;

#[must_use]
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map = HashMap::new();

for (i, number) in nums.into_iter().enumerate() {
let diff = target - number;

if let Some(&j) = map.get(&diff) {
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
return vec![i as i32, j as i32];
}

Expand Down
1 change: 0 additions & 1 deletion crates/valid_anagram/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

#[must_use]
pub fn is_anagram(s: String, t: String) -> bool {
if s.len() != t.len() {
return false;
Expand Down

0 comments on commit 01c4a46

Please sign in to comment.