-
Notifications
You must be signed in to change notification settings - Fork 47
Leaves - Georgina #26
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,110 @@ | ||
| # 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" | ||
| if n < 0 | ||
| raise ArgumentError | ||
| end | ||
| return 1 if n == 1 || n == 0 | ||
| return n * factorial(n-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: 0(n) | ||
| # Space complexity: 0(n) | ||
| def reverse(s, i = 0, j = -1, n_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. 👍 |
||
| if i == s.length | ||
| else | ||
| n_s << s[j] | ||
| reverse(s, i + 1, j -1, n_s) | ||
| end | ||
| return n_s | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: 0(n) | ||
| # Space complexity: 0(n) | ||
| def reverse_inplace(s, i = 0, j = -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 == s.length/2 | ||
| else | ||
| temp = s[i] | ||
| s[i] = s[j] | ||
| s[j] = temp | ||
| reverse_inplace(s, i + 1, j - 1) | ||
| end | ||
| return s | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n, i = 0, j = 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. 👍 |
||
| if n == i | ||
| return j | ||
| else | ||
| return bunny(n, i + 1, j + 2) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(s, i = 0, ope = 0, close = 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 s.length % 2 != 0 | ||
| if i < s.length | ||
| if s[i] == "(" | ||
| ope += 1 | ||
| else | ||
| if i >= 1 | ||
| close += 1 | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| return nested(s, i + 1, ope, close) | ||
| end | ||
| return ope == close | ||
| 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 array.length == 0 | ||
| if array[i] == value | ||
| return true | ||
| end | ||
| if i < array.length | ||
| return search(array, value, i + 1) | ||
| else | ||
| return false | ||
| end | ||
| 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, i = 0, j = -1, coun = 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 true if s == "" | ||
| if i < s.length / 2 | ||
| if s[i] == s[j] | ||
| return is_palindrome(s, i + 1, j - 1, coun + 1) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| return coun % 2 == 0 | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def digit_match(n, m, i = 0, coun = 0) | ||
| if (n.class != Array) && (m.class != Array) | ||
| n = n.digits | ||
| m = m.digits | ||
| end | ||
| if i < n.length | ||
| if n[i] == m[i] | ||
| return digit_match(n, m, i + 1, coun + 1) | ||
| else | ||
| return digit_match(n, m, i + 1, coun) | ||
| end | ||
| else | ||
| return coun | ||
|
Comment on lines
+101
to
+108
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 assumes the two numbers start with the same number of digits. Maybe this would be better, if you started at the last digit. |
||
| 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.
👍