Make the answer more compact. First, give a max. 2 sentence description of the algorithm, then give a few bullet points outlining the major parts of the algorithm and only then give an implementation that includes 3 unit tests at the end.