2nd exercise verified with pure python implementation and comparison