Implement code for ex4.py