Implemented solution for w4 in Python