solution to https://www.codeeval.com/browse/86/