-
Notifications
You must be signed in to change notification settings - Fork 53
Partial completed #40
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(n) | ||
| # Space complexity: O(n) | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n < 0 | ||
| raise ArgumentError | ||
| elsif n <= 1 | ||
| return 1 | ||
| else | ||
| n * factorial(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n^2) | ||
|
|
||
| def reverse(s) | ||
|
Comment on lines
+15
to
18
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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| if s.empty? || s.length == 1 | ||
| return s | ||
| else | ||
| s[-1] + reverse(s[1..-2]) + s[0] | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # s = "bear" | ||
| # reverse_in_place!(s) | ||
| # s == "raeb" | ||
| # "bear" => "reab" => "raeb" | ||
| # reverse("bear") calls reverse("ea") calls reverse("") | ||
| # reverse_in_place("bear") | ||
| # calls r_i_p_helper("bear", 0) | ||
| # calls r_i_p_helper("reab", 1) | ||
| # calls r_i_p_helper("raeb", 2) -> returns "raeb" | ||
| # r_i_p_helper("", 0) s.length = 0, idx = 0 | ||
| # ...("a", 0) s.length = 1, idx = 0 | ||
| # ...("raeb", 2) s.length = 4, idx = 2 | ||
| # ...("butter", 0) => ("rutteb", 1) => ("rettub", 2) ("rettub", 3) => return | ||
| def reverse_in_place_helper(s, index) | ||
|
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. 👍 Nice |
||
| if index == s.length / 2 | ||
| return s | ||
| else | ||
| temp = s[index] | ||
| s[index] = s[s.length - index - 1] | ||
| s[s.length - index - 1] = temp | ||
| reverse_in_place_helper(s, index + 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_in_place(s) | ||
|
Comment on lines
+50
to
+52
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. 👍 |
||
| reverse_in_place_helper(s, 0) | ||
| end | ||
|
|
||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
|
Comment on lines
+56
to
58
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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 0 | ||
| else | ||
| return 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise NotImplementedError, "Method not implemented" | ||
| 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.
👍