-
Notifications
You must be signed in to change notification settings - Fork 46
Leaves - Alice #20
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 - Alice #20
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,96 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), the amount of recursive calls depends on the size of n. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| 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), the amount of recursive calls depends on the size of the string. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| def reverse(s) | ||
|
Comment on lines
+12
to
14
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 because you create a new array with each recursive call this is O(n2) for both time/space complexity. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length <= 1 | ||
|
|
||
| reversed_s = 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.
|
||
| reversed_s += s[0] | ||
| return reversed_s | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), the amount of recursive calls depends on the size of the string. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| def reverse_inplace(s, low = 0, high = 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. 👍 Awesome! |
||
| return s if high-low <= 1 | ||
|
|
||
| temp = s[low] | ||
| s[low] = s[high] | ||
| s[high] = temp | ||
|
|
||
| return reverse_inplace(s, low+1, high-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), the amount of recursive calls depends on the size of n. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| 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 if n == 1 | ||
|
|
||
| return 2 + bunny(n-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), the amount of recursive calls depends on the size of the string. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| 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. 👍 |
||
| 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(log n), the amount of searches is cut in half with every recursive call. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| def search(array, value, low = 0, high = array.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. 👍 The time/space complexity is O(log n) in both. This also assumes that the array is sorted. |
||
| mid = (high + low) / 2 | ||
| return true if array[mid] == value | ||
| return false if high < low | ||
|
|
||
| if array[mid] > value | ||
| return search(array, value, low, high = mid-1) | ||
| else | ||
| return search(array, value, low = mid + 1, high) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), the amount of recursive calls depends on the size of the string. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| def is_palindrome(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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.length == 1 || s.length == 0 | ||
|
|
||
| if s[0] == s[-1] | ||
| return is_palindrome(s[1..-2]) | ||
| else | ||
| return false | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), the amount of recursive calls depends on the size either of n or m. | ||
| # Space complexity: O(n), each recursive call is added to the stack. | ||
| 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" | ||
| end | ||
| 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.
👍