-
Notifications
You must be signed in to change notification settings - Fork 39
Sockets - Shubha #17
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?
Sockets - Shubha #17
Changes from all commits
1ea6465
4d1bb2d
6307657
b0f17b4
63a2d27
34df546
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,116 @@ | ||
| require "pry" | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n recursive calls are made | ||
| # Space complexity: O(n) where the stack is n levels deep | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise ArgumentError if n < 0 | ||
| if (n == 1) || (n == 0) | ||
| return 1 | ||
| else | ||
| return n * factorial(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n)^2 where n/2 recursive calls are made and each calls slice, which is O(n) | ||
| # Space complexity: O(n)^2 where the max depth of the function is n/2, and each level creates a new string slice | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length == 1 || s.length == 0 | ||
| return s | ||
| else | ||
| s[-1] + reverse(s[1...-1]) + s[0] | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n)^2 where n/2 recursive calls are made and each calls slice, which is 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. now that you're doing this with |
||
| # Space complexity: O(n) where the max depth of the function is n/2, | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| left = 0 | ||
| right = s.length - 1 | ||
| return swap(left, right, s) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def swap(left, right, str) | ||
| if right <= left | ||
| return str | ||
| else | ||
| temp = str[left] | ||
| str[left] = str[right] | ||
| str[right] = temp | ||
| swap(left + 1, right - 1, str) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: O(n) where n recursive calls are made | ||
| # Space complexity: O(n) where the max depth is n | ||
| def bunny(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n <= 0 | ||
| return 0 | ||
| else | ||
| return 2 + bunny(n - 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. 👍 |
||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n)^2 where n/2 recursive calls are made and each calls slice, which is O(n) | ||
| # Space complexity: O(n)^2 where the max depth of the function is n/2 | ||
|
|
||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.length == 0 | ||
| left = 0 | ||
| right = s.length - 1 | ||
| return check_parens(left, right, s) | ||
| end | ||
|
|
||
| def check_parens(left, right, str) | ||
| if str[left] == str[right] || left == right | ||
| return false | ||
| elsif left > right | ||
| return true | ||
| else | ||
| check_parens(left + 1, right - 1, str) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n)^2 because there are 2n-1 calls each time and each slices the array | ||
| # Space complexity: O(n log n) because at most the algorithm goes log2 n levels deep, and each layer creates a slice | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return false if array.length == 0 | ||
| if array.length == 1 | ||
| return true if array[0] == value | ||
| else | ||
| first_half = array[0...(array.length / 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. This looks like an attempt at MergeSort or something. Because you're making new arrays, this will be O(n^2), but I don't see what advantage you're getting by splitting it. This could be easier as: return false if array.length == 0
return true if value == array[0]
return search(array[1...-1])
Author
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. Wouldn't this save stack memory (space complexity) though? At most, the method will only go log2(n) levels deep at a time, as opposed to n levels if you just knocked off one number with each call. 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. It will save on some stack space, but won't save you in time complexity. |
||
| second_half = array[(array.length / 2)...array.length] | ||
| return true if search(first_half, value) || search(second_half, value) | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n)^2 where n/2 recursive calls are made and each calls slice, which is O(n) | ||
| # Space complexity: O(n)^2 where the max depth of the function is n/2 | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.length == 0 | ||
| left = 0 | ||
| right = s.length - 1 | ||
| return check_letters(left, right, s) | ||
| end | ||
|
|
||
| def check_letters(left, right, str) | ||
| if str[left] != str[right] | ||
| return false | ||
| elsif left >= right | ||
| return true | ||
| else | ||
| check_letters(left + 1, right - 1, str) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: If n < m, time complexity will be O(log10 n) since the number of calls is dependent on how many times the smaller number can be divided by 10 | ||
|
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: also O(log10 n) since the depth is proportional to the number of calls | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| match_value = (n % 10 == m % 10) ? 1 : 0 | ||
| if n / 10 == 0 || m / 10 == 0 | ||
| return match_value | ||
| else | ||
| return match_value + 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.
👍