First solution is in Python, functional-style.