Whooooooooooo cares?
This behavior - "code-begging" - used to really annoy me on forums and mailing lists, because you'd put the time into answering one of these and... Your work would sink beneath the waves forever. You were, by and large, writing for the benefit of one person - so wanting to make sure that they deserved the effort is pretty fair. Not that you really could - generally, the best you could hope for is someone who'd at least respond and let you know that your answer worked before you and everyone else forgot about it forever.
Yeah, forums were pretty broken for this sorta thing. But that's why Stack Overflow was created in the first place...
See, you are not at the mercy of the asker here. Even if they take your answer and disappear forever, your answer sticks around to help the next person with the same problem. The first person to ask for code to do X could be a lazy, no-good resume-padding ingrate, and it still doesn't matter - your answer is still there for everyone else who might need to do X, most of whom will also be lazy ingrates because they come from The Internet and that's what Internet People are like but... You'll get a few who aren't, a few for whom your answer will make a difference, a few who'll give back when the tables are turned and they find themselves possessing unique knowledge of how to do Y...
...and that's how Stack Overflow works.
When I first learned to program, I saved for probably a month to buy a crappy copy of Turbo C. I read the worthless book that came with it, the (actually quite good) online documentation, and in another month I'd managed to become... a really lousy C programmer. Then I found my way onto the 'Net, and all the open source I could eat. And for the next few years, I downloaded and read and learned... Eventually, I started answering others' programming questions - not because they deserved it, but because I didn't deserve any of the education I got, and yet it was freely-given anyway.
A good many of these questions - like your example - have other problems: they're unclear, they're impossibly broad, they're duplicates... But in the absence of all of that, if the worst you can say about a given question is, "I don't think the asker deserves code" then... Stop caring about the asker. They may be a spoiled kid like I used to be, but that doesn't mean only such characters will benefit from your work - if you desire to write, then write, write for the ages and write for the masses, and sooner or later someone will appreciate the lessons you've taught.