-
Notifications
You must be signed in to change notification settings - Fork 53
Rachael Water #41
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?
Rachael Water #41
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,94 @@ | ||||||||
| # frozen_string_literal: true | ||||||||
|
|
||||||||
| # 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" | ||||||||
|
|
||||||||
| return 1 if n <= 1 | ||||||||
|
|
||||||||
|
|
||||||||
| n * factorial(n - 1) | ||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Space complexity: ? | ||||||||
| # Time complexity: O(n) | ||||||||
| # Space complexity: O(n) | ||||||||
| def reverse(s) | ||||||||
|
Comment on lines
+15
to
17
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. 👍 Given how you have the |
||||||||
| raise NotImplementedError, "Method not implemented" | ||||||||
| return s if s.length <= 1 | ||||||||
|
|
||||||||
| reversed_str = reverse(s[1..-1]) | ||||||||
| reversed_str += s[0] | ||||||||
| reversed_str | ||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Space complexity: ? | ||||||||
| # Time complexity: O(n) | ||||||||
| # Space complexity: O(1) | ||||||||
| def reverse_inplace(s) | ||||||||
|
Comment on lines
+25
to
27
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. Just noting this is incomplete. |
||||||||
| raise NotImplementedError, "Method not implemented" | ||||||||
|
|
||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Space complexity: ? | ||||||||
| # Time complexity: O(n) | ||||||||
| # Space complexity: O(n) | ||||||||
| def bunny(n) | ||||||||
|
Comment on lines
+31
to
33
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 doesn't work see below |
||||||||
| raise NotImplementedError, "Method not implemented" | ||||||||
| if n == 0 | ||||||||
| return 0 | ||||||||
| else | ||||||||
| 2 * bunny(n-1) | ||||||||
| return | ||||||||
|
Comment on lines
+37
to
+38
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.
Suggested change
|
||||||||
| end | ||||||||
|
|
||||||||
| return answer | ||||||||
|
|
||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Space complexity: ? | ||||||||
| def nested(s) | ||||||||
|
Comment on lines
45
to
47
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. Just noting this is incomplete. |
||||||||
| raise NotImplementedError, "Method not implemented" | ||||||||
| # if s == "" | ||||||||
| # return true | ||||||||
| # elsif s.length % 2 != 0 | ||||||||
| # return false | ||||||||
| # else | ||||||||
| # return nested(s) | ||||||||
| # end | ||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Time complexity: O(n) | ||||||||
| # Space complexity: ? | ||||||||
| def search(array, value) | ||||||||
|
Comment on lines
-33
to
59
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 = 0 | ||||||||
| length = array.length | ||||||||
| while array[n] != value | ||||||||
| n += 1 | ||||||||
| search(array, value) | ||||||||
| if n = length | ||||||||
| return false | ||||||||
| end | ||||||||
| end | ||||||||
| return true | ||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Space complexity: ? | ||||||||
| # Time complexity: O(n^2) | ||||||||
| # Space complexity: O(n^2) | ||||||||
| def is_palindrome(s) | ||||||||
|
Comment on lines
+72
to
74
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. 👍 , but can you see any way to adjust this to be O(n)? |
||||||||
| raise NotImplementedError, "Method not implemented" | ||||||||
| if s.length < 2 | ||||||||
| true | ||||||||
| elsif s[0] != s[-1] | ||||||||
| false | ||||||||
| else | ||||||||
| is_palindrome(s[1..-2]) | ||||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
| # Time complexity: ? | ||||||||
| # Space complexity: ? | ||||||||
| def digit_match(n, m) | ||||||||
| raise NotImplementedError, "Method not implemented" | ||||||||
| end | ||||||||
| def digit_match(n, m, num_matches = 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. Just noting this is incomplete. |
||||||||
| # n = n.to_s | ||||||||
| # m = m.to_s | ||||||||
| # if n < 10 || m < 10 | ||||||||
| # return num_matches | ||||||||
| # else | ||||||||
| # return digit_match(n/10, m/10, num_matches = 0) | ||||||||
| # 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.
👍