Solution to https://www.codeeval.com/open_challenges/25/