-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Kim #2
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 - Kim #2
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,98 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n is equal to the input value | ||
| # Space complexity: O(n) - where n is equal to the input value | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n < 0 | ||
| raise ArgumentError, "input must be a number greater than or equal to 0." | ||
| elsif n == 1 || n == 0 | ||
| return 1 | ||
| else | ||
| return n * factorial(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n is equal to the length of the input string | ||
| # not confident on this space complexity | ||
| # Space complexity: O(n ^ 2) - where n is equal to the length of the input string ^ 2 | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 0 || s.length == 1 | ||
| return s | ||
| else | ||
| return s[-1] + (reverse(s[0..-2])) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) - where n is the length of the string / 2 | ||
| # Space complexity: O(n) - where n is the length of the string / 2 | ||
| def reverse_inplace(s, i = 0, j = 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. 👍 Outstanding! |
||
| if i >= j | ||
| return s | ||
| else | ||
| temp = s[i] | ||
| s[i] = s[j] | ||
| s[j] = temp | ||
|
|
||
| return reverse_inplace(s, i += 1, j -= 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n is equal to the input value | ||
| # Space complexity: O(n) - where n is equal to the input value | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return n | ||
| else | ||
| return 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n equal the length of the string / 2 | ||
|
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. Because |
||
| # Space complexity: O(n) - where n equals the length of the string / 2 + the length of the string | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s == "" | ||
| return true | ||
| elsif s.length == 1 || s[0] != "(" || s[-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 |
||
| return false | ||
| else | ||
| return nested(s[1..-2]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n equals 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. Similar to the above O(n^2) |
||
| # Space complexity: O(n) - where n equals the length of the array * 2 | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if array.empty? | ||
| return false | ||
| elsif array[0] == value | ||
| return true | ||
| else | ||
| return search(array[1..-1], value) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n equals the length of the string / 2 | ||
| # Space complexity: O(n) - where n equals the length of the string / 2 + length of the string | ||
| 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. O(n^2) similar to the above, look at your reverse in place, could you use a similar technique? |
||
| raise NotImplementedError, "Method not implemented" | ||
| if s == "" || s.length == 1 | ||
| return true | ||
| elsif s[0] != s[-1] | ||
| return false | ||
| else | ||
| return is_palindrome(s[1..-2]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - where n equals the number of digits in the smallest input | ||
| # Space complexity: O(n) - where n equals the number of digits in the smallest input | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| if (n / 10 == 0) || (m / 10 == 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. These ifs could be simplified a bit. Otherwise very good! |
||
| return (n % 10 != m % 10) ? 0 : 1 | ||
| else | ||
| if (n % 10 == m % 10) | ||
| return 1 + digit_match(n / 10, m / 10) | ||
| else | ||
| return digit_match(n / 10, m / 10) | ||
| end | ||
| 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.
Because
s[0..-2]will also create a new array, this makes the time complexity also O(n^2)