-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Faiza #11
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 - Faiza #11
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,27 +1,56 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - > the function will run as many times as the given number. | ||
| # Space complexity: O(1) -> we're not creating new variables but the method is taking up space in the stack until the base case equals true, then the stacks will popped of 1 by 1 starting from the top of the stack. | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # raise NotImplementedError, "Method not implemented" | ||
| if n < 0 | ||
| raise ArgumentError | ||
| elsif n == 1 || n == 0 | ||
| return 1 | ||
| end | ||
| return n * factorial(n - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def fib(n) | ||
| if n < 2 | ||
| return n | ||
| else | ||
| return fib(n - 1) + fib(n - 2) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: O(n), where n isnthe length of the input | ||
|
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(1) because additional space in memory is not being consumed | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # raise NotImplementedError, "Method not implemented" | ||
| if s.nil? || s.length == 0 || s.length == 1 | ||
| return s | ||
| else | ||
| return s[-1] + reverse(s[0...-1]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), where n isnthe length of the input | ||
| # Space complexity: O(1) because additional space in memory is not being consumed | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # raise NotImplementedError, "Method not implemented" | ||
| if s.nil? || s.length == 0 || s.length == 1 | ||
| return s | ||
| else | ||
| return s[-1] + reverse(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 not in place since you create a new string. Think about making a helper which takes the string, a left index and right index. The base case is when |
||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) because additional space in memory is not being consumed | ||
|
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) since you use the system stack for the recursion. |
||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 || n.nil? | ||
| return 0 | ||
| # if 50 is not equal to 100 (50 * 2) | ||
| else | ||
| return 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
|
|
@@ -36,14 +65,24 @@ def search(array, value) | |
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the size of the input | ||
| # Space complexity: O(1) because additional space in memory is not being consumed | ||
|
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) for both time and space since you use the system stack and create a new string with each iteration. |
||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # raise NotImplementedError, "Method not implemented" | ||
| if s.nil? || s.length == 0 || s.length == 1 | ||
| return true | ||
| else | ||
| if s[0] == s[-1] | ||
| is_palindrome(s[1..-2]) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| 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.
This works, but see my video on time and space complexity.