-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Angela attempting recursion #4
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,49 +1,89 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(nlogn) | ||
| # Space complexity: O(nlogn) | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n < 0 | ||
| raise ArgumentError, "Error!!" | ||
| end | ||
|
|
||
| if n == 0 || n == 1 | ||
| return 1 | ||
| else | ||
| return n * factorial(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
|
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 O(n^2) since |
||
| # Space complexity: O(n) | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 0 | ||
| return s | ||
| else | ||
| return s[-1] + reverse(s[0...-1]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s) | ||
|
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 not in place since you create a new string. Think about making a helper which takes the string, a left index and right index. The base case is when |
||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 0 | ||
| return s | ||
| else | ||
| return s[-1] + reverse(s[0...-1]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 0 | ||
| else | ||
| return 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(s) | ||
|
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. You seem to be mixing an iterative and recursive solution, see my notes on the reverse in place method. |
||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 0 | ||
| return true | ||
| elsif find_pair(s).nil? | ||
| return false | ||
| end | ||
|
|
||
| if find_pair(s) | ||
| s.slice!(find_pair(s), 2) | ||
| return nested(s) | ||
| end | ||
| end | ||
|
|
||
| def find_pair(s) | ||
| i = 0 | ||
| while i < s.length | ||
| if s[i] == "(" && s[i + 1] == ")" | ||
| return i | ||
| end | ||
| i += 1 | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
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.
Nope this is O(n) since n just decreases by one with each iteration.