Advertisement
Guest User

binary search

a guest
Oct 22nd, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import sys
  2.  
  3. s = raw_input('give a string: ')
  4. n = len(s)
  5. low = 0
  6. high = n-1
  7. ans = (high-low)/2
  8.  
  9. x = ()
  10. l = ''
  11.  
  12. print "Give a letter: "
  13. l = sys.stdin.read(1)
  14.  
  15. if n != 0:
  16. if l != s[0] and l != s[n-1]:
  17. if l == s[ans]:
  18. a = 0
  19. b = 1
  20. while l == s[ans-a]:
  21. x = x + (ans-a,)
  22. a += 1
  23. while l == s[ans+b]:
  24. x = x + (ans+b,)
  25. b += 1
  26. else:
  27. while l != s[ans]:
  28. if s[ans] > l:
  29. high = ans
  30. else:
  31. low = ans
  32. ans = (high + low)/2
  33.  
  34. if low == ans or high == ans:
  35. break
  36.  
  37. a = 0
  38. b = 1
  39. while l == s[ans-a]:
  40. x = x + (ans-a,)
  41. a += 1
  42. while l == s[ans+b]:
  43. x = x + (ans+b,)
  44. b += 1
  45.  
  46. if l == s[0]:
  47. a = 0
  48. while l == s[a]:
  49. x = x + (a,)
  50. a += 1
  51. if a >= n:
  52. break
  53.  
  54. if l == s[n-1] and l != s[0]:
  55. b = 1
  56. while l == s[n-b]:
  57. x = x + (n-b,)
  58. b += 1
  59.  
  60. if x == ():
  61. print 'Er zit geen %s in de gegenven string!' % l
  62. else:
  63. print "%s" % (x,)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement