-
Notifications
You must be signed in to change notification settings - Fork 47
branches - vi #45
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?
branches - vi #45
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,100 @@ | ||||||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| # Time complexity: O(n^2) | ||||||
| # Space complexity: O(n^2) | ||||||
| def factorial(n) | ||||||
| raise NotImplementedError, "Method not implemented" | ||||||
| if n < 0 | ||||||
| raise ArgumentError | ||||||
| elsif n <= 1 | ||||||
| return 1 | ||||||
| else | ||||||
| return n * factorial(n - 1) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| # Time complexity: O(n) | ||||||
| # Space complexity: O(n) | ||||||
| def reverse(s) | ||||||
| raise NotImplementedError, "Method not implemented" | ||||||
| end | ||||||
| reversed_string = "" | ||||||
| if s.length >= 1 | ||||||
| reversed_string = reverse(s[1..-1]) | ||||||
| reversed_string += s[0] | ||||||
| else | ||||||
| return reversed_string | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| def reverse_inplace(s) | ||||||
| raise NotImplementedError, "Method not implemented" | ||||||
| # Time complexity: O(n) | ||||||
| # Space complexity: O(1) | ||||||
| def reverse_inplace(s, i = 0, j = -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 space complexity is O(n) due to the system stack |
||||||
| n = s.length | ||||||
| if i == n / 2 | ||||||
| return s | ||||||
| else | ||||||
| letter = s[i] | ||||||
| s[i] = s[j] | ||||||
| s[j] = letter | ||||||
| reverse_inplace(s, i + 1, j - 1) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| def bunny(n) | ||||||
| raise NotImplementedError, "Method not implemented" | ||||||
| # Time complexity: O(n) | ||||||
| # Space complexity: O(n) | ||||||
| def bunny(n, ears = 0) | ||||||
|
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 works although I was expecting something like: def bunny(n)
return 0 if n == 0
return 2 + bunny(n-1)
end |
||||||
| if ears == n + n | ||||||
| return ears | ||||||
| else | ||||||
| ears += n | ||||||
| bunny(n, ears) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| def nested(s) | ||||||
| raise NotImplementedError, "Method not implemented" | ||||||
| # Time complexity: O(n) | ||||||
| # Space complexity: O(n) | ||||||
| def nested(s, low = 0, high = (s.length - 1)) | ||||||
| if s.length.odd? | ||||||
| return false | ||||||
| elsif low < high | ||||||
| if s[low] != "(" && s[high] != ")" | ||||||
|
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. Small bug
Suggested change
|
||||||
| return false | ||||||
| end | ||||||
| return nested(s, low + 1, high - 1) | ||||||
| else | ||||||
| return true | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Time complexity: O(n) | ||||||
| # Space complexity: ? | ||||||
| def search(array, value) | ||||||
|
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" | ||||||
| end | ||||||
| new_array_minus_one = 0..-2 | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| def is_palindrome(s) | ||||||
| raise NotImplementedError, "Method not implemented" | ||||||
| if array.empty? | ||||||
| return false | ||||||
| elsif array[-1] == value | ||||||
| return true | ||||||
| else | ||||||
| search(array[new_array_minus_one], value) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: O(n) | ||||||
| # Space complexity: O(1) | ||||||
| def is_palindrome(s, i = 0, j = -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. 👍 But the space complexity is O(n) |
||||||
| n = s.length | ||||||
|
|
||||||
| if i >= s.length + j | ||||||
| return true | ||||||
| elsif | ||||||
| s[i] != s[j] | ||||||
| return false | ||||||
| elsif s[i] == s[j] | ||||||
| is_palindrome(s, i + 1, j - 1) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time complexity: ? | ||||||
| # Space complexity: ? | ||||||
| def digit_match(n, m) | ||||||
| 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.
👍