Rewrite the algorithm learn from ML class(week1-4) in python