Skip to content

Jump search #2415

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 10 commits into from
Sep 12, 2020
Merged

Jump search #2415

merged 10 commits into from
Sep 12, 2020

Conversation

grochedix
Copy link
Contributor

Describe your change:

Added type hinting, docstring, doctest for jump search algorithm.

  • 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}.

import math


def jump_search(arr, x):
def jump_search(arr: list, x: int) -> int:
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
def jump_search(arr: list, x: int) -> int:
def jump_search(arr: List[int], x: int) -> int:

First import List from typing

Copy link
Member

@cclauss cclauss Sep 11, 2020

Choose a reason for hiding this comment

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

This limits us to only the int data type. Without this specificity, would the algorithm work with strings, tuples, floats?

x = 55
user_input = input("Enter numbers separated by a comma:\n").strip()
arr = [int(item) for item in user_input.split(",")]
x = int(input("Enter the number to be searched:\n"))
print(f"Number {x} is at index {jump_search(arr, x)}")
Copy link
Member

Choose a reason for hiding this comment

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

-1 is a valid position in python, check if the number is found before printing it

@TravisBuddy
Copy link

Hey @briseballoches,
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: 1fc95270-f450-11ea-a882-ef0b60d9fbb3

@grochedix
Copy link
Contributor Author

Modified. Thanks.

>>> jump_search([-5, -2, -1], -1)
2
>>> jump_search([0, 5, 10, 20], 8)
-1
Copy link
Member

Choose a reason for hiding this comment

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

I tend to like to raise an exception like str.index() vs str.find() but that is a personal choice.

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.

LGTM

@cclauss cclauss merged commit f754c0d into TheAlgorithms:master Sep 12, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* jump_search: doctest, docstring, type hint, inputs

* jumpsearch.py: case number not found

* trailing whitespace jump search
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