+ 2

Anyone have some cool examples of recursive algorythms?

I've just leaned about them and only seen one that seemed beneficial as a recursive algorythm instead of a loop. It was a factorial program. I'd like to see other examples.

28th Jul 2017, 10:30 PM
Michael Russell
Michael Russell - avatar
5 ответов
+ 3
I do not know if they are cool but at least they are recursive ^^ https://code.sololearn.com/cg3DwhMKOw1E/?ref=app
28th Jul 2017, 10:46 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 17
How about a parser? Every piece of a statement is either a 'basic' component, or a statement, the former is your base case, the latter is your recursive case. Heuristics for game opponents: Try a move. Is the game over? If yes, assign a value based on who won. If no, make a minor alteration to the value of the next move you try, then try that move. (This one can get complicated quick, but consider Tic Tac Toe, you can trace that logic tree to always tie or win) The caveat here is that while recursion is rather more intuitive, you are using different resources than you might in a loop. you should take care with the cases in which you use it. Anything you can do via recursion you can do with a loop (and vice versa) so pick your tool wisely.
29th Jul 2017, 3:17 AM
Jim
Jim - avatar
+ 1
# reverse a string , if you want to https://code.sololearn.com/cGOFrgHb6hC9/#py
29th Jul 2017, 8:14 AM
richard
0
Factorial, Fibonacci, Merge Sort...
28th Jul 2017, 11:44 PM
Denis Felipe
Denis Felipe - avatar
0
here you are. the code recursively checks parenthesis matching: (123+(245+1) )( ..()()..) https://code.sololearn.com/cesSwXg4kmt6/?ref=app
29th Jul 2017, 7:14 AM
yuri