My own solution for Algorithms Excercise