Conversation
anselrognlie
left a comment
There was a problem hiding this comment.
✨💫 Nice job on your implementation, Reid. I left some comments below.
You left the comprehension questions blank, and a few of the complexity questions are incomplete, so I'm grading this as a yellow for now. But please feel free to resubmit if you would like a green score!
🟡
| Time Complexity: O(log n) | ||
| Space Complexity: ? |
There was a problem hiding this comment.
👀 Your time complexity looks good, but what about the space complexity?
| Space Complexity: ? | ||
| """ | ||
| pass | ||
| if value == None: |
There was a problem hiding this comment.
👀 Prefer using is to compare to None
| """ | ||
| pass | ||
|
|
||
| if len(self.store) == 0: |
There was a problem hiding this comment.
👀 We have an empty helper we could use here
| Time Complexity: O(log n) | ||
| Space Complexity: ? |
There was a problem hiding this comment.
👀 Again, time complexity looks good, but what about the space complexity?
| Time complexity: ? | ||
| Space complexity: ? |
There was a problem hiding this comment.
👀 What are the time and space complexities for empty?
| Time complexity: ? | ||
| Space complexity: ? |
There was a problem hiding this comment.
👀 What are the time and space complexities for heap_up?
| if not self.store: | ||
| return True |
There was a problem hiding this comment.
👀 An empty list is falsy, but what would happens if the list were not empty?
Python would fall off the end of the function, with the default None return value. But since we're returning a boolean from one path, we should be consistent everywhere.
if not self.store:
return True
else:
return Falsewhich simplifies to
return not self.store|
|
||
|
|
||
|
|
||
| def heap_down(self, index): |
There was a problem hiding this comment.
✨ Nice approach of first determining the candidate child, then deciding whether the swap is required.
Though not prompted, consider what the time and space complexity is for heap_down. Does it differ from heap_up at all? What impact does the recursive call have on the complexity? Could this be improved?
| Time Complexity: ? | ||
| Space Complexity: ? |
There was a problem hiding this comment.
👀 What are the time and space complexities for heap_sort?
Your approach looks good. To heap sort, we build a heap from the data to be sorted, then we remove the values from the heap, which gives them back to us in order. What does this mean for the time and space complexity?
| while not heap.empty(): | ||
| nums[index] = heap.remove() | ||
| index += 1 |
There was a problem hiding this comment.
Note the since this isn't a fully in-place solution (the MinHeap has a O(n) internal store), we don't necessarily need to modify the passed in list. The tests are written to check the return value, so we could unpack the heap into a new result list to avoid mutating the input.
result = []
while not heap.empty():
result.append(heap.remove())
return result
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?