I have a strange situation at work, where a colleague of mine often asks me and other co-workers for working code. 

I would like to help him, but this constant request of trivial snippets interrupts my thoughts and sometimes makes it hard to concentrate.

Plus, I have the impression (...) that this requests are generated by lack of competence, more than by laziness. In fact, he often asks things pretending to know the answer, since when I solve the problem he usually says things like "Sure", "Yes, that's what I thought", giving me the impression that my answer isn't worth it.

How can I solve this embarrassing situation?  

Should I show more explicitly in front of other colleagues his lack of knowledge (by saying things like: "do it yourself if you can, please") or continue giving him what he wants?

I think that he should aggregate all his questions in one, so that I can give him a portion of my time and he can work all by himself on his things.

There is no hierarchy in the team, I must say we both have a similar seniority of five years, more or less. For the same reason I believe I cannot report to management, since trivial questions are often ignored.

I discussed with other two members and they agree with me: in fact he often ask things cycling through colleagues.