在二进制搜索中,我遇到了这个问题,无法将字符'/‘识别为字符串或int。因此,比较几乎是不可能的。我很好奇这个问题的想法是什么。如何对python中的url执行二进制搜索?
url1 = "https://diversity.google"
url2 = "https://www.aboutamazon.com/workplace/diversity-inclusion"
url3 = "https://www.indeed.com/q-Diversity-jobs.html?vjk=ba073b4704d48c67"
url4 = "https://careers.linkedin.com/diversity-and-inclusion"
url5 = "https://github.com/about/diversity"
url6 = "https://www.apple.com/diversity/"
url7 ="https://www.samsung.com/us/about-us/diversity-and-inclusion/"
url8 = "https://diversity.fb.com"
url9 ="instagram:none"
url10 = "https://careers.twitter.com/en/diversity.html"
data = [url1,url2,url3,url4,url5,url6,url7,url8,url9,url10]
print(data)
def binary_search(i, data, low, high, x):
mid = (high+low) / 2
if data[i][mid] > x:
return binary_search(data, low, mid-1, x)
elif data[i] [mid] < x:
return binary_search(data, mid+1, high, x)
else:
return mid
error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-fb856fb84489> in <module>()
9 high = [i][-1]
10
---> 11 foundOne = binary_search(i,data,low,high,x)
12
13 if (foundOne == x):
<ipython-input-29-739df621f0c5> in binary_search(i, data, low, high, x)
20 print(data)
21 def binary_search(i, data, low, high, x):
---> 22 mid = (high+low) / 2
23
24 if data[i][mid] > x:
TypeError: unsupported operand type(s) for /: 'str' and 'int'(update)******
这就是我称之为it.Apologies的地方,因为它不包括它。
x = '/'
counterData = []
counter = 0
for i in data:
low = [i][0]
high = [i][-1]
foundOne = binary_search(i,data,low,high,x)
if (foundOne == x):
counter += 1
else:
counter += 0
counterData.append(counter)发布于 2021-09-23 19:06:40
这是一个有效的版本。您可以看到它依次搜索每个条目,然后打印索引。
当我进行搜索时,我通常会把被搜索的东西称为“干草堆”,而我正在寻找的东西叫做“针”。
另外,您的函数不处理在干草堆中找不到针头的情况。
url1 = "https://diversity.google"
url2 = "https://www.aboutamazon.com/workplace/diversity-inclusion"
url3 = "https://www.indeed.com/q-Diversity-jobs.html?vjk=ba073b4704d48c67"
url4 = "https://careers.linkedin.com/diversity-and-inclusion"
url5 = "https://github.com/about/diversity"
url6 = "https://www.apple.com/diversity/"
url7 = "https://www.samsung.com/us/about-us/diversity-and-inclusion/"
url8 = "https://diversity.fb.com"
url9 = "instagram:none"
url10 = "https://careers.twitter.com/en/diversity.html"
data = [url1,url2,url3,url4,url5,url6,url7,url8,url9,url10]
data.sort()
def binary_search(haystack, low, high, needle):
if high < low:
return -1
mid = (high+low) // 2
if haystack[mid] > needle:
return binary_search(haystack, low, mid-1, needle)
elif haystack[mid] < needle:
return binary_search(haystack, mid+1, high, needle)
else:
return mid
for i in data:
foundOne = binary_search(data, 0, len(data)-1, i)
print(i, foundOne)输出:
https://careers.linkedin.com/diversity-and-inclusion 0
https://careers.twitter.com/en/diversity.html 1
https://diversity.fb.com 2
https://diversity.google 3
https://github.com/about/diversity 4
https://www.aboutamazon.com/workplace/diversity-inclusion 5
https://www.apple.com/diversity/ 6
https://www.indeed.com/q-Diversity-jobs.html?vjk=ba073b4704d48c67 7
https://www.samsung.com/us/about-us/diversity-and-inclusion/ 8
instagram:none 9https://stackoverflow.com/questions/69305008
复制相似问题