Conversation
priority_queue.py
Outdated
| def downwards(self, index): | ||
| next_index = index + 1 | ||
| if next_index is len(self.heap): | ||
| def up(self, index, elem): |
There was a problem hiding this comment.
Try to name your functions with a verb (because the function does some action). In this case, you could call your up function as swim, bubble_up, etc. Avoid such namings as up / upwards etc.
priority_queue.py
Outdated
| if next_index is len(self.heap): | ||
| def up(self, index, elem): | ||
| while index > 0 and elem.priority > self.heap[int((index - 1) / 2)].priority: | ||
| self.heap[int(index)] = self.heap[int((index - 1) / 2)] |
There was a problem hiding this comment.
What is int((index - 1) / 2 in your function? You've already used it twice here, so consider saving this expression to a variable with readable naming that explains the purpose of this expression.
priority_queue.py
Outdated
| if next_index is len(self.heap): | ||
| def up(self, index, elem): | ||
| while index > 0 and elem.priority > self.heap[int((index - 1) / 2)].priority: | ||
| self.heap[int(index)] = self.heap[int((index - 1) / 2)] |
There was a problem hiding this comment.
Why do you wrap index with int? It's already int.
There was a problem hiding this comment.
Sometimes indexes is float. For example: when index is 2, 2 - 1 is 1, 1 / 2 is 0,5. Indexes must be int, not float. int() function returns a number rounded + converts it to int.
There was a problem hiding this comment.
Better implemented now.
priority_queue.py
Outdated
| while index > 0 and elem.priority > self.heap[int((index - 1) / 2)].priority: | ||
| self.heap[int(index)] = self.heap[int((index - 1) / 2)] | ||
| index -= 1 | ||
| index /= 2 |
There was a problem hiding this comment.
Why did you write these operations separately? It's actually index = (index - 1) / 2 (you have used it already). So what this expression means?
priority_queue.py
Outdated
| else: | ||
| self.heap[int(index)] = elem | ||
|
|
||
| def down(self, index): |
There was a problem hiding this comment.
Again. Name your functions with a verb. In this case, you could name your down function as sink, bubble_down, etc.
No description provided.