-
Notifications
You must be signed in to change notification settings - Fork 30
Exercises 1-5 for recursion tracing #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| # Recursion Problems | ||
|
|
||
| ## Definitions | ||
| Define the following: | ||
| Define the following: | ||
|
|
||
| - Recursion | ||
| Recursion : See recursion | ||
| - Recursive Case | ||
| A problem that can be divided into smaller problems | ||
| - Base Case | ||
| A problem that can be solved immediately | ||
| - Activation Chain/Stack | ||
| This is what Crystal used bubbles to show us, how the methods are using one another | ||
| - Activation Record/Call | ||
| The methods that are called as we follow what is happening in a recursive call | ||
| - Infinite Recursion/Stack Overflow/Stack too deep | ||
| Never ending recursion, too much memory/resources being used up to solve this never ending recursion | ||
| - Tail Recursion | ||
|
|
||
| the return value of any given recursive step is the same as the return value of the next recursive call | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great answer! We didn't cover it in class though - so good googling :) I wonder though, does the answer make sense to you? I've been debating covering this in class - just not sure if there's time. |
||
| ## Tracing through a recursive method | ||
|
|
||
| ### Trace #1 | ||
|
|
@@ -25,9 +31,24 @@ end | |
| ``` | ||
|
|
||
| - What is mystery1(5)? | ||
| = 5 + mystery 4 = 15 | ||
| m(4) = 4 + m(3) = 10 | ||
| m(3) = 3 + m(2) = 6 | ||
| m(2) = 2 + m(1) = 3 | ||
| m(1) = 1 | ||
| - What is mystery1(10)? | ||
| m(10) = 10 + mystery 9= 55 | ||
| m(9) = 9 + m(8) = 45 | ||
| m(8) = 8 + m(7) = 36 | ||
| m(7) =7 + m(6) = 28 | ||
| m(6) = 6 + m(5) = 21 | ||
| m(5) = 5 + mystery 4 = 15 | ||
| m(4) = 4 + m(3) = 10 | ||
| m(3) = 3 + m(2) = 6 | ||
| m(2) = 2 + m(1) = 3 | ||
| m(1) = 1 | ||
| - What is mystery1(0)? | ||
|
|
||
| infinite recursion, stack too deep (never reaches end) | ||
| ### Trace #2 | ||
| ``` | ||
| def mystery2(n) | ||
|
|
@@ -40,8 +61,16 @@ end | |
| ``` | ||
|
|
||
| - What is mystery2(123)? | ||
| mystery2(123) = (3) + mystery2(12) = 5 | ||
| m2(12) = 1 + m2(1) = 2 | ||
| m2(1) = 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) The logic is correct. but m2(12) should be 2 + m2(1) which changes the rest of the recursion math. |
||
| - What is mystery2(9005)? | ||
| mystery2(9005) = 5 + m2(900) = 14 | ||
| m2(900) = 0 + m2(90) = 9 | ||
| m2(90) = 0+ m2(9) = 9 | ||
| m2(9) =9 | ||
| - What is mystery2(-123)? | ||
| mystery2(-123) = -123 | ||
| - _Added Fun: How could we make `mystery2(-123)` work the way we might expect it to work instead of the way it does?_ | ||
|
|
||
| ### Trace #3 | ||
|
|
@@ -61,8 +90,22 @@ end | |
| ``` | ||
|
|
||
| - What is mystery3(1)? | ||
| m3(1) = m3(0) = 100 | ||
| m3(0) = 100 | ||
| - What is mystery3(13)? | ||
| m3(13) = m3(12) =100 | ||
| m3(12) = m(6) =100 | ||
| m(6) = m(3) =100 | ||
| m(3) = m(2) =100 | ||
| m(2) = m(1) =100 | ||
| m(1) = m(0) = 100 | ||
| m(0) = 100 | ||
| - What is mystery3(-6)? | ||
| m(-6) = m(-3) = 200 | ||
| m(-3) = m(-4)= 200 | ||
| m(-4) = m(-2) =200 | ||
| m(-2) = m(-1) = 200 | ||
| m(-1) = 200 | ||
|
|
||
| ### Trace #4 | ||
| ``` | ||
|
|
@@ -76,8 +119,16 @@ end | |
| ``` | ||
|
|
||
| - What is mystery4(10,2)? | ||
| m(10,2) = 10 * m(10, 1) =100 | ||
| m(10,1) = 10 * m(10, 0) = 10 | ||
| m(10,0) =1 | ||
| - What is mystery4(4,3)? | ||
| m(4,3) = 4 * m(4, 2) = 64 | ||
| m(4,2) = 4 * m(4,1) = 16 | ||
| m(4,1) = 4 * m(4,0) = 4 | ||
| m(4,0) = 1 | ||
| - What is mystery4(5,0)? | ||
| m(5,0) = 1 | ||
|
|
||
| ### Trace #5 | ||
| ``` | ||
|
|
@@ -91,11 +142,16 @@ end | |
| ``` | ||
|
|
||
| - What is mystery5("hi")? | ||
| m(hi) = * + m(h) = "**" | ||
| m(h) = * + “”= “*" | ||
| m(“”) = “" | ||
| - What is mystery5("")? | ||
| m(“”) = (“”) | ||
| - What is mystery5("Hi, there!")? | ||
| m(hi, there!) “**********" | ||
| - _Added Fun: How could we make only alphabetic characters to be changed to stars?_ | ||
|
|
||
| ### Trace #6 | ||
| ### Trace #6 #Will come back to this one =) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or not... which is totally fine too :) 5/6 were just added challenges. |
||
| ``` | ||
| def mystery6(s) | ||
| if s == nil || s.length == 0 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... this is true. But not the definition of recursion. Recursion is specifically when a method calls itself, directly or indirectly.