-
Notifications
You must be signed in to change notification settings - Fork 53
water - marj #45
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?
water - marj #45
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .idea/ | ||
| .rakeTasks |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,115 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: n | ||
| # Space complexity: n | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise ArgumentError if n < 0 | ||
| return 1 if n == 0 | ||
| return 1 if n == 1 | ||
| return n * factorial(n - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: n^2 | ||
| # Space complexity: n^2 | ||
| def reverse(s) #was the answer to this supposed to be different from the one below?? | ||
|
Comment on lines
+12
to
+14
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 s.length == 0 | ||
| return s | ||
| else | ||
| return reverse(s[1..-1]) +s[0] | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: n | ||
| # Space complexity: n | ||
| def reverse_inplace(s) | ||
|
Comment on lines
+22
to
24
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 the same as the previous method and it's not in place. Consider passing the front and rear indexes and swapping elements at each index. def reverse_inplace(s, front = 0, last = s.length - 1)
return s if front >= last
temp = s[last]
s[last] = s[front]
s[front] = temp
return reverse_inplace(s, front + 1, last - 1)
end |
||
| raise NotImplementedError, "Method not implemented" | ||
| #fix - add a few default arguments | ||
| #add optional arguments | ||
| if s.length == 0 | ||
| return s | ||
| else | ||
| return reverse(s[1..-1]) + s[0] | ||
| end | ||
|
|
||
| #Try Iterator to Recusion... | ||
| # reverse_index = -1 | ||
| # count = 0 | ||
| # length = s.length | ||
| # | ||
| # while count < length / 2 | ||
| # temp = s[count] | ||
| # s[count] = s[reverse_index] | ||
| # s[reverse_index] = temp | ||
| # | ||
| # count += 1 | ||
| # reverse_index -= 1 | ||
| # end | ||
| # return s | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| #*** | ||
| # Could not for the life of me | ||
| # figure out how to do this recursively based | ||
| # on the tests provided. | ||
| # Didn't even use a loop to convert...>_< | ||
| #*** | ||
| # Time complexity: n | ||
| # Space complexity: n | ||
| def bunny(n) | ||
|
Comment on lines
+55
to
57
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 works. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return 0 if n == 0 | ||
| if n > 0 | ||
| return bunny(n - 1) + 2 | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: n^2 | ||
| # Space complexity: n^2 | ||
| def nested(s) | ||
|
Comment on lines
+64
to
66
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 true if s.length == 0 | ||
| return false if s.length % 2 != 0 | ||
| if s[0] != s[-1] | ||
| return nested(s[1..-2]) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
|
Comment on lines
77
to
79
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 works with O(n^2) space/time complexity. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return false if array.length == 0 | ||
| if array[0] == value | ||
| return true | ||
| else | ||
| return search(array[1..-1], value) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: n | ||
| # Space complexity: n | ||
| def is_palindrome(s) | ||
|
Comment on lines
+88
to
90
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. 👍 But time/space complexity is O(n^2) |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.length == 0 | ||
| if s[0] == s[-1] | ||
| return is_palindrome(s[1..-2]) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: n | ||
| # Space complexity: n | ||
| # | ||
| # how many calls | ||
| # how expensive is each call | ||
| def digit_match(n, m) | ||
|
Comment on lines
+99
to
104
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 O(log n) if n is the size of the smaller of the two numbers (basically you divide by 10 each recursive call). |
||
| raise NotImplementedError, "Method not implemented" | ||
| if n < 10 || m < 10 | ||
| last_digit_m = m % 10 | ||
| last_digit_n = n % 10 | ||
| return last_digit_m == last_digit_n ? 1 : 0 | ||
| else | ||
| last_digit_m = m % 10 | ||
| last_digit_n = n % 10 | ||
| match = last_digit_m == last_digit_n ? 1 : 0 | ||
| return digit_match(n / 10, m / 10) + match | ||
| 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.
👍