Algorithm problem solutions in Python