string - Python: How to set up a __str__ function in a PriorityQueue -
i have made priority queue functions use binary heaps functions. however, in testing file trying print out queue this.
your queue looks this: 15 -> 45 -> 10 -> 100 somewhere that, keeps printing out queue stored rather items in queue, example of this:
<priorityqueue.priorityqueue object @ 0x01e95530> i read on pythondocs , concluded need str function. however, having trouble creating me out here on how like? lot. here whole amount of code.
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 above heap class priorityqueue takes from: below pq class , test file.
#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)) return 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() 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()) edit: tried following:
def __str__(self): return str(self.heap) this prints out this:
<myheap.heap object @ 0x01e255f0>
ok, idea of __str__ return sort of string represents object in human-readable way. have construct string , whatever return printed instead of
<priorityqueue.priorityqueue object @ 0x01e95530> in case, have return items of self.heap.heap, separated ->. work output described:
def __str__(self): if self.size(): heap_items = [str(i) in self.heap.heap if i] heap_items_str = ' -> '.join(heap_items) return "your queue looks this: {}".format(heap_items_str) else: return "your queue empty." note we're using self.heap.heap, not self.heap because self in case priorityqueue instance, , priorityqueue has .heap property contains heap. it's heap want call .heap on, in-turn gives list we're going for.
Comments
Post a Comment