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.

My response would be to say "I'm a little busy right now, can you email me and I'll deal with it later". Chances are some of his questions are legitimate, by forcing him to email you it doesn't interrupt your flow and he is unlikely to bother detailing the problem in an email if its trivial. You then also have a record to show to management if his questions still stay at an unreasonable level.