+ 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.
5 Réponses
+ 3
I do not know if they are cool but at least they are recursive ^^
https://code.sololearn.com/cg3DwhMKOw1E/?ref=app
+ 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.
+ 1
# reverse a string , if you want to
https://code.sololearn.com/cGOFrgHb6hC9/#py
0
Factorial, Fibonacci, Merge Sort...
0
here you are. the code recursively checks parenthesis matching: (123+(245+1) )( ..()()..)
https://code.sololearn.com/cesSwXg4kmt6/?ref=app