-
Notifications
You must be signed in to change notification settings - Fork 47
Leaves - Morgan #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?
Leaves - Morgan #40
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,74 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) -- I think all of my methods might be this space/time complexity but not positive! | ||
| # Space complexity: O(n) | ||
| def factorial(num) | ||
| raise ArgumentError if num < 0 | ||
| return num * factorial(num - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: 0(n) | ||
| # Space complexity: O(n) | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length > 1 then | ||
| ((s[s.length - 1, 1] = s[(s.length - (s.length + 1)), 1]) + reverse_recursive(s.length - 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. You don't have a |
||
| else | ||
| s | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length <= 1 | ||
| reversed_str = reverse(s[1..-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. You're calling the It's also in no way in place. |
||
| reversed_str += s[0] | ||
| reversed_str | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return 0 if n < 1 | ||
| return 2 if n == 1 | ||
| return 2 + bunny(n-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(s) | ||
|
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 since you are creating a new array with They should be O(n2) |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.length == 0 | ||
| if s[0] == "(" && s[-1] == ")" | ||
| return nested(s[1..s.length-2]) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def search(array, value, index = 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. You're never calling this method with another index number, so you don't need this argument. Alternatively you don't need to create a new array and instead call the method with an increasing index. |
||
| return false if array.empty? | ||
| return true if array[index] == value | ||
| return search(array[1..-1], value) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.empty? || s.length == 1 | ||
| return false if s[0] != s[s.length -1] | ||
| return is_palindrome(s, 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def digit_match(n, m) | ||
|
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" | ||
| n = n.to_s | ||
| m = m.to_s | ||
| return 0 if n[-1] == nil || m[-1] == nil | ||
| if n[-1] == m[-1] | ||
| return 1 + digit_match(n[0..-2], m[0..-2]) | ||
| else | ||
| return digit_match(n[0..-2], m[0..-2]) | ||
| 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.
You're missing a base case here.