Refined PS2Q1

Refined Problem Set 2 Question 1 into a function.