Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. import operator
  2. from bisect import bisect_left
  3.  
  4. def index(a, x):
  5.     'Locate the leftmost value exactly equal to x'
  6.     i = bisect_left(a, x)
  7.     if i != len(a) and a[i] == x:
  8.         return i
  9.     raise ValueError
  10.  
  11. def BinSearchLeft(li, x):
  12.     i = 0
  13.     j = len(li)-1
  14.     m = int(j/2)
  15.     while li[m][0] > x and i < j:
  16.         if x > li[m][0]:
  17.             i = m+1
  18.         else:
  19.             j = m-1
  20.         m = int((i+j)/2)
  21.     if i > j:
  22.         return 'Нет такого'
  23.     else:
  24.         return m
  25.  
  26. a = [[1,2,3,4,5],
  27. [11,12,13,14,15],
  28. [21,22,23,24,25],
  29. [31,32,33,34,35],
  30. [41,42,43,44,45]]
  31.  
  32. x = 24
  33.  
  34. row = BinSearchLeft(a, x)
  35.  
  36. print(row)
  37.  
  38. col = index(a[row], x)
  39.  
  40. print(col)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement