-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Ari (in progress!) #15
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,27 +1,50 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), where n is the value of the number | ||
| # Space complexity: O(n), where n is the value of the number | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 1 | ||
| elsif n.nil? || n < 0 | ||
| raise ArgumentError, "This value is not valid." | ||
| end | ||
|
|
||
| return n * factorial(n-1) | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), where n is the length of the string | ||
| # Space complexity: O(n), because there will be n layers to the call stack. | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
|
|
||
| if s.length == 0 | ||
| return s | ||
| else | ||
| return s[s.length-1] + reverse(s[0, s.length-1]) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), where n is the length of the string | ||
| # Space complexity: O(n), because there will be n layers to the call stack. | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
|
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. To do in-place you can create a helper method: Then each iteration you can swap the characters at |
||
|
|
||
| if s.length == 0 | ||
| return s | ||
| else | ||
| return s[s.length-1] + reverse(s[0, s.length-1]) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
|
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 can't predict the complexity? |
||
| # Space complexity: ? | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n % 2 == 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 should actually be: return 0 if n == 0
return 2 + bunny(n-1) |
||
| return 0 | ||
| else | ||
| return n + bunny(2-1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
|
|
@@ -46,4 +69,4 @@ def is_palindrome(s) | |
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| 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.
Technically s[0, s.length-1] creates a new string of length n-1, so this solution is actually n^2.