-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Sopheary #10
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 - Sopheary #10
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,91 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the value of the input | ||
| # Space complexity: O(1) because there's nothing storing the result | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return 1 if n == 0 | ||
| return 1 if n == 1 | ||
| raise ArgumentError if n < 0 | ||
| return n * factorial(n - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of s | ||
| # Space complexity: O(n) where n is the length of s | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length == 0 | ||
| last = s[-1] | ||
| s = s[0..-2] | ||
| reversed_string = last + reverse(s) | ||
| return reversed_string | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the string | ||
| # 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 will be O(n) since you end up using the system stack. |
||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length == 0 | ||
| return s if s.length == 1 | ||
|
|
||
| reverse_helper(s, 0, s.length - 1) | ||
| return s | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_helper(s, first_index, last_index) | ||
|
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 if first_index > last_index | ||
|
|
||
| temp = s[first_index] | ||
| s[first_index] = s[last_index] | ||
| s[last_index] = temp | ||
| reverse_helper(s, first_index + 1, last_index - 1) | ||
| end | ||
|
|
||
| # Time complexity: O(n) where n is the value of the input | ||
| # Space complexity: O(1) | ||
| def bunny(n) | ||
| 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) where n is the length of s | ||
| # Space complexity: O(1) | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s == "" | ||
| return false if !s.include?("(") | ||
|
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 isn't a good idea since it goes through the entire length of the string. That means your algorithm is O(n^2). Instead just check the 1st and last character. |
||
| return false if !s.include?(")") | ||
| s.sub!("(", "") | ||
| s.sub!(")", "") | ||
| return nested(s) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the array | ||
|
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: O(1) because only the variable last storing one element of the array | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return false if array.empty? | ||
| return true if array[0] == value | ||
| last = array.pop | ||
| return true if last == value | ||
| return search(array, value) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where is the length of s | ||
| # Space complexity: O(1) because there's no variable storing the string | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.empty? | ||
| if s[0] == s[-1] | ||
| s.sub!(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.
Instead think about tracking the left and right indexes adjusting them each recursive call. |
||
| if s.length >= 1 | ||
| s.sub!(s[-1], "") | ||
| end | ||
| is_palindrome(s) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the lenght of the smaller number | ||
| # Space complexity: O(1) because there's no variable storing the values of the numbers | ||
| 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 | ||
| return 0 if n == 0 || m == 0 | ||
| return 1 + digit_match(n / 10, m / 10) if n % 10 == m % 10 | ||
| return digit_match(n / 10, m / 10) | ||
| 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.
This creates a new string which increases the time and space time complexity.
So the time and space complexity is
O(n^2).