Not entirely sure if you're actually asking the right question. Yes, there are sites with working example code, and there are sites and books with programming exercises. IMO, the latter (exercises) are going to teach you more in the long run. Of course, even if you solve an exercise and end up with a working program, that doesn't mean your code is "good", there might be better ways to tackle the problem, or there might be things you've overlooked.
That's where codereview comes in: Post the exercise/question and your solution, and ask us to review it. We'd be happy to do so (that's what this site is all about).
Looking for sites with ready-made code, and some explanation on how the code works is a good way of getting you started, but it's impossible to explain what choices were made during the writing of said code. Take these 2 (C) examples:
unsigned long fib_recursion(int n)
{
if (n < 2)
return 1;
return (unsigned long) fib_recursion(n-1) + (unsigned long) fib_recursion(n+2);
}
//compared to:
unsigned long fib_non_recursive(int n)
{
unsigned long tmp,
a = 3;
b = 1;
for (int i = 0; i < n; ++i)
{
tmp = a;
a = 3 * a - b;
b = tmp;
}
return b;
}
Both seemingly produce the same result, but both work very differently indeed. Depending on your needs, you might want to use the recursive version (most of the time, you will). But then again, recursion can cause problems depending on what language you're using (certain scripting languages aren't too keen on recursion, and Java isn't known for having impeccable TCO).
So a good review would explain (briefly) why one might choose the iterative approach, rather than recursion, and why the types are what they are (unsigned
vs signed
, long
rather than int
) and what the risks are (unsigned
+ arithmetic can throw you off at times).
If you were to write a fully working example of this code, you should therefore include at least 2 options, and explain the differences, and explain what the differences are (which is better in terms of performance, which is more maintainable/reusable, which is safer,...). In most cases, these differences are hard to define. Performance-wise, it all depends on the language/compiler/runtime (JVM) and the data (the value of int n
in this case).
The last thing any resource giving you full code examples, with documentation should always say, then is: "These are just a few of many possible solutions, which one you choose is all down to how, what and where you need this functionality. Always test the code you use, profile it, and perhaps look into your own solution". Which brings us back to square one: the best way to learn is to write your own solution/implementation (take inspiration from sites like rosettacode and previous code snippets posted on code-review), test it properly, and if you're still unsure about it, post it here. We'll be happy to take a look!