diff --git a/module6/index.md b/module6/index.md index e86700d..928e1c5 100644 --- a/module6/index.md +++ b/module6/index.md @@ -44,3 +44,5 @@ In Module 6, students will begin to dive into the skills and mindsets necessary ### Week 5 * [Prepping for an Interview](./lessons/Week5/PreppingForInterviews) +* [Recursion](./lessons/Week5/Recursion) + diff --git a/module6/lessons/Week5/Recursion.md b/module6/lessons/Week5/Recursion.md new file mode 100644 index 0000000..f3bfd77 --- /dev/null +++ b/module6/lessons/Week5/Recursion.md @@ -0,0 +1,184 @@ +--- +layout: page +title: Recursive Functions +--- + +### Warmup + +
+ +Imagine you have been given the following code challenge in an interview: + +Build a function that takes in a string, and returns that string reversed. EX: `input: "Megan", output: "nageM"`. You must adhere to the following constraints: +* You may not use _any_ LINQ methods +* You may not use `for`, `foreach`, or `while` +* The function must work for strings of any length\ + +Spend 5 minutes brainstorming what this function might look like. + +
+ +## Recursive Functions + +Recursion is a technique for iterating over an operation by having a function (or method) call itself repeatedly until it arrives at a result. In other words, recursion is a way to loop without using `while` or `for`! Let's take a look at an example. + +Let's say we want to countdown from a given number - the number will always be positive, but it could be * any * integer. We could approach this in two ways: with loops, and with recursion: + +```c# +public class Program +{ + public static void Main() + { + Console.WriteLine("Looping:"); + LoopingCountdown(20); + Console.WriteLine("Recursive:"); + RecursiveCountdown(20); + } + + static void LoopingCountdown(int num) + { + while (num > -1) + { + Console.WriteLine(num); + num--; + } + } + + static void RecursiveCountdown(int num) + { + if (num == 0) + { + Console.WriteLine(num); + } + else + { + Console.WriteLine(num); + RecursiveCountdown(num - 1); + } + } +} +``` + +
+ +Make sure to identify the following characteristics of the method above - talk specifically about how the recursive instructions move us closer to the base case. + +
+ +Use the slides below to walkthrough what is happening on the stack. Not _everything_ that is happening on the stack is specifically called out - I tried to include only the most necessary information. + + + +
+ +### Diagramming the Call Stack + + + +
+ +
+ +### When Disaster Strikes + + + +
+ +
+ +### Recursive Functions + + + +
+ +## Exercises + +The best way to start understanding recursion is to just try doing it! Feel free to work through these problems in any language. + +### Exercise 1 + +Reverse a string. + +```js +// create a function which takes a string of characters and +// recursively calls itself to reverse the string + +// e.g. + +string reversedString = reverse('Ariel'); + +Console.WriteLine(reversedString); // leirA +``` + +### Exercise 2 + +Calculate a number to a specific power. + +```c# +// create a function which takes a number and an exponent and +// recursively calls itself to calculate the product + +// e.g. +int baseNum = 2; +int exponent = 4; +int product = Power(baseNum, exponent); // 2 to the 4th power + +Console.WriteLine(product); // 16 +``` + +### Exercise 3 + +In mathematics, the factorial of a non-negative integer is the product of all positive integers less than or equal to n. For example, the factorial of 5 is 120. + +``` +5 x 4 x 3 x 2 x 1 = 120 +``` + +Write a recursive function that calculates the factorial of a number. + +### Exercise 4 + +The Collatz conjecture applies to positive numbers and speculates that it is always possible to `get back to 1` if you follow these steps: + +- If `n` is 1, stop. +- Otherwise, if `n` is even, repeate this process on `n/2` +- Otherise, if `n` is odd, repeat this process on `3n + 1` + +Write a recursive function that calculates how many steps it takes to get to 1 + +n | collatz(n) |Steps +--- | :---: | --- +2 | 1 | 2 -> 1 +3 | 7 | 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 +4 | 2 | 4 -> 2 -> 1 +5 | 5 | 5 -> 16 -> 8 -> 4 -> 2 -> 1 +6 | 8 | 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 + +