MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / search

Function search

project_euler/problem_049/sol1.py:71–92  ·  view source on GitHub ↗

function to search a number in a list using Binary Search. >>> search(3, [1, 2, 3]) True >>> search(4, [1, 2, 3]) False >>> search(101, list(range(-100, 100))) False

(target: int, prime_list: list)

Source from the content-addressed store, hash-verified

69
70
71def search(target: int, prime_list: list) -> bool:
72 """
73 function to search a number in a list using Binary Search.
74 >>> search(3, [1, 2, 3])
75 True
76 >>> search(4, [1, 2, 3])
77 False
78 >>> search(101, list(range(-100, 100)))
79 False
80 """
81
82 left, right = 0, len(prime_list) - 1
83 while left <= right:
84 middle = (left + right) // 2
85 if prime_list[middle] == target:
86 return True
87 elif prime_list[middle] < target:
88 left = middle + 1
89 else:
90 right = middle - 1
91
92 return False
93
94
95def solution():

Callers 1

solutionFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected