|
1 | | -# Recursion |
| 1 | +# Recursion for Iteration |
2 | 2 |
|
3 | 3 |
|
4 | 4 | [Any function that can be written iteratively (_with loops_) can be written using recursion][recursion-and-iteration], and [vice-versa][recursion-is-not-a-superpower]. |
5 | 5 | A recursive strategy [may not always be obvious][looping-vs-recursion] or easy — but it is always possible. |
6 | 6 | So the `while-loop`s used in other approaches to Wordy can be re-written to use recursive calls. |
7 | 7 |
|
8 | | -That being said, Pyton famously does not perform [tail-call optimization][tail-call-optimization], and limits recursive calls on the stack to a depth of 1000 frames, so it is important to only use recursion where you are confident that it can complete within the limit (_or something close to it_). |
| 8 | +That being said, Python famously does not perform [tail-call optimization][tail-call-optimization], and limits recursive calls on the stack to a depth of 1000 frames, so it is important to only use recursion where you are confident that it can complete within the limit (_or something close to it_). |
9 | 9 | [Memoization][memoization] and other strategies in [dynamic programming][dynamic-programming] can help to make recursion more efficient and "shorter" in Python, but it's always good to give it careful consideration. |
10 | 10 |
|
11 | 11 | Recursion works best with problem spaces that resemble trees, include [backtracking][backtracking], or become progressively smaller. |
12 | | - Some examples include financial processes like calculating [amortization][amortization] and [depreciation][depreciation], tracking [radiation reduction through nuclei decay][nuclei-decay], and algorithms like [biscetion search][bisection-search], [depth-firs search][dfs], and [merge sort][merge-sort]_). |
| 12 | + Some examples include financial processes like calculating [amortization][amortization] and [depreciation][depreciation], tracking [radiation reduction through nuclei decay][nuclei-decay], and algorithms like [biscetion search][bisection-search], [depth-firs search][dfs], and [merge sort][merge-sort]. |
13 | 13 |
|
14 | | -Other algorithms such as [breadth-first search][bfs], [Dijkstra's algorithm][dijkstra], and [Bellman-Ford Algorithm][bellman-ford] lend themselves better to iteration. |
| 14 | +Other algorithms such as [breadth-first search][bfs], [Dijkstra's algorithm][dijkstra], and the [Bellman-Ford Algorithm][bellman-ford] lend themselves better to iteration. |
15 | 15 |
|
16 | 16 |
|
17 | 17 | ```python |
@@ -170,7 +170,7 @@ def calculate(question): |
170 | 170 | ``` |
171 | 171 |
|
172 | 172 |
|
173 | | -This variation shows how the dictionary of operators from `operator` can be augmented with [regex][re] to perform string matching for a question. |
| 173 | +This variation shows how the dictionary of operators from `operator` can be augmented with [regex][re] to perform string matching for a question. |
174 | 174 | Regex are also used here to check that a question is a valid and to ensure that the base case (_nothing but digits are left in the question_) is met for the recursive call in `calculate()`. |
175 | 175 | The regex patterns use [named groups][named-groups] for easy reference, but it's not necessary to do so. |
176 | 176 |
|
|
0 commit comments