There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
The name min is suggested to be used as a variable number in this exercise even though there is a built-in function in python that uses the same name, I do not think this is the best option as that overwrites the function as far as I know.
Why self.heap_list[1] = self.heap_list.pop() is not a valid option to swap the element and remove it in the same time ?
I had to use the solution option because none of mine was accepted. Shouldn’t they just try if the code is valid instead of checking every details of the syntax…
Hi, I’m looking at the solution to this exercise and there’s one thing I don’t understand. In part 3 we are asked to:
“…swap the element at index 1 with the last element in the internal list.”
The code for this is given as:
self.heap_list[1] = self.heap_list[self.count]
which I understand makes the value of the element at index 1 the same as the value of the element at the last index. What I can’t see is how this (or any other code given) makes the reverse true, i.e. to make the value of the element at the last index the same as the value of the element at index 1. Obviously, the code works but I don’t understand how.
It’s too late, I know, but I want to try answering anyway…
I like this trick. It’s not swaping literally, it’s “making the value of the element at index 1 the same as the value of the element at the last index” namely as you understand it. And the last element we are going to delete, so we don’t care what value it has.
Please correct me, somebody, if I’m mistaken.
So I was doing this exercise, and I came up with this code:
def retrieve_min(self):
if len(self.heap_list) == 1:
print("No items in heap")
return None
min = self.heap_list[1]
print("Removing {min} from {self.heap_list}".format(min=min, self=self))
self.heap_list[1] = self.heap_list[-1]
del self.heap_list[-1]
print("Last element moved to first: {self.heap_list}\n".format(self=self))
return min
and I was wondering, if we are only working with the first index and the last from the list, do we really need to keep track of index with “self.count” instead of just using self.heap_list[-1] to get the last index of the list?
Cause as far as I’m in this lesson, my code seems to work just fine.
Edit:
I thought about leaving the solution code so anyone can compare it =)
def retrieve_min(self):
if self.count == 0:
print("No items in heap")
return None
min = self.heap_list[1]
print("Removing: {0} from {1}".format(min, self.heap_list))
self.heap_list[1] = self.heap_list[self.count]
self.heap_list.pop()
self.count -= 1
print("Last element moved to first: {0}".format(self.heap_list))
return min
Why does this swap the 2nd element with the end element? self.heap_list[1] = self.heap_list[-1]
I thought this only sets element 2 as the last element and leaves the last element the same(not element 1). This is confusing(someone, @tgrtim).
like others have said…
instructions say “Then, swap the element at index 1 with the last element in the internal list.”
but no solution works here to achieve this. you have you look at the accepted solution to see that they are not even following the instructions.
i think the correct instructions would be, based on the accepted solution, “Set the element at index 1 with the last element in the internal list, then remove the last element”