-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Jump search #2415
Conversation
import math | ||
|
||
|
||
def jump_search(arr, x): | ||
def jump_search(arr: list, x: int) -> int: |
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.
def jump_search(arr: list, x: int) -> int: | |
def jump_search(arr: List[int], x: int) -> int: |
First import List from typing
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.
This limits us to only the int data type. Without this specificity, would the algorithm work with strings, tuples, floats?
searches/jump_search.py
Outdated
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)}") |
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.
-1 is a valid position in python, check if the number is found before printing it
Hey @briseballoches, TravisCI finished with status TravisBuddy Request Identifier: 1fc95270-f450-11ea-a882-ef0b60d9fbb3 |
Modified. Thanks. |
>>> jump_search([-5, -2, -1], -1) | ||
2 | ||
>>> jump_search([0, 5, 10, 20], 8) | ||
-1 |
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 tend to like to raise an exception like str.index() vs str.find() but that is a personal choice.
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.
LGTM
* jump_search: doctest, docstring, type hint, inputs * jumpsearch.py: case number not found * trailing whitespace jump search
Describe your change:
Added type hinting, docstring, doctest for jump search algorithm.
Checklist:
Fixes: #{$ISSUE_NO}
.