-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Kasey #14
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 - Kasey #14
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) | ||
| # Space complexity: O(n), err, does the stack take up a lot of space? theres n numbers of calls | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise ArgumentError, "cant be negative number" if 0 > n | ||
| if n == 0 | ||
| return 1 | ||
| else | ||
| return n * factorial(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: o(n) where n is the length of the string | ||
| # Space complexity: o(n) because it creates "temp" string to return | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s == "" | ||
| if s.length == 1 | ||
| return s | ||
| else | ||
| return temp = s[-1] + reverse(s[0...-1]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: constant, cause its in place, although, once again, it makes s.length stack calls, so does that mean its O(n) because it takes a place in the stack that many times? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s == "" | ||
| return s[-1] + reverse_inplace(s[0...-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. This is creating new strings, so it isn't in place. To do it in place you need to swap characters one at a time without creating a new string. |
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) (or O(n) cause it has that count to create and return?) | ||
|
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. Since you have a system stack, it's 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) (well, O(n/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. Due to creating a new string it's actually O(n^2) |
||
| # Space complexity: O(1) | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # maybe note to future self s[1...-1] is the same thing as s[1..-2] so count dots | ||
| if (s.length == 2 && s[0] == "(" && s[-1] == ")") || s.length == 0 | ||
| return true | ||
| elsif s[0] == "(" && s[-1] == ")" | ||
| return nested(s[1...-1]) | ||
| else | ||
| return false | ||
| 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) |
||
| # Space complexity: O(1) | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if array.length == 0 | ||
| return false | ||
| elsif array.length >= 1 && array[0] == value | ||
|
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. You don't need the |
||
| return true | ||
| else | ||
| return search(array[1...array.length], 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) |
||
| # Space complexity: O(1) | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if (s.length == 3 && s[0] == s[-1]) || (s.length == 2 && s[0] == s[-1]) || s.length == 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. This could probably be simplified to |
||
| return true | ||
| elsif s[0] == s[-1] | ||
| return is_palindrome(s[1...-1]) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # 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 space. |
||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| # if (n < 10 || m < 10) && n % 10 == m % 10 | ||
| # blerg don't need can go straight to | ||
| if (n == 0 || m == 0) | ||
| return 0 | ||
| elsif n % 10 == m % 10 | ||
| return 1 + digit_match(n / 10, m / 10) | ||
| else | ||
| return 0 + digit_match(n / 10, m / 10) | ||
| 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.
Since
s[0...s.length-1]creates a new string, this algorithm is actually O(n^2)