def heapsort(A): H = Heap() n = len(A) for i in range(n): H.insert(A[i]) k = n - 1 for i in range(H._csize): A[k] = H.deletemax() k = k - 1
class KruskalMST: def __init__(self, inNoOfVertices, iNoOfEdges): self.noOfVertices = inNoOfVertices self.tree = Heap(iNoOfEdges) def makeTree(self, inEdges): for edge in inEdges: edge = edge.split("\t") src = int(edge[0]) print edge[1] dest = int(edge[1]) weight = int(edge[2]) self.tree.insert(((src, dest), weight)) print self.tree def findParent(self, inParent, inNode): if inParent[inNode] == inNode: return inNode return self.findParent(inParent, inParent[inNode]) def union(self, inParent, inRank, inNode1, inNode2): parentOfNode1 = self.findParent(inParent, inNode1) parentOfNode2 = self.findParent(inParent, inNode2) if inRank[parentOfNode1] > inRank[parentOfNode2]: inParent[parentOfNode2] = parentOfNode1 elif inRank[parentOfNode2] > inRank[parentOfNode1]: inParent[parentOfNode1] = parentOfNode2 else: inParent[parentOfNode1] = parentOfNode2 inRank[parentOfNode2] += 1 def GetMST(self): mst = {} parent = {} rank = {} for node in range(self.noOfVertices): parent[node] = node rank[node] = 0 while len(mst.keys()) < self.noOfVertices - 1: edge, weight = self.tree.pop() node1 = edge[0] node2 = edge[1] parentOfNode1 = self.findParent(parent, node1) parentOfNode2 = self.findParent(parent, node2) if parentOfNode1 != parentOfNode2: mst[edge] = weight self.union(parent, rank, node1, node2) return mst
from Heaps import Heap import random data = [ 'Pomeranian', "Siberian Husky", "German Shepherd", "Golden Retriever", "Labrador Retriever" ] ret = [ 'Pomeranian', 'German Shepherd', 'Labrador Retriever', 'Siberian Husky', 'Golden Retriever' ] priority = [5, 2, 4, 1, 3] passed = True heap = Heap.Heap(len(data)) print("=================Testing add():=================") print("Adding Priority: ", priority) print("Adding Value: ", data) for i in range(len(data)): heap.add(priority[i], data[i]) print("=================Testing remove():=================") for i in range(len(data)): get = heap.remove() passed = passed and get == ret[i] print(get) print("Heap add() and remove() test status: ", "passed" if passed is True else "failed") test = []
from Heaps import Heap heap = Heap() heap.insert(12) heap.insert(-3) heap.insert(23) heap.insert(13) heap.insert(4) heap.heapsort() heap.heapsort()
import re # regex python standard library module string = 'It is a nice day in New Jersey. Time to go for a walk!' # create a list of chars sorted by frequency in descending order s = re.sub(r'[^a-zA-Z\s]+', '', string) print('Filter non-letter, non-whitespace chars:\n {}\n'.format(s)) s = s.lower() print('All lowercase:\n {}\n'.format(s)) s = re.sub(r'\s+', '', s) print('Remove whitespace:\n {}\n'.format(s)) unique_chars = set(s) chars = {} for c in unique_chars: occurances = re.findall(c, s) chars[c] = len(occurances) # create a sorted from Heaps import SimpleBinaryHeapTree as Heap heapCondition = lambda kv_1, kv_2: kv_1[1] > kv_2[1] heap = Heap(heapCondition) for kv in chars.items(): heap.add(kv) for i in range(len(heap)): print(heap.pop())
from Heaps import Heap heap = Heap() heap.add(10) heap.add(15) heap.add(8) heap.add(17) heap.add(20) heap.add(25) print(heap.items) print(heap.poll()) print(heap.items) print(heap.poll()) print(heap.items)
def __init__(self, inNoOfVertices, iNoOfEdges): self.noOfVertices = inNoOfVertices self.tree = Heap(iNoOfEdges)