I'm enjoying learning python, but struggling to understand recursion. I feel like a complete idiot. If I'm struggling with recursion, should I just give up?
All sorts of topics are hard when they are new. Add to this when a topic has some difficult parts. Recursion is one of the tricky ones. The concept is simple: a function that calls itself. Doing that in a useful way is the hard part.
One of my friends had a very interesting recursive approach to understanding things that she could not understand -- keep reading about them until you understand :) Don't give up! You can never fail if you keep on trying. Google "recursion explained" or ask questions here on OpenStudy. Recursion is hard to use correctly, but it is not needed to be used that often. It's on of those techniques that is a great optimization techniques, but in real life development it is not used very often.
I suppose the best way forward is to find recursion exercises to do...it's just frustrating.lol. I'm still stuck on prob_set for. The end bit
Here is an interesting resource for you: http://www.sparknotes.com/cs/recursion/examples/problems.html you can take on re-writing these in Python (versus C). And here is an interactive tutorial on recursion in Python: http://interactivepython.org/courselib/static/pythonds/Recursion/recursionsimple.html Hope these few resources will help -- but as always -- feel free to ask specific questions here!
A recursive function has 3 parts that are required: When to recurse more, a base case, and what to do as it "unwinds". You actually started doing recursion in first grade when you added two numbers. You were recursing as you set up the addition, making sure that the numbers lined up right. The base case was when you didn't have any more numbers to set up. As you unwound the recursion, you added the numbers and the carry, output the last digit of the addition and returned the carry to the calling function. IMO, a good exercise for learning recursion is a program that checks to see if a word is a palindrome, spelled the same forwards and backwards.
Thanks. Going to do more recursion exercises until it's engraved in my brain. People here are so helpful :) I've bookmarked those two websites
And for some, the reason why they call it re\(\bf\color{red}{curse}\)ive is because of what students do while learning this topic. So don't feel too bad! Many people have issues with this.
IMO, the more programming that you've done before you really tackle recursion, the harder recursion is. Also, recursive programming is pretty simple if you keep those three sections in mind. The interesting thing is to spot problems that are recursive in nature.
Join our real-time social learning platform and learn together with your friends!