Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[run]
source =
src/weighted_graph.py
src/weighted_traversal.py

omit =
src/priorityq.py
src/deque.py
src/que_.py
src/stack.py
src/binheap.py
src/dijkstra.py
src/graph_1.py
src/graph_2.py
src/dll.py
src/linked_list.py
src/reverse_linked_list.py
src/test_dll.py
src/test_binheap.py
src/test_deque.py
src/test_dijkstra.py
src/test_graph.py
src/test_graph_2.py
src/test_linked_list.py
src/test_priority_que.py
src/test_que_.py
src/test_reverse_linked_list.py
src/test_stack.py
src/test_weighted_graph.py
src/test_weighted_traversal.py

[report]
show_missing = True
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Data Structures for Python 401
#Used the shift and pop methods from our double linked list in dll.py.


##Deque Branch
#Implement a dequeue data structure that has pop, popleft, append, appendleft,
#peek, peekleft, and size methods. Implemented using a composition of double linked
#list.

##Binary Heap Branch
#Implement a pop() and push() function for this exercise.
#Worked on initialization and implementation of our the data structure itself.
Expand Down
10 changes: 2 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


extra_packages = {
'testing': ['pytest', 'pytest-watch', 'pytest-cov', 'tox']
'testing': ['pytest-cov', 'tox']
}


Expand All @@ -14,12 +14,6 @@
author="Kurt Maurer" "Anna Shelby",
author_email="bonanashelby@gmail.com",
license="MIT",
py_modules=["server, client"],
package_dir={'': 'src'},
install_requires=["ipython", "pytest"],
extras_require=extra_packages,
entry_points={
'console_scripts': [
]
}
extras_require=extra_packages
)
12 changes: 6 additions & 6 deletions src/deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def popleft(self):
"""Shift for popleft."""
return self._new_dll.shift()

def peek(self): # pragma no cover
def peek(self):
"""Return a new value without dequeuing it."""
print(self._new_dll.head.data)
return self._new_dll.head.data

def peekleft(self): # pragma no cover
def peekleft(self):
"""Return a new value from the left without dequeing it."""
print(self._new_dll.tail.data)
return self._new_dll.tail.data

def __len__(self):
def size(self):
"""Length function for the queue."""
return len(self._new_dll)
return self._new_dll.size()
27 changes: 27 additions & 0 deletions src/dijkstra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Dijkstra algorithm for our shortest path graph."""
from weighted_graph import Weighted
from priorityq import PriorityQueue

# {'A':{'B': 7, 'C': 9}, 'B': {'D': 2, 'E': 4}, 'C': {'F': 6}, 'D':{}, 'E': {}, 'F': {}}


def dijkstra(weighted_graph, start_node):
"""Find the shortest path to nodes from starting node."""
if not weighted_graph.has_node(start_node):
raise IndexError('Node not in this weighted graph.')
current_node = {start_node: 0}
visited = {}
priorityq = PriorityQueue()
priorityq.insert(current_node, 0)
while len(priorityq) > 0:
current_node = priorityq.pop()
next_nodes = weighted_graph[list(current_node.keys())[0]]
for key, value in next_nodes.items():
distance_from_start_node = value + list(current_node.values())[0]
priorityq.insert(
{key: distance_from_start_node}, distance_from_start_node
)
if key in visited and distance_from_start_node > visited[key]:
continue
visited.update({key: distance_from_start_node})
return visited
33 changes: 12 additions & 21 deletions src/dll.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,27 @@ def push(self, val):
"""Push a value to the head of the list."""
if not val:
raise TypeError('Please provide a not null value.')
self._length += 1
if self.tail is None and self.head is None:
new_node = Node(val)
self.tail = new_node
self.head = new_node
self.tail = self.head = new_node
else:
new_node = Node(val, None, self.head)
self.head.next_node = new_node
self.head = new_node
self._length += 1

def append(self, val):
"""Append a val to the tail of a list."""
if not val:
raise TypeError('Please provide a not null value.')
self._length += 1
if self.tail is None and self.head is None:
new_node = Node(val)
self.tail = new_node
self.head = new_node
self.tail = self.head = new_node
else:
new_node = Node(val, self.tail, None)
self.tail.prior_node = new_node
self.tail = new_node
self._length += 1

def pop(self):
"""Pop pops from the head of the list."""
Expand All @@ -56,55 +54,48 @@ def pop(self):
self._length -= 1
if self.head == self.tail:
last_pop = self.head
self.head = None
self.tail = None
self.tail = self.head = None
return last_pop.data
popped = self.head
self.head = self.head.prior_node
popped.prior_node = None
self.head.next_node = None
return popped.data

def shift(self):
"""Remove the node from the tail of the list."""
if not self.head:
if not self.tail:
raise IndexError(
'There\'s nothing to remove from the linked list.')
self._length -= 1
if self.head == self.tail:
last_pop = self.head
self.head = None
self.tail = None
self.tail = self.head = None
return last_pop.data
shifted = self.tail
self.tail = self.tail.next_node
shifted.next_node = None
self.tail.prior_node = None
return shifted.data

def __len__(self):
def size(self):
"""Return the length of the double linked list."""
return self._length

def remove(self, val):
"""Remove a node with the value provided."""
if val is None:
raise TypeError(
'That value is not in this particular linked list.')
if self.head.data == val:
self.pop()
return
elif self.tail.data == val:
self.shift()
return
current_node = self.head
while current_node is not None:
if current_node.data != val:
current_node = current_node.prior_node
else:
new_next_node = current_node.next_node
new_prior_node = current_node.prior_node
current_node.next_node.prior_node = new_prior_node
current_node.prior_node.next_node = new_next_node
current_node.next_node = None
current_node.prior_node = None
new_next_node.prior_node = new_prior_node
new_prior_node.next_node = new_next_node
self._length -= 1
break
28 changes: 20 additions & 8 deletions src/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ def pop(self):
self._length -= 1
return popped.data

def __len__(self):
def size(self):
"""Return the length of the linked list."""
return self._length

def __len__(self):
"""Interact with built-in len() function."""
return self.size()

def search(self, val):
"""Search the nodes for the value provided."""
current_node = self.head
Expand All @@ -56,7 +60,7 @@ def search(self, val):
else:
return current_node.data
else:
return None
return

def remove(self, val):
"""Removenode from anywhere in the linked list with the given value."""
Expand All @@ -65,26 +69,34 @@ def remove(self, val):
'That value is not in this particular linked list.')
if self.head.data == val:
self.pop()
return None
return
current_node = self.head
previous_node = self.head
while current_node is not None:
if current_node.next_node.data != val:
if current_node.data != val:
previous_node = current_node
current_node = current_node.next_node
continue
else:
removed_node = current_node.next_node
current_node.next_node = current_node.next_node.next_node
previous_node.next_node = current_node.next_node
self._length -= 1
if removed_node.next_node is not None:
removed_node.next_node = None
break
else:
raise ValueError('Value not found.')

def display(self):
"""Present a visual representation of the linked list."""
node = self.head
display_str = ' '
while node is not None:
display_str += ' '
display_str += '\''
display_str += str(node.data)
display_str += '\''
node = node.next_node
display_str = ', '.join(display_str.split())
return '{}{}{}'.format('(', display_str, ')')

def __str__(self):
"""Interact with built-in print function."""
return self.display()
19 changes: 4 additions & 15 deletions src/priorityq.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Implement a priority queue using binary heap."""

