-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Ngoc #3
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?
Ports - Ngoc #3
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| def square_of_two(n) | ||
| if n == 0 | ||
| return 1 | ||
| end | ||
| temp = square_of_two(n / 2) | ||
| if n % 2 == 0 | ||
| return temp * temp | ||
| elsif n % 2 == 1 | ||
| return 2 * temp * temp | ||
| end | ||
| end | ||
|
|
||
| p square_of_two(2) | ||
| p square_of_two(4) | ||
| p square_of_two(5) | ||
| p square_of_two(0) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,108 @@ | ||
| # 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) | ||
|
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" | ||
| if n < 0 | ||
| raise ArgumentError | ||
| end | ||
| if n == 0 | ||
| return 1 | ||
| else | ||
| return n * factorial(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(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. This is actually O(n^2) because |
||
| # Space complexity: O(n) | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.empty? | ||
| return "" | ||
| end | ||
| if s.length == 1 | ||
| return s[0] | ||
| else | ||
| return s[s.length - 1] + reverse(s[0..s.length - 2]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: O(n) | ||
| # Space complexity: O(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. Space complexity is O(n) due to the system stack. |
||
| def reverse!(s, b, e) | ||
|
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. Great work! 👍 |
||
| if b >= e | ||
| return | ||
| else | ||
| temp = s[b] | ||
| s[b] = s[e] | ||
| s[e] = temp | ||
| reverse!(s, b + 1, e - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.empty? | ||
| return "" | ||
| end | ||
| if s.length == 1 | ||
| return s[0] | ||
| end | ||
| b = 0 | ||
| e = s.length - 1 | ||
| reverse!(s, b, e) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 0 | ||
| else | ||
| return 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(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. This one is O(n^2) for the same reasons as reverse, for both space & time |
||
| # Space complexity: O(n) | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 0 | ||
| return true | ||
| elsif s.length == 1 | ||
| return false | ||
| end | ||
| return s[0] == "(" && s[s.length - 1] == ")" && nested(s[1..s.length - 2]) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(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. O(n^2) |
||
| # Space complexity: O(n) | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if array.empty? | ||
| return false | ||
| end | ||
| if array[0] == value | ||
| return true | ||
| else | ||
| return search(array[1..array.length - 1], value) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(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. O(n^2), think about how you solved reverse in place, could you use that technique to make this more efficient? |
||
| # Space complexity: O(n) | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length <= 1 | ||
| return true | ||
| end | ||
| return s[0] == s[s.length - 1] && is_palindrome(s[1..s.length - 2]) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
|
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. O(n) for both space and time |
||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| if m == 0 || n == 0 | ||
| return 0 | ||
| end | ||
| if m % 10 == n % 10 | ||
| return digit_match(n / 10, m / 10) + 1 | ||
| else | ||
| return digit_match(n / 10, m / 10) + 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.
huh?