Often, when I put together an answer, I copy the OP's code in to my IDE, and then refactor things as I see the problems. Each time I make a change, I add it to my answer as a bullet-item of things that were "wrong".
As I work through the code, I build up my answer. It's then no real effort for me to copy the resulting code back to the answer.
What I am trying to say, is that for me, often, the "summary" code and the actual answer are simultaneously written. Further, I would not be able to write the answer nearly as easily if I could not code the changes and visualize the problems as I go.
So, having done the rewrite just to write the answer, it's no stretch to paste that too.
There is no rule against doing it, and I would rather people and the OP could see the result as I intended it to be without having to cobble things together themselves.