I appreciate original posters' honesty.
He made clear that it's for a homework exercise, and he made clear that he was looking for advice. Many others have offered advice without offering code.
I beleive you are wrong offering code. The OP probably wishes to try to write it himself (code-wise and not analysis-wise).
Obviously, your code is good and I beleive that you wrote it with good intentions, and would be extremely helpful after the OP has delivered his homework, but at this time I think you should remove it, as a favor to the original poster (if he hasn't read that already).