Skip to content

Update linear_search.py #2422

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

Merged
merged 5 commits into from
Sep 14, 2020
Merged

Conversation

Ashley-J-George
Copy link
Contributor

@Ashley-J-George Ashley-J-George commented Sep 13, 2020

I've added a Python implementation of recursive search algorithm

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Python implementation of recursive linear search algorithm
@spamegg1
Copy link
Contributor

spamegg1 commented Sep 13, 2020

@Ashley-J-George Hi there, Your first PR ever huh? Good luck!

It looks like you did not provide any doctests for rec_linear_search.
rec_linear_search still has the same doctests as linear_search.
Can you provide some different doctests that use rec_linear_search instead?

There is an issue when, for example, low or high are greater than len(sequence). It would give a list index out of bounds error. Maybe handle it with an exception?

Also, type hints for both linear_search and rec_linear_search would be appreciated. For example:

def rec_linear_search(sequence: list, low: int, high: int, target: float) -> int:

Added different doctests
Added the parameter hints
Handled the exception
@TravisBuddy
Copy link

Hey @Ashley-J-George,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 340dc310-f65d-11ea-afed-a1bc0e34d213

@Ashley-J-George
Copy link
Contributor Author

Ashley-J-George commented Sep 14, 2020

Hey @Ashley-J-George,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

@cclauss @spamegg1 How should I rectify this?

My actual code is

`
'''
This is pure Python implementation of recursive linear search algorithm
'''

def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
'''
Pure implementation of recursive linear search algorithm in Python

:param sequence: An array of items
: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

Examples:
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)
The element 0 is present at index 0
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)
The element 700 is present at index 4
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)
The element 5 is present at index 1
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)
The element -6 is not present in the array
'''
if high < low:
    return -1
try:
    if sequence[low] == target:
        return low
    if sequence[high] == target:
        return high
    return rec_linear_search(sequence, low + 1, high - 1, target)
except IndexError:
    print('Invalid upper or lower bound!')

def main():
arr = list(map(int, input('Enter space separated values: ').split())) # Maps each input into an integer
key = int(input('Enter the value you want to search: '))
index = rec_linear_search(arr, 0, len(arr) - 1, key)

if index:
    print("The element", key, "is present at index", index)
else:
    print("The element", key, "is not present in the array")

if name == 'main':
print(rec_linear_search([0, 30, 500, 100, 700], 0, 8, 700))

`

@Ashley-J-George
Copy link
Contributor Author

Ashley-J-George commented Sep 14, 2020 via email

added parameter hints to linear_search
@TravisBuddy
Copy link

Hey @Ashley-J-George,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 156ed5e0-f660-11ea-afed-a1bc0e34d213

@spamegg1
Copy link
Contributor

spamegg1 commented Sep 14, 2020

@Ashley-J-George Build log says

3 W293 blank line contains whitespace

Very easy to fix! Check out CONTRIBUTING.md about running black on your code.
You are almost there my friend! Good luck 👍

Ashley-J-George and others added 2 commits September 14, 2020 15:31
Both the functions return the index if the target is found and -1 if it is not found
The rec_linear_search raises an exception if there is an indexing problem
Made changes in the doc comments
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice work!!

@cclauss cclauss merged commit 799fde4 into TheAlgorithms:master Sep 14, 2020
@Ashley-J-George Ashley-J-George deleted the patch-1 branch September 14, 2020 10:56
@Ashley-J-George
Copy link
Contributor Author

Thank You!

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Update linear_search.py

Python implementation of recursive linear search algorithm

* Update linear_search.py

Added different doctests
Added the parameter hints
Handled the exception

* Update linear_search.py

added parameter hints to linear_search

* Update linear_search.py

Both the functions return the index if the target is found and -1 if it is not found
The rec_linear_search raises an exception if there is an indexing problem
Made changes in the doc comments

* Update linear_search.py

Co-authored-by: Christian Clauss <cclauss@me.com>
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.

6 participants