FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Basic-Python-Programs/binarySearch.py at main · gitishman/Basic-Python-Programs · GitHub
gitishman
/
Basic-Python-Programs
Public
forked from
souravjain540/Basic-Python-Programs
Notifications
You must be signed in to change notification settings
Fork
0
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
Basic-Python-Programs
/
binarySearch.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (37 loc) · 1.09 KB
Breadcrumbs
Basic-Python-Programs
/
binarySearch.py
Copy path
File metadata and controls
43 lines (37 loc) · 1.09 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
def
binarySearch
(
arr
,
target
):
start
=
0
end
=
len
(
arr
)
-
1
while
(
start
<=
end
):
mid
=
start
+
(
end
-
start
)
//
2
if
(
target
<
arr
[
mid
]):
end
=
mid
-
1
elif
(
target
>
arr
[
mid
]):
start
=
mid
+
1
else
:
return
mid
return
-
1
def
userInput
():
arr
=
[]
n
=
int
(
input
(
"Enter number of elements: "
))
print
(
"Enter the elements"
)
for
i
in
range
(
0
,
n
):
element
=
int
(
input
())
arr
.
append
(
element
)
print
(
arr
)
target
=
int
(
input
(
"Enter the target element: "
))
result
=
binarySearch
(
arr
,
target
)
if
(
result
==
-
1
):
print
(
"Element not found"
)
else
:
print
(
"The element was found at index "
,
result
)
# Tests ('pip install pytest'; run with 'pytest binarySearch.py')
def
test_find_element_in_list
():
arr
=
[
11
,
12
,
22
,
25
,
34
,
64
,
90
,
91
]
result
=
binarySearch
(
arr
,
25
)
assert
result
==
3
def
test_elem_missing
():
arr
=
[
11
,
12
,
22
,
25
,
34
,
64
,
90
,
91
]
result
=
binarySearch
(
arr
,
16
)
assert
result
==
-
1
if
__name__
==
"__main__"
:
userInput
()
Back
|
FazBrowse Home
|
New Git URL