Make a summary of what you have learned before. Before looking at the two-point search method, can only be like rather than understand the approximate. Now with so many days of accumulation of knowledge can be written on their own.
And on the basis of the algorithm book, the need to find the number to do a human-machine interactive operation.
In addition, the initial exposure to the __name__== "__main__" this piece, but also only a preliminary understanding of a probable. When debugging, it is found that this statement will not be executed in conjunction with the next two pieces of code. The principle is not clear, check the information, specious, look at the back can be further understood.
In addition, a more detailed understanding of return and print is today. My understanding is that once the statement has a return, similar to break, the subsequent program will no longer execute. The print statement will continue to execute later. This should be the biggest difference.
This is my first paragraph of the algorithm program, in the original book based on the addition of a lot of Chinese man-machine interaction, did not expect a little C + + foundation I can actually write algorithm program. Thank you for this time to strive for yourself!
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #author:show5304 5 6 defBinarySearch (list, item):7low=08High=len (list)-19 ifIteminchlist:Ten whileTrue: OneMid=int ((Low+high)/2) A iflist[mid]==Item: - Print(str (item) +"in the array, the first"+str (mid) +"a location") - return the eliflist[mid]<Item: -Low=mid+1 - elifList[mid]>Item: -High=mid-1 + Else: - Print("There's no such number .") + return A at - if __name__=="__main__": -arr = [1, 6, 9, 15, 26, 38, 49, 57, 63, 77, 81, 93] - whileTrue: -Key = input ("Please enter the number you want to find:") - ifKey = =" ": in Print("Thanks for using! ") - Break to Else: +BinarySearch (arr, int (key))
Python Learning (5) write a binary algorithm program