Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import bisect
  2.  
  3. l = [(5, 100), (15, 200), (20, 300)]
  4.  
  5. # --------------------
  6.  
  7. x = bisect.bisect(l, (10, None))
  8. print x, "Falsch, ich will 0"
  9.  
  10. # --------------------
  11.  
  12. x = bisect.bisect(l, (10, None))-1
  13. print x, "Richtig, ich will 0"
  14.  
  15. # --------------------
  16.  
  17. x = bisect.bisect(l, (10, None))
  18. if x > 0:
  19.     x -= 1
  20. print x, "Richtig, inkl. special case handling für 0. index"
  21.  
  22. # --------------------
  23.  
  24. x = bisect.bisect(l, (15, None))
  25. if x > 0:
  26.     x -= 1
  27. print x, "falsch, ich will 1. Problem ist, dass (15, None) < (15, 200) ist."
  28.  
  29. # --------------------
  30.  
  31. t = 15
  32. x = bisect.bisect(l, (t, None))
  33. if x < len(l) and l[x][0] == t:
  34.     pass
  35. elif x > 0:
  36.     x -= 1
  37. print x, "richtig, mit Test für genauer Zeittreffer (exception handling letzer Wert im array)"
  38.  
  39. # --------------------
  40.  
  41. def getValue(l, t):
  42.     x = bisect.bisect(l, (t, None))
  43.     if x < len(l) and l[x][0] == t:
  44.          pass
  45.     elif x > 0:
  46.         x -= 1
  47.        
  48.     return x
  49.  
  50. # --------------------
  51.  
  52. print "----- tests -----"
  53. for i in range(25):
  54.     print i, "=", l[getValue(l, i)][1], "(index:", getValue(l, i), ")"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement