Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson16-diy2
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
51e36358
authored
Mar 13, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
ce23a357
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
7 deletions
my_search.py
my_search.py
View file @
51e36358
from
random
import
randint
as
rdt
,
choice
as
chc
from
random
import
randint
as
rdt
,
choice
as
chc
num_list
=
[]
num_list
=
[
22
,
32
,
93
,
233
,
32
,
954
,
992
,
834
,
28
,
110
,
2
,
89
,
77
,
8
,
63
,
3
,
9954
]
step
=
rdt
(
1
,
3
)
for
i
in
range
(
1
,
101
,
step
):
num_list
.
append
(
i
)
guess_num
=
chc
(
num_list
)
guess_num
=
chc
(
num_list
)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def
binary_search
(
alist
,
num
):
def
binary_search
(
alist
,
num
):
'''
'''
b
inary search a num.
B
inary search a num.
a
list is want search list,
A
list is want search list,
num is want search number.
num is want search number.
'''
'''
...
@@ -28,6 +25,23 @@ def binary_search(alist,num):
...
@@ -28,6 +25,23 @@ def binary_search(alist,num):
high
=
mid
-
1
high
=
mid
-
1
return
None
return
None
def
bubble_sort
(
alist
,
reverse
=
False
):
'''
Bubble soft a list.
Alist is want soft list,
reverse is order or reverse order.
'''
for
i
in
range
(
0
,
len
(
alist
)
-
1
):
for
j
in
range
(
0
,
len
(
alist
)
-
1
):
if
reverse
==
False
:
if
alist
[
j
]
<
alist
[
j
+
1
]:
alist
[
j
],
alist
[
j
+
1
]
=
alist
[
j
+
1
],
alist
[
j
]
elif
reverse
==
True
:
if
alist
[
j
]
>
alist
[
j
+
1
]:
alist
[
j
],
alist
[
j
+
1
]
=
alist
[
j
+
1
],
alist
[
j
]
else
:
raise
TypeError
(
"reverse's type must is bool!"
)
num_list
=
bubble_sort
(
num_list
,
True
)
result
=
binary_search
(
num_list
,
guess_num
)
result
=
binary_search
(
num_list
,
guess_num
)
guess_num
=
str
(
guess_num
)
guess_num
=
str
(
guess_num
)
print
(
"要猜的数字:
%
s"
%
(
guess_num
))
print
(
"要猜的数字:
%
s"
%
(
guess_num
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment