• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

/home/mark/model/software/ScrumPy/ScrumPy/Bioinf/PyoCyc/OrderedList.py

00001 
00002 def findn(list, item, lo, hi):
00003   
00004     if hi-lo == 1:
00005         if abs(list[hi] - item) < abs(list[lo] - item):
00006             return hi
00007         return lo
00008     mid = (hi+lo)/2
00009     if item < list[mid]:
00010         return findn(list,item,lo, mid)
00011     return findn(list,item,mid, hi)
00012 
00013 
00014 def FindNearest(List,Item):
00015     """ pre: len(List) >0, (List[i] - Item) is valid for i[0..len(List)-1]
00016        post: returns index of the item in list nearest to Item"""
00017 
00018 
00019     
00020     return findn(List, Item, 0, len(List)-1)
00021     
00022 
00023 def insert(list,item,left,right): # recursive search and insert
00024     if item <= list[left]:
00025         list.insert(left,item)
00026     elif item >= list[right]:
00027         list.insert(right+1, item)
00028     else:
00029         mid = (left + right) /2
00030         if item > list[mid]:
00031             insert(list,item,mid,right-1)
00032         else:
00033             insert(list,item,left+1,mid)
00034 
00035 def Insert(List,Item):
00036     """ pre: List is ordered or empty, (Item > List[i], Item < List[i]) valid for i[0..len(List)-1]
00037        post: Item inserterd in order into list """
00038         
00039 
00040 
00041 
00042     lenlist = len(List)
00043     if lenlist==0:
00044         List.insert(0,Item)
00045     else:
00046         insert(List,Item,0,lenlist-1)
00047     

Generated on Tue Sep 4 2012 15:38:01 for ScrumPy by  doxygen 1.7.1