Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. class  FKbinarySearch():
  2.  
  3.     def __init__(self, itemList, item, listSorted = 0):
  4.         if not listSorted:
  5.             itemList.sort()
  6.         self.lst = itemList
  7.         first = 0
  8.         last = len(itemList) - 1
  9.         found = False
  10.         while (first <= last and not found):
  11.             mid = (first + last) // 2
  12.             if itemList[mid] == item:
  13.                 found = True
  14.             else:
  15.                 if item < itemList[mid]:
  16.                     last = mid - 1
  17.                 else:
  18.                     first = mid + 1
  19.         self.midNumber = itemList[mid]
  20.         self.midIdx = mid
  21.         self.foundBool = found
  22.  
  23.     def inList(self):
  24.         return self.foundBool
  25.  
  26.     def midNum(self):
  27.         return self.midNumber
  28.  
  29.     def midIndex(self):
  30.         return self.midIdx
  31.  
  32.     def listHigher(self, removeMidValue = False):
  33.         listi = self.lst[self.midIdx:]
  34.         if self._binary_search(listi,self.midNumber) and removeMidValue:
  35.             listi.remove(self.midNumber)
  36.         return listi
  37.  
  38.     def listLower(self, removeMidValue = False):
  39.         listi = self.lst[:self.midIdx]
  40.         if self._binary_search(listi,self.midNumber) and removeMidValue:
  41.             listi.remove(self.midNumber)
  42.         return listi
  43.  
  44.     def _binary_search(self,listi,tala):
  45.         first = 0
  46.         last = len(listi) - 1
  47.         found = False
  48.         while (first <= last and not found):
  49.             mid = (first + last) // 2
  50.             if listi[mid] == tala:
  51.                 found = True
  52.             else:
  53.                 if tala < listi[mid]:
  54.                     last = mid - 1
  55.                 else:
  56.                     first = mid + 1
  57.         return found
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement