-
Notifications
You must be signed in to change notification settings - Fork 39
Ports Amy M #7
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?
Ports Amy M #7
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,87 @@ | ||
| # 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" | ||
| raise ArgumentError if n < 0 | ||
| return 1 if n == 1 || n == 0 | ||
| return n * factorial(n - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length <= 1 | ||
| reversed_str = reverse(s[1..-1]) | ||
| reversed_str += s[0] | ||
| reversed_str | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length < 1 | ||
| return (s[-1] + reverse_inplace(s[0...-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. 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 |
||
| 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 | ||
| elsif n == 1 | ||
| return 2 | ||
| end | ||
| return 2 + bunny(n - 1) | ||
| 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 actually O(n^2) |
||
| # Space complexity: O(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. Space complexity will be O(n^2) due to the system stack and a new string on each level of the stack. |
||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s == "" | ||
| return true | ||
| elsif s.length == 1 | ||
| return false | ||
| elsif s[0] == s[-1] | ||
| return false | ||
| else | ||
| return nested(s[1..-2]) | ||
| 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 also O(n^2) for both space and time complexity. |
||
| # Space complexity: O(1) | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if array.empty? | ||
| return false | ||
| elsif array[0] == value | ||
| return true | ||
| else | ||
| return search(array[1..-1], value) | ||
| 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. O(n^2) for both time and space complexity. You could do it with O(n) using a helper method and current left_index and right_index parameters. |
||
| # Space complexity: O(1) | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length < 2 | ||
| return true | ||
| elsif s[0] != s[-1] | ||
| return false | ||
| else | ||
| return is_palindrome(s[1..-2]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(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. Due to the system stack, the space complexity will be O(n) |
||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| if m == 0 || n == 0 | ||
| return 0 | ||
| end | ||
| if m % 10 == n % 10 | ||
| return 1 + digit_match(n / 10, m / 10) | ||
| else | ||
| return digit_match(n / 10, m / 10) | ||
| 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.
s[1..-1]creates a new string of length n-2, which means your time and space complexities are going to be O(n^2)