-
Notifications
You must be signed in to change notification settings - Fork 47
Branches - Diana #47
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?
Branches - Diana #47
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,85 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| require 'pry' | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n < 0 | ||
| raise ArgumentError | ||
| end | ||
| return 1 if n == 0 | ||
| return n * factorial(n-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| # I think this is reversing the string in place? | ||
| def reverse(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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length <= 1 | ||
| return reverse(s[1..-1]) + s[0] | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length <= 1 | ||
| return reverse_inplace(s[1..-1]) + 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. This creates a new array, so it's not in place |
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(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" | ||
| return n if n == 0 | ||
| return 2 + bunny(n-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # NOT PASSING TESTS | ||
| def nested(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. Ok so not implemented |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.length == 0 | ||
| # need to check if s = even number of paren-pairs() | ||
| # return true if s.length % 2 == 0 | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def search(array, value, i = 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. 👍 |
||
| return false if i >= array.length | ||
| return true if array[i] == value | ||
| return search(array, value, i + 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| # ONLY PASSING 2 TESTS | ||
| def is_palindrome(s, l = 0, r = s.length-1 ) | ||
| if s[l] == s[r] | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
|
|
||
| while l < s.length | ||
|
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 a while if it's recursive. You can make the base case
|
||
| return is_palindrome(s, l + 1, r - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # NOT PASSING TESTS | ||
| def digit_match(n, m, i = -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. You can also pull off digits by % 10. |
||
| # i want to check each index from the end and keep track of matching values | ||
| n = n.to_s | ||
| m = m.to_s | ||
| tracker = 0 | ||
|
|
||
| if n[i] == m[i] | ||
| tracker += 1 | ||
| else | ||
| tracker += 0 | ||
| end | ||
|
|
||
| return tracker if i < -(n.length) | ||
|
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 i is less than n's length? |
||
|
|
||
| return digit_match(n, m, i - 1) | ||
| 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.
👍