每周算法编程联系1
Posted linlianhuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每周算法编程联系1相关的知识,希望对你有一定的参考价值。
在一个有序的列表中,查找指定数字的索引位置:
mylist = [1,2,3,4,5,6,7,8,9,10,11]
# list_len = mylist.__len__()
# print list_len
def find_number_index(list,target,start):
length = list.__len__()
print "--- "+str(length)
mid = int(length/2)
if mid <=1 :
if list[mid] == target:
return "find index: " + str(mid+ start)
elif list[mid-1] == target:
return "find index: " + str(mid -1+ start)
elif length == 3 and list[mid+1] == target :
return "find index: " + str(mid + 1 + start)
else:
return "could not mad a number"
elif list[mid] == target:
print " mylist[mid] == target "
return "find index: " + str(mid + start)
elif list[mid] > target:
print str(list[mid]) + " mylist[mid] > target "
list_min = list[:mid]
print list_min
return find_number_index(list_min,target,start)
else :
print " mylist[mid] < target "
list_max = list[mid:]
print list_max
return find_number_index(list_max,target, start+mid)
if __name__=="__main__":
print find_number_index(mylist,3, 0)
以上是关于每周算法编程联系1的主要内容,如果未能解决你的问题,请参考以下文章
Quarkchain的Qi Zhou给大家present他新发明的PoW PoS混合算法以及基于不规则编程的Hash算法