def sort_k_sorted(arr, k): ans = [] h = MinHeap() for i in range(k + 1): h.insert(arr[i]) for i in range(k + 1, len(arr)): smallest = h.extract() ans.append(smallest) h.insert(arr[i]) while not h.is_empty(): smallest = h.extract() ans.append(smallest) return ans
def k_smallest(arr, k): h = MinHeap() for item in arr: h.insert(item) for i in range(k): smallest = h.extract() print(smallest) return
def single_item_test(): h = MinHeap() h.insert(1) assert h.heap_data[0] == 1 assert_size(h, 1) elem = h.extract() assert elem == 1 assert_size(h, 0)
def incomplete_tree_test(): h = MinHeap() h.insert(2) h.insert(1) assert h.heap_data[0] == 1 assert h.heap_data[1] == 2 assert_size(h, 2) elems = [] elems.append(h.extract()) elems.append(h.extract()) assert elems == [1, 2] assert_size(h, 0)
def several_elements_test(): h = MinHeap() h.insert(5) h.insert(3) h.insert(4) h.insert(122) h.insert(100) h.insert(2) h.insert(8) h.insert(18) assert_size(h, 8) elems = [] for i in range(8): elems.append(h.extract()) assert elems == [2, 3, 4, 5, 8, 18, 100, 122] assert_size(h, 0)
from heap import MinHeap, MaxHeap from math import floor import sys #invariant: maintain min heap size = floor(n/2), median is the the max in max heap with open('Median.txt') as f: #with open('median10_test.txt') as f: a = [int(l) for l in f] minHeap = MinHeap([]) maxHeap = MaxHeap([]) medians = [] for i in range(len(a)): if minHeap.size() == 0 and maxHeap.size() == 0: maxHeap.insert(a[i]) else: if a[i] < maxHeap.top(): maxHeap.insert(a[i]) else: minHeap.insert(a[i]) if minHeap.size() > floor((i+1)/2): maxHeap.insert(minHeap.extract()) elif minHeap.size() < floor((i+1)/2): minHeap.insert(maxHeap.extract()) medians.append(maxHeap.top()) print(sum(medians)%10000)
class PathFinderNormal: def __init__(self, maze_file): self.close = dict() self.open = MinHeap() self.open_dict = dict() self.grid = [] self.start_state = None self.goal_state = None for i in range(max_size): self.grid.append([]) with open(maze_file, 'r') as f: line_row = 0 for line in f: self.grid.append([]) line = line.rstrip() if not line: continue if not self.start_state: self.start_state = line_to_coord(line) self.start_state.g = 0 continue if not self.goal_state: self.goal_state = line_to_coord(line) continue for pos in line: self.grid[line_row].append(True if pos == '1' else False) line_row += 1 def find_path(self): return self.find_path_internal(self.start_state, self.goal_state, []) def find_path_internal(self, start, goal, path): path.append(start) if start == goal: return path self.close[start] = True neighbors = self.compute_valid_neighbors(start) if neighbors: for n in neighbors: n.g = start.g + 1 if self.grid[n.y][n.x]: continue n.distance = calculate_manhattan_distance(n, goal) self.open.insert(n) self.open_dict[n] = True nextC = self.open.extract() if not nextC: return [] return self.find_path_internal(nextC, goal, path) def compute_valid_neighbors(self, start): neighbors = [] # Find all neighbors. if start.x - 1 >= 0: n = Coord(start.x - 1, start.y) if not self.close.has_key(n): neighbors.append(n) if start.y - 1 >= 0: n = Coord(start.x, start.y - 1) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) if start.y + 1 < max_size: n = Coord(start.x, start.y + 1) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) if start.x + 1 < max_size: n = Coord(start.x + 1, start.y) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) return neighbors
class PathFinderTieBreak: def __init__(self, maze_file): self.close = dict() self.open = MinHeap() self.open_dict = dict() self.grid = [] self.start_state = None self.goal_state = None self.known_distances = dict() for i in range(max_size): self.grid.append([]) with open(maze_file, 'r') as f: line_row = 0 for line in f: self.grid.append([]) line = line.rstrip() if not line: continue if not self.start_state: self.start_state = line_to_high_coord(line) self.start_state.g = 0 continue if not self.goal_state: self.goal_state = line_to_high_coord(line) continue for pos in line: self.grid[line_row].append(True if pos == '1' else False) line_row += 1 def find_new_start(self): yind = 0 for y in self.grid: xind = 0 for x in y: if not self.grid[yind][xind] and (self.start_state.x != xind or self.start_state.y != yind): self.start_state = line_to_high_coord( str(xind) + "," + str(yind)) self.start_state.g = 0 print "New start is " + str(self.start_state) return xind += 1 yind += 1 def find_path(self): self.open_dict.clear() self.close.clear() self.open = MinHeap() return self.find_path_internal(self.start_state, self.goal_state, [], []) def find_path_internal(self, start, goal, path, final_path): path.append(start) if start == goal: final_path.append(start) # Fill in h values with what we learned. i = len(final_path) for c in final_path: self.known_distances[c] = i i -= 1 return (path, final_path) self.close[start] = True neighbors = self.compute_valid_neighbors(start) if neighbors: for n in neighbors: n.g = start.g + 1 if self.grid[n.y][n.x]: continue if n in self.known_distances: n.distance = self.known_distances[n] else: n.distance = calculate_manhattan_distance(n, goal) self.open.insert(n) self.open_dict[n] = True nextC = self.open.extract() if not nextC: return ([], []) if calculate_manhattan_distance(start, nextC) == 1: final_path.append(start) path, new_final_path = self.find_path_internal(nextC, goal, path, final_path) return (path, final_path) def compute_valid_neighbors(self, start): neighbors = [] # Find all neighbors. if start.x - 1 >= 0: n = HighCoord(start.x - 1, start.y) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) if start.y - 1 >= 0: n = HighCoord(start.x, start.y - 1) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) if start.y + 1 < max_size: n = HighCoord(start.x, start.y + 1) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) if start.x + 1 < max_size: n = HighCoord(start.x + 1, start.y) if not self.close.has_key(n) and not self.open_dict.has_key(n): neighbors.append(n) return neighbors