Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FKbinarySearch():
- def __init__(self, itemList, item, listSorted = 0):
- if not listSorted:
- itemList.sort()
- self.lst = itemList
- first = 0
- last = len(itemList) - 1
- found = False
- while (first <= last and not found):
- mid = (first + last) // 2
- if itemList[mid] == item:
- found = True
- else:
- if item < itemList[mid]:
- last = mid - 1
- else:
- first = mid + 1
- self.midNumber = itemList[mid]
- self.midIdx = mid
- self.foundBool = found
- def inList(self):
- return self.foundBool
- def midNum(self):
- return self.midNumber
- def midIndex(self):
- return self.midIdx
- def listHigher(self, removeMidValue = False):
- listi = self.lst[self.midIdx:]
- if self._binary_search(listi,self.midNumber) and removeMidValue:
- listi.remove(self.midNumber)
- return listi
- def listLower(self, removeMidValue = False):
- listi = self.lst[:self.midIdx]
- if self._binary_search(listi,self.midNumber) and removeMidValue:
- listi.remove(self.midNumber)
- return listi
- def _binary_search(self,listi,tala):
- first = 0
- last = len(listi) - 1
- found = False
- while (first <= last and not found):
- mid = (first + last) // 2
- if listi[mid] == tala:
- found = True
- else:
- if tala < listi[mid]:
- last = mid - 1
- else:
- first = mid + 1
- return found
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement