Implemented solution for w3 in Python