# [{1: 0}, {100: 1}, {33: 2}, {44: 3}, {2: 0}]


class PriorityQueue(object):
"""Implement a priority queue."""
Expand All @@ -18,15 +16,8 @@ def heapify(self, iterable):
parent = (item_index - 1) // 2
while item_index > 0:
if list(heap_list[parent].values())[0] is 0 and list(heap_list[item_index].values())[0] is 0:
if list(heap_list[item_index].keys())[0] < list(heap_list[parent].keys())[0]:
curr_val = heap_list[parent]
heap_list[parent] = heap_list[item_index]
heap_list[item_index] = curr_val
item_index = parent
parent = (item_index - 1) // 2
else:
item_index = parent
parent = (item_index - 1) // 2
item_index = parent
parent = (item_index - 1) // 2
elif list(heap_list[parent].values())[0] > 0 and list(heap_list[item_index].values())[0] is 0:
item_index = parent
parent = (item_index - 1) // 2
Expand All @@ -46,14 +37,12 @@ def heapify(self, iterable):
else:
item_index = parent
parent = (item_index - 1) // 2
else:
break
return heap_list

def insert(self, value, priority=0):
"""Insert a value into the priority queue with an optional priority."""
if not isinstance(value, int) or not isinstance(priority, int):
raise TypeError("Must provide an integer for value and priority.")
if not isinstance(priority, int):
raise TypeError("Must provide an integer for priority.")
if priority < 0:
raise ValueError("You may not use a negative priority. Priority must be 0 or greater.")
self._heap.append({value: priority})
Expand Down
4 changes: 2 additions & 2 deletions src/que_.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def peek(self):
"""Return a new value without dequeuing it."""
print(self._new_dll.tail.data)

def __len__(self):
def size(self):
"""Length function for the queue."""
return len(self._new_dll)
return self._new_dll.size()
13 changes: 8 additions & 5 deletions src/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ def __init__(self, data=None):
self._new_linked_list = LinkedList(data)

def push(self, val):
"""Push function for stack."""
return self._new_linked_list.push(val)
"""Add a value to the top of the stack."""
self._new_linked_list.push(val)

def pop(self):
"""Pop function for stack."""
"""Remove and return the top of the stack."""
if len(self) == 0:
raise IndexError(
'Cannot pop from empty stack.')
return self._new_linked_list.pop()

def __len__(self):
"""Length function for stack."""
return len(self._new_linked_list)
"""Return the number of items in the stack."""
return self._new_linked_list.size()
Loading