Python implements two-point lookup algorithm instances

Source: Internet
Author: User
In this paper, we describe the method of realizing binary search algorithm in Python. Share to everyone for your reference. The implementation method is as follows:

#!/usr/bin/env pythonimport sys def SEARCH2 (a,m): Low  = 0 High   = Len (a)-1 while   (Low <= High):    mid = ( Low + high)/2    midval = A[mid]    if midval < m: Low      = mid + 1     elif midval > M: High      = Mid-1     E LSE:      Print mid       return mid   print-1  return-1if __name__ = = "__main__":  a = [Int (i) for I in List ( SYS.ARGV[1])]  m = int (sys.argv[2])  search2 (a,m)

Run:

administrator@ubuntu:~/python$ Python test_search2.py 123456789 4
3

Note:

1. ' __ ': Because Python's class members are public and publicly accessible public, there is a lack of proprietary private properties like the Orthodox object-oriented language.

So just use __ to simulate the private properties. These __ properties are often used internally and are usually not rewritten. And not read.

Add 2 underline purpose, one is not and common public attribute duplicate name conflict, second, not let the object user (non-developer) arbitrary use.

2.__name__ = = "__main__" indicates that the program script is executed directly.
If not equal to means that the script was introduced with import by another program. Its __name__ property is set to the module name

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.