add Dijkstra's and OrderedStack (#270) · wxpython/algorithms@d890b34 · GitHub
Skip to content

Commit d890b34

Browse files
agranya99goswami-rahul
authored andcommitted
add Dijkstra's and OrderedStack (keon#270)
* Dijkstra's single source shortest path algorithm * Update dij.py provided test case * Create orderedStack.py Stack that is always sorted in order highest to lowest. Can be applied in e-mail/messages application to display latest messages at the top. * Update and rename dij.py to dijkstra.py * Update orderedStack.py Made suggested changes * Update orderedStack.py * Modify the orderedStack and add unittest * Update dijkstra.py * Update dijkstra.py * Update dijkstra.py * Update dijkstra.py * Update dijkstra.py * Update dijkstra.py corrected indentation * Update ordered_stack.py Forgot to remove return statement from push function. I needed it in one of my programs. Removed. Removed redundant brackets * Update test_stack.py * Update test_stack.py
1 parent 5898ae0 commit d890b34

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

graph/dijkstra.py

Lines changed: 36 additions & 0 deletions

stack/ordered_stack.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#The stack remains always ordered such that the highest value is at the top and the lowest at the bottom
2+
3+
class OrderedStack:
4+
def __init__(self):
5+
self.items = []
6+
7+
def is_empty(self):
8+
return self.items == []
9+
10+
def push_t(self, item):
11+
self.items.append(item)
12+
13+
def push(self, item): #push method to maintain order when pushing new elements
14+
temp_stack = OrderedStack()
15+
if self.is_empty() or item > self.peek():
16+
self.push_t(item)
17+
else:
18+
while item < self.peek() and not self.is_empty():
19+
temp_stack.push_t(self.pop())
20+
self.push_t(item)
21+
while not temp_stack.is_empty():
22+
self.push_t(temp_stack.pop())
23+
24+
def pop(self):
25+
if self.is_empty():
26+
raise IndexError("Stack is empty")
27+
return self.items.pop()
28+
29+
def peek(self):
30+
return self.items[len(self.items) - 1]
31+
32+
def size(self):
33+
return len(self.items)

tests/test_stack.py

Lines changed: 15 additions & 0 deletions

0 commit comments

Comments
 (0)