mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
0
ebook_src/abstract_structures/heap/__init__.py
Normal file
0
ebook_src/abstract_structures/heap/__init__.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import heapq
|
||||
|
||||
def find_N_largest_items_seq(seq, N):
|
||||
''' find the N largest items in a sequence '''
|
||||
return heapq.nlargest(N,seq)
|
||||
|
||||
def find_N_smallest_items_seq(seq, N):
|
||||
''' find the N smallest items in a sequence '''
|
||||
return heapq.nsmallest(N, seq)
|
||||
|
||||
def find_smallest_items_seq_heap(seq):
|
||||
''' find the smallest items in a sequence using heapify first'''
|
||||
''' heap[0] is always the smallest item '''
|
||||
''' pops the smallest item, O(logN) '''
|
||||
heapq.heapify(seq)
|
||||
return heapq.heappop(seq)
|
||||
|
||||
def find_smallest_items_seq(seq):
|
||||
''' if it is only one item, min() is faster '''
|
||||
return min(seq)
|
||||
|
||||
def find_N_smallest_items_seq_sorted(seq, N):
|
||||
''' N ~ len(seq), better sort instead'''
|
||||
return sorted(seq)[:N]
|
||||
|
||||
def find_N_largest_items_seq_sorted(seq, N):
|
||||
''' N ~ len(seq), better sort instead'''
|
||||
return sorted(seq)[len(seq)-N:]
|
||||
|
||||
|
||||
def test_find_N_largest_smallest_items_seq(module_name='this module'):
|
||||
seq = [1, 3, 2, 8, 6, 10, 9]
|
||||
N = 3
|
||||
assert(find_N_largest_items_seq(seq, N) == [10, 9, 8])
|
||||
assert(find_N_largest_items_seq_sorted(seq, N) == [8, 9, 10])
|
||||
assert(find_N_smallest_items_seq(seq, N) == [1,2,3])
|
||||
assert(find_N_smallest_items_seq_sorted(seq, N) == [1,2,3])
|
||||
assert(find_smallest_items_seq(seq) == 1)
|
||||
assert(find_smallest_items_seq_heap(seq) == 1)
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_find_N_largest_smallest_items_seq()
|
||||
|
49
ebook_src/abstract_structures/heap/heapify.py
Normal file
49
ebook_src/abstract_structures/heap/heapify.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
class Heapify(object):
|
||||
def __init__(self, data=None):
|
||||
self.data = data or []
|
||||
for i in range(len(data)//2, -1, -1):
|
||||
self.__max_heapify__(i)
|
||||
|
||||
def __repr__(self):
|
||||
return '{}'.format(self.data)
|
||||
|
||||
def parent(self, i):
|
||||
return i >> 1
|
||||
|
||||
def left_child(self, i):
|
||||
return (i << 1) + 1
|
||||
|
||||
def right_child(self, i):
|
||||
return (i << 1) + 2 # +2 instead of +1 because it's 0-indexed.
|
||||
|
||||
def __max_heapify__(self, i):
|
||||
largest = i
|
||||
left = self.left_child(i)
|
||||
right = self.right_child(i)
|
||||
n = len(self.data)
|
||||
largest = (left < n and self.data[left] > self.data[i]) and left or i
|
||||
largest = (right < n and self.data[right] > self.data[largest]) and right or largest
|
||||
if i is not largest:
|
||||
self.data[i], self.data[largest] = self.data[largest], self.data[i]
|
||||
self.__max_heapify__(largest)
|
||||
|
||||
def extract_max(self):
|
||||
n = len(self.data)
|
||||
max_element = self.data[0]
|
||||
self.data[0] = self.data[n - 1]
|
||||
self.data = self.data[:n - 1]
|
||||
self.__max_heapify__(0)
|
||||
return max_element
|
||||
|
||||
|
||||
def test_Heapify():
|
||||
l1 = [3, 2, 5, 1, 7, 8, 2]
|
||||
h = Heapify(l1)
|
||||
assert(h.extract_max() == 8)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_Heapify()
|
27
ebook_src/abstract_structures/heap/merge_sorted_seqs.py
Normal file
27
ebook_src/abstract_structures/heap/merge_sorted_seqs.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import heapq
|
||||
|
||||
|
||||
def merge_sorted_seq(seq1, seq2):
|
||||
''' merge two sorted sequences with little ovehead. the result
|
||||
will be sorted, which is different of doing just +'''
|
||||
result = []
|
||||
for c in heapq.merge(seq1, seq2):
|
||||
result.append(c)
|
||||
return result
|
||||
|
||||
|
||||
def test_merge_sorted_seq(module_name='this module'):
|
||||
seq1 = [1, 2, 3, 8, 9, 10]
|
||||
seq2 = [2, 3, 4, 5, 6, 7, 9]
|
||||
seq3 = seq1 + seq2
|
||||
assert(merge_sorted_seqseq1, seq2) == sorted(seq3))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_merge_sorted_seq()
|
||||
|
40
ebook_src/abstract_structures/heap/priority_queue.py
Normal file
40
ebook_src/abstract_structures/heap/priority_queue.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import heapq
|
||||
|
||||
class PriorityQueue(object):
|
||||
''' implements a priority queue class '''
|
||||
def __init__(self):
|
||||
self._queue = []
|
||||
self._index = 0 # comparying same priority level
|
||||
|
||||
def push(self, item, priority):
|
||||
heapq.heappush(self._queue, (-priority, self._index, item))
|
||||
self._index += 1
|
||||
|
||||
def pop(self):
|
||||
return heapq.heappop(self._queue)[-1]
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
def __repr__(self):
|
||||
return "Item({!r})".format(self.name)
|
||||
|
||||
|
||||
def test_PriorityQueue():
|
||||
''' push and pop are all O(logN) '''
|
||||
q = PriorityQueue()
|
||||
q.push(Item('test1'), 1)
|
||||
q.push(Item('test2'), 4)
|
||||
q.push(Item('test3'), 3)
|
||||
assert(str(q.pop()) == "Item('test2')")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_PriorityQueue()
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue