Python Two-point find detailed

Source: Internet
Author: User
Let's start with an example.

#!/usr/bin/env python import sys   def search2 (a,m): Low   = 0 High    = Len (a)-1 while    (Low <= High):     mi D = (low + high)/2     midval = A[mid]        if midval < m: Low       = mid + 1      elif midval > M: High       = mid-1< C10/>else:       print mid        return mid    print-1   return-1  if __name__ = = "__main__":   a = [Int (i) For I in list (sys.argv[1])]   m = int (sys.argv[2])   

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

Python uses two-point search to find the subscript for a number

To consider the case of duplicate numbers

Class solution (Object):  def searchrange (self, Nums, target): ""    "    : Type Nums:list[int]    : type target: int    : Rtype:list[int] "" "    def binary_search (start,end,value): While      end>=start:        mid = (start +end)//2        Print (mid)        if nums[mid]>target:          end = Mid-1        elif nums[mid]
 
  
   
  =start and Nums[mid+value] = = target:              end = Mid+value            else:              return mid          else:            if Mid+1<=end and Nums[mid+value] = = Target:              start = Mid+value            else:              return mid       return-1    a=binary_search (0, Len (nums) -1,-1)    B=binary_search (0,len (nums) -1,1)    return [a,b]a = solution () L = [2,2]print (A.searchrange (l , 2))
 
  

The definition of the binary algorithm is not more said, Baidu a bit to know (support homemade laughter)

Import sys Source = [1,2,3,4,5,6,7,8,9,10] #must is in order des = Int (sys.argv[1]) low = 0 High = len (source)-1 Targeti  Ndex =-1 print "des=", des while low <= high:   middle = (low + high)/2   if des = = Source[middle]:     targetindex = Middle     break   elif des < Source[middle]: High     = middle-1     print "Middle element[index=", Middle, ", Value= ", Source[middle],"] is bigger than DES, continue search from[", Low," to ", High,"] "   else: low     = middle + 1     

Finally in sharing a

' filename--binarysearch.py '  src = []  def binarysearch (Low, high, Target, *src):   ' Two-point lookup ' while low   <=  High:     mid = (low + high)//2     midval = Src[mid]     if target < midval: High       = mid-1     elif target > Midval: Low       = mid + 1     else:       return mid     binarysearch (low, high, Target, *src)  print (' please Input number: ') for number in range:   src.append (int (input (' Num%d: '% number))  sortlist = tuple (src) 
  key = Int (input (' Please input key: ')] location = BinarySearch (0, Len (SRC)-1, Key, *sortlist)  if location! = None :   print (' Find target at%d '% (location + 1)) Else:   
  • 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.