Completed code for the exercises in lecture 4