my solution to HW1..could be generalized for use in future homeworks