-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Improve: Handle empty sequence case in linear_search #14627
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
Closed
snehapriy958
wants to merge
7
commits into
TheAlgorithms:master
from
snehapriy958:improve-linear-search-edge-case
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c57978
Fix: improve docstrings and use ValueError in rec_linear_search
snehapriy958 234f50b
Improve: handle empty sequence case in linear_search
snehapriy958 2ffa363
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 28fff0f
Fix: Improve: handle empty sequence case in linear_search
snehapriy958 a3425cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 96b4f34
Merge branch 'master' into improve-linear-search-edge-case
snehapriy958 4f53f94
Merge branch 'master' into improve-linear-search-edge-case
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,21 +12,19 @@ | |
| def linear_search(sequence: list, target: int) -> int: | ||
| """A pure Python implementation of a linear search algorithm | ||
|
|
||
| :param sequence: a collection with comparable items (sorting is not required for | ||
| linear search) | ||
| :param target: item value to search | ||
| :return: index of found item or -1 if item is not found | ||
| :param sequence: a collection with comparable items (No sorting required) | ||
| :param target: Value to search for | ||
| :return: index of target if found, else -1 | ||
|
|
||
| Examples: | ||
| >>> linear_search([0, 5, 7, 10, 15], 0) | ||
| 0 | ||
| >>> linear_search([0, 5, 7, 10, 15], 15) | ||
| 4 | ||
| >>> linear_search([0, 5, 7, 10, 15], 5) | ||
| 1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove tests?!? |
||
| >>> linear_search([0, 5, 7, 10, 15], 6) | ||
| -1 | ||
| """ | ||
| if not sequence: | ||
| return -1 | ||
|
|
||
| for index, item in enumerate(sequence): | ||
| if item == target: | ||
| return index | ||
|
|
@@ -35,14 +33,13 @@ def linear_search(sequence: list, target: int) -> int: | |
|
|
||
| def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: | ||
| """ | ||
| A pure Python implementation of a recursive linear search algorithm | ||
| Recursive linear search algorithm | ||
|
|
||
| :param sequence: a collection with comparable items (as sorted items not required | ||
| in Linear Search) | ||
| :param low: Lower bound of the array | ||
| :param high: Higher bound of the array | ||
| :param target: The element to be found | ||
| :return: Index of the key or -1 if key not found | ||
| :param sequence: Collection of comparable items (no sorting required) | ||
| :param low: Lower index bound | ||
| :param high: Higher index bound | ||
| :param target: Value to search for | ||
| :return: Index of the target if found, else -1 | ||
|
|
||
| Examples: | ||
| >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) | ||
|
|
@@ -55,7 +52,7 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: | |
| -1 | ||
| """ | ||
| if not (0 <= high < len(sequence) and 0 <= low < len(sequence)): | ||
| raise Exception("Invalid upper or lower bound!") | ||
| raise ValueError("Invalid upper or lower bound!") | ||
| if high < low: | ||
| return -1 | ||
| if sequence[low] == target: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please add a test that throws an exception
if sequence != sorted(sequence)