python - Priority Queues: Enqueue returns 'None' instead of int value -


i having problems priorityqueue class in python. think set way , i'm trying test it, keep getting word "none" when print out value enqueuing isn't right.

this test code, following output , expected output:

from priorityqueue import priorityqueue  pq = priorityqueue()   print(pq.enqueue(1, 10)) print(pq.enqueue(2, 5)) print(pq.enqueue(3, 90)) print(pq) print(pq.size()) 

output:

none none none <priorityqueue.priorityqueue object @ 0x01ee5250> 2 

expected output:

10 5 90 90,5,10 2 

why print word none? have no idea why that. here priorityqueue class gets functions from.

#priorityqueue.py myheap import heap   class priorityqueue(object):      def __init__(self):         self.heap = heap()      def enqueue(self, priority, item):         '''post: item inserted specified priority in pq.'''         self.heap.insert((priority, item))      def first(self):         '''post: returns not remove highest priority item pq.'''         return self.heap.size()       def dequeue(self):         '''post: removes , returns highest priority item pq.'''         if self.heap.size() none:             raise valueerror("error queue empty.")         x = self.first()         self.heap.delete_max()         return x      def size(self):         '''post: returns number of items in pq.'''         return self.heap.size() 

all these functions, or of them take custom heap class this.

class heap(object):      def __init__(self, items=none):          '''post: heap created specified items.'''          self.heap = [none]         if items none:             self.heap_size = 0         else:             self.heap += items             self.heap_size = len(items)             self._build_heap()      def size(self):          '''post: returns number of items in heap.'''          return self.heap_size      def _heapify(self, position):          '''pre: items 0 position - 1 satisfy heap property.            post: heap property satisfied entire heap.'''          item = self.heap[position]         while position * 2 <= self.heap_size:             child = position * 2             # if right child, determine maximum of 2 children.             if (child != self.heap_size , self.heap[child+1] > self.heap[child]):                 child += 1             if self.heap[child] > item:                 self.heap[position] = self.heap[child]                 position = child             else:                 break         self.heap[position] = item      def delete_max(self):          '''pre: heap property satisfied            post: maximum element in heap removed , returned. '''          if self.heap_size > 0:             max_item = self.heap[1]             self.heap[1] = self.heap[self.heap_size]             self.heap_size -= 1             self.heap.pop()             if self.heap_size > 0:                 self._heapify(1)             return max_item      def insert(self, item):          '''pre: heap property satisfied.            post: item inserted in proper location in heap.'''          self.heap_size += 1         # extend length of list.         self.heap.append(none)         position = self.heap_size         parent = position // 2         while parent > 0 , self.heap[parent] < item:             # move item down.             self.heap[position] = self.heap[parent]             position = parent             parent = position // 2         # puts new item in correct spot.         self.heap[position] = item      def _build_heap(self):          ''' pre: self.heap has values in 1 self.heap_size            post: heap property satisfied entire heap. '''          # 1 through self.heap_size.          in range(self.heap_size // 2, 0, -1): # stops @ 1.             self._heapify(i)      def heapsort(self):          '''pre: heap property satisfied.            post: items sorted in self.heap[1:self.sorted_size].'''          sorted_size = self.heap_size          in range(0, sorted_size -1):             # since delete_max calls pop remove item, need append dummy value avoid illegal index.             self.heap.append(none)             item = self.delete_max()             self.heap[sorted_size - i] = item 

could tell me problem of why isn't working? thought supposed return value. know need iter priorityqueue class, don't know like. me?

your enqueue method doesn't have return statement, returns none.

if expecting python like, say, ruby or scheme, , return value of last expression evaluated--well, python isn't ruby; 1 of biggest differences that, unlike ruby, python statement-based language not expression, there is no "last expression", last statement, has no value.

meanwhile, returning none method mutates self considered pythonic thing (see list.append, example), wrote code correctly, if didn't intend to. :)

this means python doesn't lend method-chaining "fluent style" that's popular in other languages. deliberate design choice; although guido has never articulated why, consider single giant expression doesn't let see flow of control through vertical space , horizontal indentation way chain of statements does, discourages giving meaningful names meaningful intermediate values, harder debug tracebacks or traditional step debugger, etc.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -