-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup Saddleback Search #824
Cleanup Saddleback Search #824
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #824 +/- ##
=======================================
Coverage 95.43% 95.44%
=======================================
Files 315 315
Lines 22809 22813 +4
=======================================
+ Hits 21768 21773 +5
+ Misses 1041 1040 -1 ☔ View full report in Codecov by Sentry. |
the `break` statement executed when the search reaches the leftmost column without finding the target element
- Handle non-rectangular matrix - Handle cases of empty and full of empty rows matrix
The added edge cases are just awesome. |
Can you have a look, @vil02? |
/// # Returns | ||
/// | ||
/// Returns `true` if the matrix is sorted both row-wise and column-wise. Otherwise, returns `false`. | ||
fn is_sorted(matrix: &[Vec<isize>]) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn is_sorted(matrix: &[Vec<isize>]) -> bool { | |
pub fn is_sorted(matrix: &[Vec<isize>]) -> bool { |
Imagine that you have some matrix. You know that you will query the position of many elements and you want to check if the matrix is soared only once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand this point. By the way, do you know how can we express the tests here? Suppose, we have two functions, one for validate matrix, one for saddleback search like this:
fn validate_matrix(matrix: &[Vec<isize>]) -> Result<(), MatrixError> {
// Check if all rows have the same length (rectangular matrix).
if matrix.iter().any(|row| row.len() != matrix[0].len()) {
return Err(MatrixError::NonRectangularInput);
}
// Check if matrix is sorted.
if !is_sorted(matrix) {
return Err(MatrixError::NotSorted);
}
Ok(())
}
and
fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> Option<(usize, usize)> {
if matrix.is_empty() || matrix.iter().all(|row| row.is_empty()) {
return None;
}
let mut left_index = 0;
let mut right_index = matrix[0].len() - 1;
while left_index < matrix.len() {
match element.cmp(&matrix[left_index][right_index]) {
Ordering::Equal => return Some((left_index, right_index)),
Ordering::Greater => {
left_index += 1;
}
Ordering::Less => {
if right_index == 0 {
break;
} else {
right_index -= 1;
}
}
}
}
None
}
Can you propose how we can write tests, @vil02?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would create a function checking if the input is rectangular and simply use it in both is_sorted
and saddleback_search
.
Regarding testing strategy: it should be enough to check saddleback_search
(as it is now) and is_sorted
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you suggest the detailed changes?
let rows = matrix.len(); | ||
let cols = matrix[0].len(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to check if the input is rectangular.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can move the logic that checks the matrix form and is_sorted
into a single function, for example, validate_matrix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above: I would introduce a new function checking if the input is rectangular and use it in both public functions.
Checklist:
cargo clippy --all -- -D warnings
just before my last commit and fixed any issue that was found.cargo fmt
just before my last commit.cargo test
just before my last commit and all tests passed.mod.rs
file within its own folder, and in any parent folder(s).DIRECTORY.md
with the correct link.COUNTRIBUTING.md
and my code follows its guidelines.