Skip to content
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

Closed

Conversation

sozelfist
Copy link
Contributor

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

@codecov-commenter
Copy link

codecov-commenter commented Oct 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.44%. Comparing base (857c73a) to head (928c630).

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.
📢 Have feedback on the report? Share it here.

sozelfist and others added 2 commits October 17, 2024 20:39
the `break` statement executed when the search reaches the leftmost
column without finding the target element
src/searching/saddleback_search.rs Outdated Show resolved Hide resolved
@sozelfist sozelfist requested a review from vil02 October 19, 2024 03:27
src/searching/saddleback_search.rs Outdated Show resolved Hide resolved
src/searching/saddleback_search.rs Show resolved Hide resolved
- Handle non-rectangular matrix
- Handle cases of empty and full of empty rows matrix
@sozelfist
Copy link
Contributor Author

The added edge cases are just awesome.

@sozelfist sozelfist requested a review from vil02 October 20, 2024 02:55
@sozelfist
Copy link
Contributor Author

Can you have a look, @vil02?

src/searching/saddleback_search.rs Outdated Show resolved Hide resolved
src/searching/saddleback_search.rs Outdated Show resolved Hide resolved
src/searching/saddleback_search.rs Outdated Show resolved Hide resolved
@sozelfist sozelfist requested a review from vil02 October 24, 2024 02:25
/// # Returns
///
/// Returns `true` if the matrix is sorted both row-wise and column-wise. Otherwise, returns `false`.
fn is_sorted(matrix: &[Vec<isize>]) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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?

Comment on lines +20 to +21
let rows = matrix.len();
let cols = matrix[0].len();
Copy link
Member

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.

Copy link
Contributor Author

@sozelfist sozelfist Oct 25, 2024

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

Copy link
Member

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.

@sozelfist sozelfist requested a review from vil02 October 25, 2024 13:48
@sozelfist sozelfist marked this pull request as draft November 3, 2024 12:08
@sozelfist sozelfist closed this Nov 4, 2024
@sozelfist sozelfist deleted the ref/search/saddleback_search branch November 4, 2024 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants