-
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
Conversation
CheezItMan
left a comment
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.
You did well and they all work, but you made some mistakes on the big-O due to the fact that you misunderstood what was happening with some of the built-in functions.
You are also doing a very odd search where you split the array, but you aren't doing a binary search, you search both halves. Take a look at my comment on that one.
lib/recursive-methods.rb
Outdated
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n/2 recursive calls are made |
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.
Not quite, as s[1..s.length-1] makes a new string with n-2 characters, and since you do this every recursive call... you end up with O(n * n) = O(n^2).
There is another way by using a helper method and keeping track of the current indexes which is O(n).
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.
Gotcha. For some reason I thought accessing a slice of an array/string would access part of what was already stored in memory.
lib/recursive-methods.rb
Outdated
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n/2 recursive calls are made | ||
| # Space complexity: O(n) where the max depth of the function is n/2 |
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.
Similarly each recursive call makes a new array so O(n^2).
|
|
||
| # 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 comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n recursive calls are made |
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.
👍
lib/recursive-methods.rb
Outdated
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n recursive calls are made |
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.
Not quite since s[0..-1] makes a new string, it's O(n^2)
It's similar for Space complexity.
lib/recursive-methods.rb
Outdated
| if s.length == 1 || s.length == 0 | ||
| return s | ||
| else | ||
| s[-1] + reverse_inplace(s[1...-1]) + s[0] |
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.
Unfortunately this isn't in place. It makes a new string.
| if n <= 0 | ||
| return 0 | ||
| else | ||
| return 2 + bunny(n - 1) |
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.
👍
lib/recursive-methods.rb
Outdated
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n/2 recursive calls are made |
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.
Similar to the above.
| 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 comment
The 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])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.
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 comment
The 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.
CheezItMan
left a comment
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.
Much better reverse_in_place method.
|
|
||
| # 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 comment
The reason will be displayed to describe this comment to others. Learn more.
now that you're doing this with swap, this method is O(n), well done!
lib/recursive-methods.rb
Outdated
| if s.length == 1 || s.length == 0 | ||
| return s | ||
| else | ||
| s[-1] + reverse_inplace(s[1...-1]) + s[0] |
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.
reverse uses reverse_in_place?
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.
Whoops. fixed!
No description provided.