Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 83 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Recursion Problems

## Definitions
Define the following:
Define the following:

- Recursion
- Recursive Case
- Base Case
- Activation Chain/Stack
- Activation Record/Call
- Infinite Recursion/Stack Overflow/Stack too deep
- Tail Recursion
- Recursion: A method that calls itself
- Recursive Case: Part of the method where the recursion happens
- Base Case: Stoping case, where the recursion stops rather than going onto infinitely.
- Activation Chain/Stack: The whole chain of events that happen in a recursive method.
- Activation Record/Call: The separate pieces of the Activation Chain (each individual 'recursion bubble')
- Infinite Recursion/Stack Overflow/Stack too deep: When there is no stopping point in the recursion, can never hit the base case.
- Tail Recursion: When the recursive call is the very last thing in the method.

## Tracing through a recursive method

Expand All @@ -25,8 +25,24 @@ end
```

- What is mystery1(5)?
m1(5) = 5 + m1(4) = **15**
m1(4) = 4 + m1(3) = 10
m1(3) = 3 + m1(2) = 6
m1(2) = 2 + m1(1) = 3
m1(1) = 1
- What is mystery1(10)?
m1(10) = 10 + m1(9) = **55**
m1(9) = 9 + m1(8) = 45
m1(8) = 8 + m1(7) = 36
m1(7) = 7 + m1(6) = 28
m1(6) = 6 + m1(5) = 21
m1(5) = 5 + m1(4) = 15
m1(4) = 4 + m1(3) = 10
m1(3) = 3 + m1(2) = 6
m1(2) = 2 + m1(1) = 3
m1(1) = 1
- What is mystery1(0)?
**"Stacked too deep" / infinite recursion**

### Trace #2
```
Expand All @@ -40,8 +56,16 @@ end
```

- What is mystery2(123)?
m2(123) = 3 + m2(12) = **6**
m2(12) = 2 + m2(1) = 3
m2(1) = 1
- What is mystery2(9005)?
m2(9005) = 5 + m2(900) = **14**
m2(900) = 0 + m2(90) = 9
m2(90) = 0 + m2(9) = 9
m2(9) = 9
- What is mystery2(-123)?
m2(-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
Expand All @@ -61,8 +85,22 @@ end
```

- What is mystery3(1)?
m3(1) = m3(0) = **100**
m3(0) = 100
- What is mystery3(13)?
m3(13) = m3(12) = **100**
m3(14) = m3(6) = 100
m3(6) = m3(3) = 100
m3(3) = m3(2) = 100
m3(2) = m3(1) = 100
m3(1) = m3(0) = 100
m3(0) = 100
- What is mystery3(-6)?
m3(-6) = m3(-3) = **200**
m3(-3) = m3(-4) = 200
m3(-4) = m3(-2) = 200
m3(-2) = m3(-1) = 200
m3(-1) = 200

### Trace #4
```
Expand All @@ -76,8 +114,16 @@ end
```

- What is mystery4(10,2)?
m4(10,2) = 10 * m4(10,1) = **100**
m4(10,1) = 10 * m4(10,0) = 10
m4(10,0) = 1
- What is mystery4(4,3)?
m4(4,3) = 4 * m4(4,2) = **64**
m4(4,2) = 4 * m4(4,1) = 16
m4(4,1) = 4 * m4(4,0) = 4
m4(4,0) = 1
- What is mystery4(5,0)?
m4(5,0) = **1**

### Trace #5
```
Expand All @@ -91,8 +137,25 @@ end
```

- What is mystery5("hi")?
m5("hi") = * + m5("i") = __**__
m5("i") = * + m5("") = *
m5("") = ""
- What is mystery5("")?
m5("") = __""__
- What is mystery5("Hi, there!")?
m5("Hi, there!") = * + m5("i, there!") = __**********__
m5("i, there!") = * + m5(", there!") = *********
m5(", there!") = * + m5(" there!") = ********
m5(" there!") = * + m5("there!") = *******
m5("there!") = * + m5("here!") = ******
m5("here!") = * + m5("ere!") = *****
m5("ere!") = * + m5("re!") = ****
m5("re!") = * + m5("e!") = ***
m5("e!") = * + m5("!") = **
m5("!") = * + m5("") = *
m5("") = ""


- _Added Fun: How could we make only alphabetic characters to be changed to stars?_

### Trace #6
Expand All @@ -111,6 +174,18 @@ end
```

- What is mystery6("goodnight moon")?
m6("goodnight moon") = m6("moon") + " " + "goodnight" = **" moon goodnight"**
m6("moon") = m6("") + " " + "moon" = " moon"
m6("") = ("")

- What is mystery6("Ada Developers Academy")?
m6("Ada Developers Academy") = m6("Developers Academy") + " " + "Ada" = **" Ada Developers Academy"**
m6("Developers Academy") = m6("Academy") + " " + "Developers" = " Developers Academy"
m6("Academy") = m6("") + " " + "Academy" = " Academy"
m6("") = ""

- What is mystery6("Hi, there!")?
m6("Hi, there!") = m6("there!") + " " + "Hi," = **" there! Hi,"**
m6("there!") = m6("") + " " + "there!" = " there!"
m6("") = ""
- _Added Fun: How could we make the reversal happen by letter, instead of by word (i.e. Make it so that mystery6("goodnight moon") returned "noom thgindoog")?_