FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TheAlgorithms-Python/searches/simple_binary_search.py at master · windNight/TheAlgorithms-Python · GitHub
windNight
/
TheAlgorithms-Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TheAlgorithms-Python
/
searches
/
simple_binary_search.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
59 lines (53 loc) · 1.71 KB
Breadcrumbs
TheAlgorithms-Python
/
searches
/
simple_binary_search.py
Copy path
File metadata and controls
59 lines (53 loc) · 1.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Pure Python implementation of a binary search algorithm.
For doctests run following command:
python3 -m doctest -v simple_binary_search.py
For manual testing run:
python3 simple_binary_search.py
"""
from
__future__
import
annotations
def
binary_search
(
a_list
:
list
[
int
],
item
:
int
)
->
bool
:
"""
>>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42]
>>> print(binary_search(test_list, 3))
False
>>> print(binary_search(test_list, 13))
True
>>> print(binary_search([4, 4, 5, 6, 7], 4))
True
>>> print(binary_search([4, 4, 5, 6, 7], -10))
False
>>> print(binary_search([-18, 2], -18))
True
>>> print(binary_search([5], 5))
True
>>> print(binary_search(['a', 'c', 'd'], 'c'))
True
>>> print(binary_search(['a', 'c', 'd'], 'f'))
False
>>> print(binary_search([], 1))
False
>>> print(binary_search([-.1, .1 , .8], .1))
True
>>> binary_search(range(-5000, 5000, 10), 80)
True
>>> binary_search(range(-5000, 5000, 10), 1255)
False
>>> binary_search(range(0, 10000, 5), 2)
False
"""
if
len
(
a_list
)
==
0
:
return
False
midpoint
=
len
(
a_list
)
//
2
if
a_list
[
midpoint
]
==
item
:
return
True
if
item
<
a_list
[
midpoint
]:
return
binary_search
(
a_list
[:
midpoint
],
item
)
else
:
return
binary_search
(
a_list
[
midpoint
+
1
:],
item
)
if
__name__
==
"__main__"
:
user_input
=
input
(
"Enter numbers separated by comma:
\n
"
).
strip
()
sequence
=
[
int
(
item
.
strip
())
for
item
in
user_input
.
split
(
","
)]
target
=
int
(
input
(
"Enter the number to be found in the list:
\n
"
).
strip
())
not_str
=
""
if
binary_search
(
sequence
,
target
)
else
"not "
print
(
f"
{
target
}
was
{
not_str
}
found in
{
sequence
}
"
)
Back
|
FazBrowse Home
|
New Git URL