Skip to content

Commit

Permalink
initial matrix
Browse files Browse the repository at this point in the history
initial search_in_a_sorted_matrix with tests
  • Loading branch information
alirezasariri78 committed Nov 6, 2024
1 parent abe1e2e commit 5b1f537
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod graph;
pub mod greedy;
pub mod machine_learning;
pub mod math;
pub mod matrix;
pub mod navigation;
pub mod number_theory;
pub mod searching;
Expand Down
2 changes: 2 additions & 0 deletions src/matrix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod search_in_a_sorted_matrix;
pub use search_in_a_sorted_matrix::search_in_a_sorted_matrix;
98 changes: 98 additions & 0 deletions src/matrix/search_in_a_sorted_matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@


///find row and column of given key in given matrix
/// m is row count and n is column count
#[derive(PartialEq, Eq, Debug)]
pub struct SortedMatrixResult {
pub column: i64,
pub row: i64,
}
#[derive(PartialEq, Eq, Debug)]
pub enum SortedMatrixErr {
KeyNotFound,
InvalidArgument(String),
}
pub fn search_in_a_sorted_matrix(
mat: Vec<Vec<f64>>,
m: usize,
n: usize,
key: f64,
) -> Result<SortedMatrixResult, SortedMatrixErr> {
if m < 1 {
return Err(SortedMatrixErr::InvalidArgument(String::from(
"m must be greater than or equal to 1",
)));
}

if n < 1 {
return Err(SortedMatrixErr::InvalidArgument(String::from(
"n must be greater than or equal to 1",
)));
}

if mat.is_empty() {
return Err(SortedMatrixErr::InvalidArgument(String::from(
"mat must be non-empty",
)));
}

let (mut i, mut j) = (m - 1, 0);

while j < n {
if (mat[i][j] - key).abs() < f64::EPSILON {
return Ok(SortedMatrixResult {
column: (j + 1) as i64,
row: (i + 1) as i64,
});
}
if key < mat[i][j] {
i -= 1;
} else {
j += 1;
}
}
Err(SortedMatrixErr::KeyNotFound)
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_search_in_a_sorted_matrix_err {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
let ((mat,m,n,key), expected) = $inputs;
assert_eq!(search_in_a_sorted_matrix(mat,m,n,key).unwrap_err(), expected);
}
)*
}
}
macro_rules! test_search_in_a_sorted_matrix {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
let ((mat,m,n,key), expected) = $inputs;
assert_eq!(search_in_a_sorted_matrix(mat,m,n,key).unwrap(), expected);
}
)*
}
}

test_search_in_a_sorted_matrix_err! {
key_not_found_test: ((vec![vec![1.0,1.0],vec![1.0,1.0]],2,2,4.0), SortedMatrixErr::KeyNotFound),
invalid_m_test: ((vec![vec![1.0,1.0],vec![1.0,1.0]],0,2,4.0), SortedMatrixErr::InvalidArgument(String::from("m must be greater than or equal to 1"))),
invalid_n_test: ((vec![vec![1.0,1.0],vec![1.0,1.0]],2,0,4.0), SortedMatrixErr::InvalidArgument(String::from("n must be greater than or equal to 1"))),
}

test_search_in_a_sorted_matrix! {
kgeneral_test1: ((vec![vec![2.1, 5.0, 7.0], vec![4.0, 8.0, 13.0], vec![9.0, 11.0, 15.0], vec![12.0, 17.0, 20.0]], 3, 3, 2.1), SortedMatrixResult{
row:1,
column:1
}),
kgeneral_test2: (( vec![vec![2.0, 5.0, 7.0], vec![4.0, 8.0, 13.0], vec![9.0, 11.0, 15.0], vec![12.0, 17.0, 20.0]], 3, 3, 5.0), SortedMatrixResult{
row:1,
column:2
}),
}
}

0 comments on commit 5b1f537

Please sign in to comment.