-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Implemented PriorityQueue and Queue in utils.py #828
Conversation
Defined the abstract methods in `Queue` and implemented them in `ProrityQueue` and `FIFOQueue`.
I don't understand what the |
@MrDupin @norvig |
I have previously tried to make changes to the Queues, but each time I realized that it's not a big issue. Students don't come here for this, so we should just have something simple that works. I am not sure what to do with this, sorry. It can go any way. For now focus on the Queues and if Mr. Norvig likes the changes, work on the Stack. |
@@ -662,41 +663,39 @@ def __missing__(self, key): | |||
|
|||
|
|||
class hashabledict(dict): |
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.
I understand the motivation for a hashable dict. It is too bad Python doesn't have frozendict
like frozenset
.
But the implementation here is O(n) to compare or hash. Given that, a simpler representation is:
class hdict(dict):
def __hash__(self): return 1
@@ -724,73 +739,94 @@ def Stack(): | |||
|
|||
|
|||
class FIFOQueue(Queue): |
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.
The code here is fine, but I think we can just use collections.deque
.
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.
But then we have to change every uses of FIFOQueue
and replace its pop
with popleft
. I thought the idea behind these classes here was to be able to use PriorityQueue
and FIFOQueue
(and maybe Stack
) with a the same code and just change in the constructor.
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.
Yes, @goswami-rahul you are correct that the motivation of a single interface was to make the various queues the same, so that they could be interchangeable, and we could highlight the similarities. This was a priority in the 1st and 2nd edition of the book (and the code goes all the way back to then). But in the 3rd (and soon 4th) edition, we accept that each search function is somewhat different, and so it is ok, and maybe even preferably, to have popleft instead of pop.
|
||
def __contains__(self, item): | ||
return item in self.queue | ||
return item in self.q | ||
|
||
|
||
class PriorityQueue(Queue): |
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.
Probably easier to use heapq
.
So should I remove all these classes and refactor all their instances with |
new changes in #878 |
Queue
abstract class.PriorityQueue
methods. (on the TODO list)FIFOQueue
methods.PriorityQueue
to comply with new changes.The
__init__
method of thePriorityQueue
is changed.Old:
New:
This change is because it can be seen that the argument passed before i.e.
min
is a built-in function,There is no specific use of
min
function in any method. It is just used as a flag to indicate that make a min PriorityQueue.This functionality is achieved better when the
order
argument isstr
rather than afunction
.There are 2 uses of this class, both of which are also updated in this PR.
Also now the
PriorityQueue
attributeself.order
is not required.Any suggestions or feedbacks are appreciated.