Final codes for hw2 (q1, q2, q3)