Skip to content

Added lab 2 files, updated README.md.#3

Open
MKruchok wants to merge 8 commits into
mainfrom
lab_2
Open

Added lab 2 files, updated README.md.#3
MKruchok wants to merge 8 commits into
mainfrom
lab_2

Conversation

@MKruchok

Copy link
Copy Markdown
Owner

No description provided.

Comment thread priority_queue.py Outdated
def downwards(self, index):
next_index = index + 1
if next_index is len(self.heap):
def up(self, index, elem):

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread 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)]

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better implemented now.

Comment thread 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)]

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner Author

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.

@MKruchok MKruchok Sep 23, 2021

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better implemented now.

Comment thread 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

Copy link
Copy Markdown

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?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread priority_queue.py Outdated
else:
self.heap[int(index)] = elem

def down(self, index):

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants