I recently saw a question about a homework assignment and the OP had tried his best to show the code he had below the assignment text. But he clearly did not understand the assignment and he got derailed by it, making things up in his code that were never even asked for.
It got downvoted for reasons I still can't understand as it was clear that it was a homework assignment and this student was clearly struggling with programming. He was also new so I consider that a bit rude as it got downvoted without comments, plus two "close" votes as the question seemed unclear. (Basically, it is: do my homework!)
So I wrote an answer and as a very experienced software developer, I used a solution that is far more advanced that a beginner would understand, yet it followed the assignment to the letter. I also pointed out that he would never convince his teacher that he wrote it, unless he could explain all the details in it. Still, the answer should compile and show him what his code is supposed to do. He can now focus on writing his own solution, try to understand the things I did and provide his teacher with my code while explaining how he got it and ask the teacher if he could explain how my solution works. So it's useful, educational and he can't cheat with my answer.
But what is the protocol for answering homework assignments? I saw a hopelessly lost student so I provided a working solution and hope to trigger him to explore and learn from it, as his teacher will not believe that he wrote it.
Is this the right way to answer homework assignments?