September+23rd+2016+by+Doris+Xue

= list algorithm =

1. test function (the initial code can be found in chapter 6)
It takes a boolean argument and will either print a message telling us that the test passed, or it will print a message to inform us that the test failed. The first line of the body (after the function’s docstring) magically determines the line number in the script where the call was made from. This allows us to print the line number of the test, which will help when we want to identify which tests have passed or failed. (chapter 6) code format="python" import sys

def test(did_pass): """ Print the result of a test.  """ linenum = sys._getframe(1).f_lineno  # Get the caller's line number. if did_pass: msg = "Test at line {0} ok.".format(linenum) else: msg = ("Test at line {0} FAILED.".format(linenum)) print(msg) code  linenum is line number of the carring function. If did_pass successfully carried, it will return the line number where expect result equal to function's result.

 2. linear search
linear search a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. code format="python" def search_linear(xs,target): for i in range(len(xs)): if xs[i] == target: return i   return -1 code Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n/2 comparisons, but the average case can be affected if the search probabilities for each element vary. ==
 * for ( i, v ) in enumerate(a list): i is the index, v is the value list[index].

==

3.binary search (sorted list)
binary search is a search logrithm that finds the position of a target value within a sorted list. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. Binary search runs in at worst logrithmic time, making Ologn comparisons, where n is the number of elements in the array and log is the binary logrithm; and using only constant (O(1)) space. code format="python" def search_linear(xs,target): for i in range(len(xs)): if xs[i] == target: return i   return -1 code

code format="python" def search_binary(xs, target): """ Find and return the index of key in sequence xs """ lb = 0 ub = len(xs) while True: if lb == ub:  # If region of interest (ROI) becomes empty return -1

# Next probe should be in the middle of the ROI mid_index = (lb + ub) // 2

# Fetch the item at that position item_at_mid = xs[mid_index]

# print("ROI[{0}:{1}](size={2}), probed='{3}', target='{4}'"       #       .format(lb, ub, ub-lb, item_at_mid, target))

# How does the probed item compare to the target? if item_at_mid == target: return mid_index     # Found it! if item_at_mid < target: lb = mid_index + 1   # Use upper half of ROI next time else: ub = mid_index       # Use lower half of ROI next time code

the complexity of binary search is log2N, because binary search only concern the rigion of interset so that every time this region is divided by 2 every time. if the len of list is a even numver, the mid should be calculated in either int way or ceil way every time. the probe compares mid[xs] and target

**3. if _name_==_‘main’_:** if we carry a file in cmd, the _"main"_ equal to _name_, because every module has an attribute about _name_, this atribute decided by the function of this module. So, if this file is imported, it is will inculde some extra imfronation but the name of this file. At that time, the _name_ doesn't equal to _"main"_.

So, If _name_ == _"main"_ can heip us test the modules or classes and it can prevent us carring a function or a class two times when we importing modules. code format="python" def func: print("func in one.py")
 * 1) file one.py

print("top-level in one.py")

if __name__ == "__main__": print("one.py is being run directly") else: print("one.py is being imported into another module")

import one
 * 1) file two.py

print("top-level in two.py") one.func

if __name__ == "__main__": print("two.py is being run directly") else: print("two.py is being imported into another module")

code

4. merge algorithms
Merging two sorted lists into one can be done in linear time and linear space .The complexity of merge algorithms is logN. Some important application of merge algorithms (that means it may present in exam) : 1.return only other items that are present in both items 2.bag diff ( remove duplicates) code format="python" def search_binary(xs, target): """ Find and return the index of key in sequence xs """ lb = 0 ub = len(xs) while True: if lb == ub:  # If region of interest (ROI) becomes empty return -1

# Next probe should be in the middle of the ROI mid_index = (lb + ub) // 2

# Fetch the item at that position item_at_mid = xs[mid_index]

# print("ROI[{0}:{1}](size={2}), probed='{3}', target='{4}'"       #       .format(lb, ub, ub-lb, item_at_mid, target))

# How does the probed item compare to the target? if item_at_mid == target: return mid_index     # Found it! if item_at_mid < target: lb = mid_index + 1   # Use upper half of ROI next time else: ub = mid_index       # Use lower half of ROI next time code