{"id":1075,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u641c\u7d22\u7b97\u6cd5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1075.html","title":{"rendered":"Python \u641c\u7d22\u7b97\u6cd5"},"content":{"rendered":"
\n
Python \u641c\u7d22\u7b97\u6cd5\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n <\/body>\u7ebf\u6027\u641c\u7d22<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>linear_search(values, search_for):
search_at = 0
search_res = False<\/span>
# Match the value with <\/span>each data element
<\/span> while <\/span>search_at < len(values) and <\/span>search_res is <\/span>False<\/span>:
if <\/span>values[search_at] == search_for:
search_res = True<\/span>
else:<\/span>
search_at = search_at + 1
return <\/span>search_res
l = [64, 34, 25, 12, 22, 11, 90]
print(linear_search(l, 12))
print(linear_search(l, 91))
<\/span><\/code><\/pre>\n<\/p><\/div>\n True
False<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u63d2\u503c\u641c\u7d22<\/h2>\n
\n
\u6709\u4e00\u4e2a\u7279\u5b9a\u7684\u516c\u5f0f\u53ef\u4ee5\u8ba1\u7b97\u4e2d\u95f4\u4f4d\u7f6e\uff0c\u8be5\u516c\u5f0f\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u663e\u793a\u3002\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>intpolsearch(values,x ):
idx0 = 0
idxn = (len(values) - 1)
while <\/span>idx0 <= idxn and <\/span>x >= values[idx0] and <\/span>x <= values[idxn]:
# Find the mid point
<\/span> mid = idx0 +\\
int(((float(idxn - idx0)\/( values[idxn] - values[idx0]))
* ( x - values[idx0])))
# Compare the value at mid point with <\/span>search value
<\/span> if <\/span>values[mid] == x:
return <\/span>\"Found \"<\/span>+str(x)+\" at index \"<\/span>+str(mid)
if <\/span>values[mid] < x:
idx0 = mid + 1
return <\/span>\"Searched element not <\/span>in <\/span>the list\"<\/span>
l = [2, 6, 11, 19, 27, 31, 45, 121]
print(intpolsearch(l, 2))
<\/span><\/code><\/pre>\n<\/p><\/div>\nFound 2 at index 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u641c\u7d22\u7b97\u6cd5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1075"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1075\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}