-
Notifications
You must be signed in to change notification settings - Fork 0
Added lab 2 files, updated README.md. #3
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: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
Fixed.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
Better implemented now.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you wrap index with int? It's already int.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you write these operations separately? It's actually index = (index - 1) / 2 (you have used it already). So what this expression means?
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.
Fixed
priority_queue.py
Outdated
| else: | ||
| self.heap[int(index)] = elem | ||
|
|
||
| def down(self, index): |
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.
Again. Name your functions with a verb. In this case, you could name your down function as sink, bubble_down, etc.
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.
Fixed
No description provided.