FIND ROTTEN WALNUT

Completed : 05.11.2017 Created by: AYDIN ÇALIKOĞLU

Preview
FIND ROTTEN WALNUT

def compareScales(leftScaleList, rightScaleList):

  result = sum(leftScaleList) - sum(rightScaleList)    

  if result < 0:        

    return 1    

  elif result > 0:        

    return -1    

  else:        

    return 0

 

 

def WalnutsBinarySearch(walnuts,index):

  size = len(walnuts)

  half=(int)(size/2)

 

  if(size<2):

    return index

 

  if size%2==1: # Size tek ise ortaki elemana bakma

    if walnuts[half+1]>walnuts[half]:

      return half

    elif walnuts[half+1]<walnuts[half]:

      return half+1

    walnutsRight = walnuts[half+1:]

  else:

    walnutsRight = walnuts[half:]

  walnutsLeft = walnuts[0:half]

 

  durum = compareScales(walnutsLeft,walnutsRight)

  if durum == 0:

    return -1

  elif durum == 1:

    return WalnutsBinarySearch(walnutsLeft,index)

  else:

    return WalnutsBinarySearch(walnutsRight,index+half+size%2)

 

walnuts= [1,1,1,1,0.5,1,1,1,1]

print(WalnutsBinarySearch(walnuts,0))


Tüm Yorumlar - All Contents