-
Notifications
You must be signed in to change notification settings - Fork 46
Branches -- Paige D. #22
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,49 +1,73 @@ | ||
| # 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) if n > 1 | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse(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, but the time/space complexities are actually O(n2) because you are generating a new array with each recursive call. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length <= 1 | ||
| return s[-1] + reverse(s[1..-2]) + s[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.
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s, i = 0, j = 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. 👍 |
||
| if i >= j | ||
| return s | ||
| else | ||
| x = s[j] | ||
| y = s[i] | ||
| s[i] = x | ||
| s[j] = y | ||
| reverse_inplace(s, i += 1, j -= 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| 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" | ||
| return 0 if n == 0 | ||
| return 2 + bunny(n - 1) if n > 0 | ||
| 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, but you have similar time/space issues with the above methods. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return false if s.length.odd? | ||
| return true if s.length == 0 | ||
| return false if s[0] != "(" || s[-1] != ")" | ||
| return nested(s[1..-2]) if s.length > 0 | ||
| 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, i = 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. 👍 |
||
| return false if i == array.length | ||
| return true if array[i] == value | ||
| return search(array, value, i += 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def is_palindrome(s, start_index = 0, last_index = 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. 👍 |
||
| return false if s[start_index] != s[last_index] | ||
| return true if start_index > last_index | ||
| return is_palindrome(s, start_index += 1, last_index -= 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| # Time complexity: O(n) - where n is the number with less digits | ||
| # Space complexity: O(n) - where n is the number with less digits | ||
| def digit_match(n, m, match_count = 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. 👍 |
||
| n = n.to_s | ||
| m = m.to_s | ||
|
|
||
| return match_count if n[-1] == nil || m[-1] == nil | ||
| return digit_match(n[0..-2], m[0..-2], match_count += 1) if n[-1] == m[-1] | ||
| return digit_match(n[0..-2], m[0..-2], match_count) | ||
| 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.
👍