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
87 changes: 84 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Recursion Problems

## Definitions
Define the following:
Define the following:

- Recursion
--see recursion; when a method calls itself
- Recursive Case
--the case where the recursion will be called
- Base Case
--the case that stops the recursion
- Activation Chain/Stack
--the process of stepping through the recursion
- Activation Record/Call
--an individual step in the activation chain
- Infinite Recursion/Stack Overflow/Stack too deep
--never reaching the base case--the error that gets raised
- Tail Recursion

## Tracing through a recursive method
Expand All @@ -24,9 +30,27 @@ def mystery1(n)
end
```

- What is mystery1(5)?
- What is mystery1(5)?
> mystery1(5) = 15
m(5) = 5 + m1(4) --> 15
m(4) = 4 + m1(3) --> 10
m(3) = 3 + m1(2) --> 6
m(2) = 2 + m1(1) --> 3
m1(1) = 1
- What is mystery1(10)?
> mystery1(10)= 55
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)?
> m1(0) --> returns stack too deep error because base case of n == 1 is never met

### Trace #2
```
Expand All @@ -40,9 +64,21 @@ end
```

- What is mystery2(123)?
>mystery2(123) = 6
m2(123) = 3 + m2(12) --> 6
m(12) = 2 + m2(1) --> 3
m(1) = 1
- What is mystery2(9005)?
>mystery2(9005) = 14
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)?
>mystery2(-123) = -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?_
>make the first part of the conditional check if a negative number is greater than -10 and a pos. number is less than ten. i think division would need to return positive values (i.e. -123/10 should return 12 not -12), or be converted to a pos value.

### Trace #3
```
Expand All @@ -61,8 +97,24 @@ end
```

- What is mystery3(1)?
- What is mystery3(13)?
>mystery3(1) = 100
m3(1) = m3(0) --> 100
m3(0) = 100
- What is mystery3(13)?
mystery3(13) = 100
m3(13) = m3(12) --> 100
m3(12) = m3(6) --> 100
m3(6) = m3(3) --> 100
m3(3) = m3(2) --> 100
m3(2) = m3(1) --> 100
m3(1) = 100
- What is mystery3(-6)?
>mystery3(-6) = 200
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,7 +128,17 @@ end
```

- What is mystery4(10,2)?
>mystery4(10,2) = 100
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)?
>mystery4(4,3) = 64
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)?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed this one :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops!


### Trace #5
Expand All @@ -91,8 +153,26 @@ end
```

- What is mystery5("hi")?
>mystery5("hi") = string of two asterisks
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct, but I'd say "**" instead of "string of two asterisks"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes! I couldn't figure out how to put in strings of asterisks into the markdown file -- the formatting was going wild.

m5("hi") = "*" + m5("i") --> string of two asterisks
m5("i") = "*" + m5("") --> "*"
m5("") = ""

- What is mystery5("")?
>m5("") = ""
- What is mystery5("Hi, there!")?
>m5("Hi, there!") = "*" + m5("i, there!") --> string of 10 asterisks
m5("i, there!") = "*" + m5(", there!") --> string of 9 asterisks
m5(", there!") = "*" + m5(" there!") --> string of 8 asterisks
m5(" there!") = "*" + m5("there!") --> string of 7 asterisks
m5("there!") = "*" + m5("here!") --> string of 6 asterisks
m5("here!") = "*" + m5("ere!") --> string of 5 asterisks
m5("ere!") = "*" + m5("re!") --> string of 4 asterisks
m5("re!") = "*" + m5("e!") --> string of 3 asterisks
m5("e!") = "*" + m5("!") --> string of 2 asterisks
m5("!") = "*" + m5("") --> "*"
m5("") = ""

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

### Trace #6
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is definitely tougher. But good job on the others!

Expand All @@ -111,6 +191,7 @@ end
```

- What is mystery6("goodnight moon")?

- What is mystery6("Ada Developers Academy")?
- What is mystery6("Hi, there!")?
- _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")?_