Well, let's start with the first one. What does it do, and how does it do it? Can you explain in English how it works? From that we can then go on to write the algorithm.