Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3.  
  4. def binary_search(card_list, card):
  5. low = 0
  6. high = len(card_list) - 1
  7. while low <= high:
  8. mid = (low + high) // 2
  9. guess = card_list[mid]
  10. if guess == card:
  11. print("{0}番目に{1}はあります".format(mid, card))
  12. return
  13. elif guess < card:
  14. low = mid + 1
  15. else:
  16. high = mid - 1
  17. return
  18.  
  19.  
  20. if __name__ == '__main__':
  21. heart_cards = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
  22. heart_eight = 'c'
  23. binary_search(heart_cards, heart_eight)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement