I got the idea for problems like factorials or finding sum from 1 to n, where there’s a pattern visible. Like this:
I got it for things like addition, subtraction, division, multiplication of two numbers as well.
Now, I am wondering how it’ll be for finding a palindrome of a number? I’ve a solution but I’m not understanding how we came towards it.
Can you post a link to the description of the challenge?
In general, recursion works by reducing the complexity of a problem through each iteration.
Perhaps the palindrome question says that numbers like 12344321 are palindromes. If we want to check through code though, how would we reduce the size of this problem recursively?
Maybe by checking if 234432 is a palindrome?
And maybe that checks if 3443 is a palindrome
And maybe that checks if 44 is a palindrome
So if you can imagine that a problem set 12344321 can be reduced recursively then you can try to code that.
Recursion an excellent way of solving arbitrary problems. And it has nothing to do with programming. Computers are just really good at doing the same thing over and over again, which makes recursion a great problem solving technique.
A tree is recursive data type. What does this mean? Just as a recursive function makes calls to itself, a recursive data type has references to itself. A tree is one of the many data structures, which is naturally recursive , i.e. recursive by its design itself.
Similarly by definition, a word which is 0 or 1 letters is a palindrome. The recursive step palindrome function is to check if the first letter and last letter are equal. If they are equal, then remove the first and last character and check the resulting string.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.