-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Alex #6
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 - Alex #6
Changes from all commits
2cb9322
e3b1301
736dc89
eca06d5
e1a419e
5f3d5ac
99a3e7b
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,90 @@ | ||
| # 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 size of the number input | ||
| # Space complexity: O(n) where n is the size of the number input | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n < 0 | ||
| raise ArgumentError | ||
| elsif n == 0 || n == 1 | ||
| return 1 | ||
| else | ||
| return n * (factorial(n - 1)) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the string | ||
|
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) for time and space due to the fact that |
||
| # Space complexity: O(n) where n is the length of the string | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length <= 1 | ||
| return s | ||
| else | ||
| return rev_str = reverse(s[1..-1]) + s[0] | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| 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. 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" | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the number of bunnies | ||
| # Space complexity: O(n) where n is the number of bunnies | ||
| def bunny(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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 0 | ||
| elsif n == 1 | ||
| return 2 | ||
| else | ||
| return 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the string | ||
| # Space complexity: O(n) where n is the length of the string | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 1 | ||
| return false | ||
| elsif s.length == 0 | ||
| return true | ||
| # chris!! I realize this would fail if given an initial string like ")(" but i can't figure it out! | ||
|
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. Take a look at my notes for the reverse in place method and a similar solution can work here. |
||
| elsif (s[0] == "(" && s[-1] == ")") || (s[0] == ")" && s[-1] == "(") | ||
| return nested(s[1..-2]) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the array | ||
|
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) due to the fact that you create a new array with each recursive call. |
||
| # Space complexity: O(n) where n is the length of the array | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if array.length == 0 | ||
| return false | ||
| end | ||
|
|
||
| if array[0] == value | ||
| return true | ||
| end | ||
|
|
||
| return search(array[1..-1], value) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the size of the number | ||
|
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 time and space. |
||
| # Space complexity: O(n) where n is the size of the number | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 1 || s.length == 0 | ||
| return true | ||
| end | ||
|
|
||
| if s[0] != s[-1] | ||
| return false | ||
| end | ||
|
|
||
| return is_palindrome(s[1...-1]) | ||
| 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.
👍