Algorithms code in python