From 2750f60f27fc254fbebc293ae9ff1e3258596e49 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 10 Sep 2020 18:47:10 +0200 Subject: [PATCH 1/3] jump_search: doctest, docstring, type hint, inputs --- searches/jump_search.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/searches/jump_search.py b/searches/jump_search.py index 5ba80e9d35be..217b76890c43 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -1,7 +1,28 @@ +""" +Pure Python implementation of the jump search algorithm. +This algorithm iterates through a sorted collection with a step of n^(1/2), +until the element compared is bigger than the one searched. +It will then perform a linear search until it matches the wanted number. +If not found, it returns -1. +""" + import math -def jump_search(arr, x): +def jump_search(arr: list, x: int) -> int: + """ + Pure Python implementation of the jump search algorithm. + Examples: + >>> jump_search([0, 1, 2, 3, 4, 5], 3) + 3 + >>> jump_search([-5, -2, -1], -1) + 2 + >>> jump_search([0, 5, 10, 20], 8) + -1 + >>> jump_search([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610], 55) + 10 + """ + n = len(arr) step = int(math.floor(math.sqrt(n))) prev = 0 @@ -21,6 +42,7 @@ def jump_search(arr, x): if __name__ == "__main__": - arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] - 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)}") From 0ab37724a7b2266d702ed234b70e132a010f5dba Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 11 Sep 2020 18:55:05 +0200 Subject: [PATCH 2/3] jumpsearch.py: case number not found --- searches/jump_search.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/searches/jump_search.py b/searches/jump_search.py index 217b76890c43..e009cc97a2e9 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -45,4 +45,8 @@ def jump_search(arr: list, x: int) -> int: 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)}") + res = jump_search(arr, x) + if res == -1: + print("Number not found!") + else: + print(f"Number {x} is at index {res}") From 25259cccf8250b9ef82349f2799743402742f3e0 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 11 Sep 2020 19:02:01 +0200 Subject: [PATCH 3/3] trailing whitespace jump search --- searches/jump_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/jump_search.py b/searches/jump_search.py index e009cc97a2e9..31a9656c55fe 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -46,7 +46,7 @@ def jump_search(arr: list, x: int) -> int: arr = [int(item) for item in user_input.split(",")] x = int(input("Enter the number to be searched:\n")) res = jump_search(arr, x) - if res == -1: + if res == -1: print("Number not found!") else: print(f"Number {x} is at index {res}")