initial commit

This commit is contained in:
2025-10-07 18:22:35 +02:00
commit c19876ed78
34 changed files with 3866 additions and 0 deletions

View File

View File

@@ -0,0 +1,61 @@
import heapq
from functools import total_ordering
# this annotation saves us some implementation work
@total_ordering
class Item(object):
def __init__(self, insertion, priority, value):
self.insertion = insertion
self.priority = priority
self.value = value
def __lt__(self, other):
# if the decision "self < other" can be done
# based on the priority, do that
if self.priority < other.priority:
return True
elif self.priority == other.priority:
# in case the priorities are equal, we
# fall back on the insertion order,
# which establishes a total ordering
return self.insertion < other.insertion
return False
def __eq__(self, other):
return self.priority == other.priority and self.insertion == other.insertion
def __repr__(self):
return '({}, {}, {})'.format(self.priority, self.insertion, self.value)
class PriorityQueue(object):
def __init__(self):
self.insertion = 0
self.heap = []
def has_elements(self):
return len(self.heap) > 0
def put(self, priority, value):
heapq.heappush(self.heap, Item(self.insertion, priority, value))
self.insertion += 1
def get(self, include_priority=False):
item = heapq.heappop(self.heap)
if include_priority:
return item.priority, item.value
else:
return item.value
def __iter__(self):
return iter([item.value for item in self.heap])
def __str__(self):
return self.__repr__()
def __repr__(self):
return ('PriorityQueue [' + ','.join((str(item.value) for item in self.heap)) + ']')
def __len__(self):
return len(self.heap)

View File

@@ -0,0 +1,27 @@
from collections import deque
class Queue(object):
def __init__(self):
self.d = deque()
def put(self, v):
self.d.append(v)
def get(self):
return self.d.popleft()
def has_elements(self):
return len(self.d) > 0
def __iter__(self):
return iter(self.d)
def __str__(self):
return self.__repr__()
def __repr__(self):
return ('Queue [' + ','.join((str(item) for item in self.d)) + ']')
def __len__(self):
return len(self.d)

View File

@@ -0,0 +1,21 @@
from collections import deque
class Stack(object):
def __init__(self):
self.d = deque()
def put(self, v):
self.d.append(v)
def get(self):
return self.d.pop()
def has_elements(self):
return len(self.d) > 0
def __iter__(self):
return iter(self.d)
def __repr__(self):
return ('Stack [' + ','.join((str(item) for item in self.d)) + ']')