From 04fa77e30d337fe64b9adb541d579d92158afb1a Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Tue, 31 Oct 2017 16:47:34 -0700 Subject: [PATCH 01/40] Done with binheap --- binheap.py | 36 ++++++++++++++++++++++++++++++++++++ test_binheap.py | 0 2 files changed, 36 insertions(+) create mode 100644 binheap.py create mode 100644 test_binheap.py diff --git a/binheap.py b/binheap.py new file mode 100644 index 0000000..22e5da7 --- /dev/null +++ b/binheap.py @@ -0,0 +1,36 @@ +"""Binary heap.""" + + +class BinHeap(object): + """ini the class node.""" + + def __init__(self): + """Create a new min heap.""" + self.heaplist = [0] + self._size = 0 + + def push(self, val): + """.""" + self.heaplist.append(val) + self._size += 1 + idx = self._size + while idx // 2 > 0: + if self.heaplist[idx] > self.heaplist[idx // 2]: + temp_val = self.heaplist[idx] + self.heaplist[idx] = self.heaplist[idx // 2] + self.heaplist[idx // 2] = temp_val + idx = idx // 2 + + def pop(self): + """.""" + self._size -= 1 + pop_val = self.heaplist[1] + self.heaplist[1] = self.heaplist.pop() + idx = 1 + while idx * 2 < self._size: + if self.heaplist[idx * 2] > self.heaplist[idx * 2 + 1]: + temp_val = self.heaplist[idx * 2 + 1] + self.heaplist[idx * 2 + 1] = self.heaplist[idx] + self.heaplist[idx] = temp_val + idx = idx * 2 + return pop_val diff --git a/test_binheap.py b/test_binheap.py new file mode 100644 index 0000000..e69de29 From 3f009ca20d6a09b4aebd49db0a4c755bed0e8ccb Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 1 Nov 2017 17:16:36 -0700 Subject: [PATCH 02/40] reworked push and working on pop, also wrote some tests --- binheap.py | 36 -------------------- src/binheap.py | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/conftest.py | 9 +++++ src/test_binheap.py | 65 +++++++++++++++++++++++++++++++++++ test_binheap.py | 0 5 files changed, 156 insertions(+), 36 deletions(-) delete mode 100644 binheap.py create mode 100644 src/binheap.py create mode 100644 src/conftest.py create mode 100644 src/test_binheap.py delete mode 100644 test_binheap.py diff --git a/binheap.py b/binheap.py deleted file mode 100644 index 22e5da7..0000000 --- a/binheap.py +++ /dev/null @@ -1,36 +0,0 @@ -"""Binary heap.""" - - -class BinHeap(object): - """ini the class node.""" - - def __init__(self): - """Create a new min heap.""" - self.heaplist = [0] - self._size = 0 - - def push(self, val): - """.""" - self.heaplist.append(val) - self._size += 1 - idx = self._size - while idx // 2 > 0: - if self.heaplist[idx] > self.heaplist[idx // 2]: - temp_val = self.heaplist[idx] - self.heaplist[idx] = self.heaplist[idx // 2] - self.heaplist[idx // 2] = temp_val - idx = idx // 2 - - def pop(self): - """.""" - self._size -= 1 - pop_val = self.heaplist[1] - self.heaplist[1] = self.heaplist.pop() - idx = 1 - while idx * 2 < self._size: - if self.heaplist[idx * 2] > self.heaplist[idx * 2 + 1]: - temp_val = self.heaplist[idx * 2 + 1] - self.heaplist[idx * 2 + 1] = self.heaplist[idx] - self.heaplist[idx] = temp_val - idx = idx * 2 - return pop_val diff --git a/src/binheap.py b/src/binheap.py new file mode 100644 index 0000000..077dba0 --- /dev/null +++ b/src/binheap.py @@ -0,0 +1,82 @@ +"""Binary heap.""" + + +class Binheap(object): + """Iniitialize the class node.""" + + def __init__(self, iterable=None): + """Create a new min heap.""" + self.heaplist = [] + self._size = 0 + if isinstance(iterable, (list, tuple)): + for item in iterable: + self.push(item) + + def push(self, val): + """Push a value to the end of heap and sort up.""" + if type(val) == int or type(val) == float: + self.heaplist.append(val) + self._size += 1 + if self._size > 1: + sort_up = True + idx = self._size - 1 + while sort_up: + if idx % 2 == 0: + parent = (idx - 2) // 2 + else: + parent = (idx - 1) // 2 + if val < self.heaplist[parent]: + self.heaplist[idx] = self.heaplist[parent] + self.heaplist[parent] = val + idx = parent + else: + sort_up = False + else: + raise ValueError('You must input numbers only.') + + def pop(self): + """Remove first value in heap and sort down.""" + if self._size == 0: + raise IndexError('There are no values to pop.') + elif self._size == 1: + self._size -= 1 + return self.heaplist.pop() + elif self._size > 1: + pop_val = self.heaplist[0] + self.heaplist[0] = self.heaplist.pop() + self._size -= 1 + idx = 0 + sort_down = True + while sort_down: + l_child = idx * 2 + 1 + r_child = idx * 2 + 2 + if self.heaplist[l_child] and self.heaplist[l_child]: + if self.heaplist[l_child] > self.heaplist[r_child]: + if self.heaplist[r_child] < self.heaplist[idx]: + temp_val = self.heaplist[idx] + self.heaplist[idx] = self.heaplist[r_child] + self.heaplist[r_child] = temp_val + idx = r_child + else: + sort_down = False + else: + if self.heaplist[l_child] < self.heaplist[r_child]: + temp_val = self.heaplist[idx] + self.heaplist[idx] = self.heaplist[l_child] + self.heaplist[l_child] = temp_val + idx = l_child + else: + sort_down = False + elif self.heaplist[l_child] and not self.heaplist[r_child]: + if self.heaplist[l_child] < self.heaplist[r_child]: + temp_val = self.heaplist[idx] + self.heaplist[idx] = self.heaplist[l_child] + self.heaplist[l_child] = temp_val + sort_down = False + elif self.heaplist[r_child] and not self.heaplist[l_child]: + if self.heaplist[r_child] < self.heaplist[idx]: + temp_val = self.heaplist[idx] + self.heaplist[idx] = self.heaplist[r_child] + self.heaplist[r_child] = temp_val + sort_down = False + return pop_val diff --git a/src/conftest.py b/src/conftest.py new file mode 100644 index 0000000..870d234 --- /dev/null +++ b/src/conftest.py @@ -0,0 +1,9 @@ +"""Fixutres for test_binheap.py.""" +import pytest + + +@pytest.fixture +def ebh(): + """Initialize an empty binary heap.""" + from binheap import Binheap + return Binheap() diff --git a/src/test_binheap.py b/src/test_binheap.py new file mode 100644 index 0000000..864db40 --- /dev/null +++ b/src/test_binheap.py @@ -0,0 +1,65 @@ +"""Test binheap.py.""" +import pytest + + +def test_initialize_empty_heap(ebh): + """Test if init binary is empty.""" + assert ebh + + +def test_push_one_value(ebh): + """Test size after push one value into heap.""" + ebh.push(9) + assert ebh._size == 1 + + +def test_push_mulitp_value(ebh): + """Test size after push multiple values into heap.""" + for i in range(20): + ebh.push(i) + assert ebh._size == 20 + + +def test_push_one_non_num(ebh): + """Test for erron if pushing non numerical value into heap.""" + with pytest.raises(ValueError): + ebh.push('NOOOOOOOO!!!!!!!!!') + + +def test_push_iterable(): + """Test if push successfully takes iterable.""" + from binheap import Binheap + heap = Binheap([1, 2, 3, 4, 5, 6, 7, 8]) + assert heap._size == 8 + + +def test_pop_on_empty_heap_raises_indexerror(ebh): + """Test for index error trying to pop from empty heap.""" + with pytest.raises(IndexError): + ebh.pop() + + +def test_pop_on_heap_with_one_item(ebh): + """Test pop returns item when only one item in heap.""" + ebh.push(1) + assert ebh.pop() == 1 + + +def test_pop_twice_on_heap_with_one_item(ebh): + """Test pop on fully popped heap.""" + ebh.push(1) + ebh.pop() + with pytest.raises(IndexError): + ebh.pop() + + +def test_pop_returns_sorted_values(ebh): + """Test pop entire bin returns sorted values.""" + from binheap import Binheap + import random + rand_nums = list(set([random.randint(0, 1000) + for i in range(20)])) + heap = Binheap(rand_nums) + # import pdb; pdb.set_trace() + all_popped = [heap.pop() for i in range(heap._size)] + assert all_popped == sorted(rand_nums) diff --git a/test_binheap.py b/test_binheap.py deleted file mode 100644 index e69de29..0000000 From 1d1137a85e92ff0b415acedc01b322be45e68b70 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Thu, 2 Nov 2017 14:27:13 -0700 Subject: [PATCH 03/40] completed readme, added setup, added tox, fixed push and pop, finished tests, all passing. --- README.md | 171 +++++++++++++++++++++++++++++++++++++++++++- setup.py | 16 +++++ src/binheap.py | 52 +++++++------- src/test_binheap.py | 15 +++- tox.ini | 9 +++ 5 files changed, 233 insertions(+), 30 deletions(-) create mode 100644 setup.py create mode 100644 tox.ini diff --git a/README.md b/README.md index 87c500a..ab9af5c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,169 @@ -# data-structures -Data-Structures +# Data-Structures + +Where a variety of data-structures found in python are being explored, such as: +* A simple class LinkedList for building linked lists as well as a subclass Stack to implement a stack. + +## Authors + +ChaiChaitanya Narukulla and Mark Reynoso created this code base as a project for Python 401 at Code Fellows Seattle. + +### Requirements for testing and development + +In order to create a development and testing environment, you will need the following: + +``` +python2, python3, ipython, pytest, pytest-watch, pytest-cov, tox +``` + +``` +To install environment: + +pip install -e .[testing][development] +``` + +### Linkded List Class Usage + +``` +To create an instance of a the LinkedList() class contained in package, from python: + +new = LinkedList() *you may choose and optional parameter of a single value or an iterable of one of the following: a tuple, a list, or a string.* + +LinkedList() contains the following methods: +* _push(val) (O1)_ - will insert the value ‘val’ at the head of the list. +* _pop() (O1)_ - will pop the first value off the head of the list and return it. Raises an exception with an appropriate message if there are no values to return. +* _size() (01)_ - will return the length of the list. +* _search(val) (On)_ - will return the node containing ‘val’ in the list, if present, else None. +* _remove(node) (On)_ - will remove the given node from the list, wherever it might be (node must be an item in the list). If the node is not in the list, it will raise an exception with an appropriate message. +* _display() (O1)_ - will return a unicode string representing the list as if it were a Python tuple literal: “(12, ‘sam’, 37, ‘tango’)” + +To access any contained methods: +new.push(val) +new.pop() +new.size() +new.search(val) +new.remove(node) +new.display() + +Additionally, LinkedList() will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. +* _print(new)_ - returns what the display() method returns. + +``` + +### Stack Class Usage + +``` +To create an instance of a Stack() class contained in package, from python: + +new = Stack() *you may choose and optional parameter of a single value or an iterable of one of the following: a tuple, a list, or a string.* + +Stack() contains the following methods: +* _push(val) (O1)_ - will insert the value ‘val’ at the head of the stack. +* _pop() (O1)_ - will pop the first value off the head of the stack and return it. Raises an exception with an appropriate message if there are no values to return. + +To access any contained methods: +new.push(val) +new.pop() + +Additionally, Stack() will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. + +``` + +### Doubly Linked List Class Usage + +``` +To create an instance of a Dll() class contained in package, from python: + +new = Dll() *you may not call Dll() with any parameters.* + +Dll() contains the following methods: +* _push(val) (O1)_ - will insert the value ‘val’ at the head of the list. +* _append(val) (O1)_ - will insert the value ‘val’ at the tail of the list. +* _pop() (O1)_ - will pop the first value off the head of the stack and return it. Raises an exception with an appropriate message if there are no values to return. +* _shift() (O1)_ - will remove the last value off the tail of the list and return it. Raises an exception with an appropriate message if there are no values to return. +* _remove(node) (On)_ - will remove the given node from the list, wherever it might be (node must be an item in the list). If the node is not in the list, it will raise an exception with an appropriate message. + +To access any contained methods: +new.push(val) +new.append(val) +new.pop() +new.shift() +new.remove() + +Additionally, Dll() will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. + +``` + +### Queue Class Usage + +``` +To create an instance of a Queue() class contained in package, from python: + +new = Queue() *you may not call Queue() with any parameters.* + +Queue() contains the following methods: +* _enqueue(val) (O1)_ - will insert the value ‘val’ at the end of the queue. +* _dequeue() (O1)_ - will remove the last value off the front of the queue and return it. Raises an exception with an appropriate message if there are no values to return. +* _peek() (O1)_ - shows the value of the node at the end of the queue. +* _size() (O1)_ - displays the number of node in the queue. + +To access any contained methods: +new.enqueue(val) +new.dequeue() +new.peek() +new.size() + +Additionally, Queue will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. + +``` + +### Deque Class Usage + +``` +To create an instance of a Deque() class contained in package, from python: + +new = Deque() *you may not call Deque() with any parameters.* + +Deque() contains the following methods: +* _append(val) (O1)_ - will insert the value ‘val’ at the end of the deque. +* _appendleft() (O1)_ - will insert a value to the front of the deque. +*_pop() (01)_ - removes the first val from the end of the deque. +*_popleft() (01)_ - removes the first val from the front of the deque. +* _peek() (O1)_ - shows the value of the item at the end of the deque. +* _peekleft() (O1)_ - shows the value of the item at the font of the deque. +* _size() (O1)_ - displays the number items in the deque. + +To access any contained methods: +new.append(val) +new.appendleft(val) +new.pop() +new.popleft() +new.peek() +new.peekleft() +new.size() + +``` + +### Binaary Heap (Binheap) Class Usage + +``` +To create an instance of a Binheap() class contained in package, from python: + +new = Binheap() *you may call Binheap() with any uniqe number or iterable of unique numbers.* + +Binheap() contains the following methods: +* _push(val) (Olog(n))_ - will insert the value ‘val’ at the end of the heap. +*_pop() (0log(n))_ - removes the first val from the end of the heap. + +To access any contained methods: +new.push(val) +new.pop() + +``` \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8011f7b --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup + +setup( + name='data-structures', + description=('A package for building and ' + 'running the data-structures module'), + package_dir={'': 'src'}, + author='Mark and chai', + author_email='chaitanyanarukulla@gmail.com mreynoso@spu.edu', + py_modules=['binheap'], + install_requires=[], + extras_require={ + 'testing': ['pytest', 'pytest-cov', 'pytest-watch', 'tox'], + 'development': ['ipython'] + }, +) diff --git a/src/binheap.py b/src/binheap.py index 077dba0..2bc2ae6 100644 --- a/src/binheap.py +++ b/src/binheap.py @@ -1,4 +1,4 @@ -"""Binary heap.""" +"""Binary heap class.""" class Binheap(object): @@ -17,10 +17,10 @@ def push(self, val): if type(val) == int or type(val) == float: self.heaplist.append(val) self._size += 1 - if self._size > 1: - sort_up = True - idx = self._size - 1 - while sort_up: + sort_up = True + idx = self._size - 1 + while sort_up: + if idx > 0: if idx % 2 == 0: parent = (idx - 2) // 2 else: @@ -31,6 +31,8 @@ def push(self, val): idx = parent else: sort_up = False + else: + sort_up = False else: raise ValueError('You must input numbers only.') @@ -50,33 +52,29 @@ def pop(self): while sort_down: l_child = idx * 2 + 1 r_child = idx * 2 + 2 - if self.heaplist[l_child] and self.heaplist[l_child]: + if r_child <= self._size - 1 and l_child <= self._size - 1: if self.heaplist[l_child] > self.heaplist[r_child]: if self.heaplist[r_child] < self.heaplist[idx]: - temp_val = self.heaplist[idx] - self.heaplist[idx] = self.heaplist[r_child] - self.heaplist[r_child] = temp_val + self.heaplist[r_child], self.heaplist[idx] =\ + self.heaplist[idx], self.heaplist[r_child] idx = r_child else: sort_down = False else: if self.heaplist[l_child] < self.heaplist[r_child]: - temp_val = self.heaplist[idx] - self.heaplist[idx] = self.heaplist[l_child] - self.heaplist[l_child] = temp_val - idx = l_child - else: - sort_down = False - elif self.heaplist[l_child] and not self.heaplist[r_child]: - if self.heaplist[l_child] < self.heaplist[r_child]: - temp_val = self.heaplist[idx] - self.heaplist[idx] = self.heaplist[l_child] - self.heaplist[l_child] = temp_val - sort_down = False - elif self.heaplist[r_child] and not self.heaplist[l_child]: - if self.heaplist[r_child] < self.heaplist[idx]: - temp_val = self.heaplist[idx] - self.heaplist[idx] = self.heaplist[r_child] - self.heaplist[r_child] = temp_val - sort_down = False + if self.heaplist[l_child] < self.heaplist[idx]: + self.heaplist[idx], self.heaplist[l_child] =\ + self.heaplist[l_child], self.heaplist[idx] + idx = l_child + else: + sort_down = False + elif l_child == self._size - 1: + if self.heaplist[l_child] < self.heaplist[idx]: + self.heaplist[idx], self.heaplist[l_child] =\ + self.heaplist[l_child], self.heaplist[idx] + sort_down = False + else: + sort_down = False + else: + sort_down = False return pop_val diff --git a/src/test_binheap.py b/src/test_binheap.py index 864db40..7b4ac0f 100644 --- a/src/test_binheap.py +++ b/src/test_binheap.py @@ -53,7 +53,7 @@ def test_pop_twice_on_heap_with_one_item(ebh): ebh.pop() -def test_pop_returns_sorted_values(ebh): +def test_pop_returns_sorted_values(): """Test pop entire bin returns sorted values.""" from binheap import Binheap import random @@ -63,3 +63,16 @@ def test_pop_returns_sorted_values(ebh): # import pdb; pdb.set_trace() all_popped = [heap.pop() for i in range(heap._size)] assert all_popped == sorted(rand_nums) + + +def test_pop_returns_sorted_values_limited(ebh): + """Test pop in controlled environment.""" + ebh.push(23), + ebh.push(2), + ebh.push(30), + ebh.push(50), + ebh.push(6), + ebh.push(17), + ebh.push(29) + all_popped = [ebh.pop() for i in range(7)] + assert all_popped == [2, 6, 17, 23, 29, 30, 50] diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..06c5283 --- /dev/null +++ b/tox.ini @@ -0,0 +1,9 @@ +[tox] + +envlist = py27, py36 + +[testenv] +commands = py.test src --cov=src --cov-report term-missing +deps = + pytest + pytest-cov \ No newline at end of file From d36c2458d6cf54a5823a3fd2c76192e6721766d0 Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Fri, 3 Nov 2017 14:12:12 -0700 Subject: [PATCH 04/40] initial setup --- src/priorityq.py | 0 src/test_priorityq.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/priorityq.py create mode 100644 src/test_priorityq.py diff --git a/src/priorityq.py b/src/priorityq.py new file mode 100644 index 0000000..e69de29 diff --git a/src/test_priorityq.py b/src/test_priorityq.py new file mode 100644 index 0000000..e69de29 From 9380413915ae8fd0991e008b37c88a7e5160d1a5 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Fri, 3 Nov 2017 15:03:23 -0700 Subject: [PATCH 05/40] untested priority heap written. works great in theory. --- src/priorityq.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/priorityq.py b/src/priorityq.py index e69de29..386b105 100644 --- a/src/priorityq.py +++ b/src/priorityq.py @@ -0,0 +1,34 @@ +"""Create a priority queue instance.""" + + +class Priorityq(object): + """Create a que with priority attributes for each value.""" + + def __init__(self): + """Initialize a priority queue instance.""" + self._que = {} + self._highest = 0 + + def insert(self, val, priority): + """Insert a new value into the queue.""" + if priority: + if priority < self._highest: + self._highest = priority + if self._que[priority]: + self._que[priority].append(val) + else: + self._que[priority] = [val] + else: + lowest = 0 + for low_priority in self._que: + if low_priority > self._highest: + lowest = low_priority + self._que[lowest].append(val) + + def pop(self): + """Remove the highest prioty value from queue.""" + return self._que[self._highest].pop(0) + + def peek(self): + """View the hightest priority item.""" + return self._que[self._highest][0] From 0881f6c022ed1ac04d71ccddc4c759e0b88de109 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Fri, 3 Nov 2017 16:52:36 -0700 Subject: [PATCH 06/40] fixed insert and pop methods and wrote a few tests, all passing --- src/conftest.py | 7 +++++++ src/priorityq.py | 41 +++++++++++++++++++++++++++-------------- src/test_priorityq.py | 24 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/conftest.py b/src/conftest.py index 870d234..5f89810 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -7,3 +7,10 @@ def ebh(): """Initialize an empty binary heap.""" from binheap import Binheap return Binheap() + + +@pytest.fixture +def pq(): + """Initialize a empty pq.""" + from priorityq import Priorityq + return Priorityq() diff --git a/src/priorityq.py b/src/priorityq.py index 386b105..476cb9d 100644 --- a/src/priorityq.py +++ b/src/priorityq.py @@ -8,26 +8,39 @@ def __init__(self): """Initialize a priority queue instance.""" self._que = {} self._highest = 0 + self._lowest = 0 - def insert(self, val, priority): + def insert(self, val, priority=0): """Insert a new value into the queue.""" - if priority: - if priority < self._highest: - self._highest = priority - if self._que[priority]: - self._que[priority].append(val) - else: - self._que[priority] = [val] + if priority <= self._highest: + self._highest = priority + if priority >= self._lowest: + self._lowest = priority + if priority in self._que: + self._que[priority].append(val) else: - lowest = 0 - for low_priority in self._que: - if low_priority > self._highest: - lowest = low_priority - self._que[lowest].append(val) + self._que[priority] = [val] def pop(self): """Remove the highest prioty value from queue.""" - return self._que[self._highest].pop(0) + if len(self._que) == 0: + raise IndexError('There are no value to pop.') + else: + self._que[self._highest].pop(0) + if self._que[self._highest] == []: + self._que.pop(self._highest) + if len(self._que) == 0: + self._highest = 0 + self._lowest = 0 + high = max(self._highest) + self._highest = high + if self._que[self._lowest] == []: + self._que.pop(self._lowest) + if len(self._que) == 0: + self._lowest = 0 + self._lowest = 0 + high = max(self._lowest) + self._highest = high def peek(self): """View the hightest priority item.""" diff --git a/src/test_priorityq.py b/src/test_priorityq.py index e69de29..4f31593 100644 --- a/src/test_priorityq.py +++ b/src/test_priorityq.py @@ -0,0 +1,24 @@ +"""Test priorityq.py.""" +import pytest + + +def test_priorityq_iinitialize_empty_que(pq): + """Test initialize an empty pq.""" + assert pq + + +def test_priorityq_insert_one_val_w_priority(pq): + """Test insert one value to pq with priority.""" + pq.insert('hi', 0) + assert pq._highest == 0 + + +def test_insert_n_values_returns_n_length(pq): + """Test that the number of values inserted is the que's length.""" + for i in range(20): + pq.insert(i) + total_length = 0 + for key in pq._que: + total_length += len(pq._que[key]) + assert total_length == 20 + From f09487392a7dc676578dbf57f746f62c97fbfd0f Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Sun, 5 Nov 2017 12:09:42 -0800 Subject: [PATCH 07/40] Test written and passing all test --- README.md | 19 +++++++++ src/priorityq.py | 15 +++---- src/test_priorityq.py | 98 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ab9af5c..45cc5eb 100644 --- a/README.md +++ b/README.md @@ -166,4 +166,23 @@ To access any contained methods: new.push(val) new.pop() +``` + +### Priorityq (Priorityq) Class Usage + +``` +To create an instance of a Priorityq() class contained in package, from python: + +new = Priorityq() *you may call Priorityq() with any uniqe number or iterable of unique numbers.* + +Priorityq() contains the following methods: +*_insert(val,priority) (O1)_ - will insert the value ‘val’ at the end of the heap. +*_pop() (01)_ - removes the higest priority and returns the higest priority. +*_peek() (01)_ - returns the higest priority. + +To access any contained methods: +new.insert(val) +new.pop() +new.peek() + ``` \ No newline at end of file diff --git a/src/priorityq.py b/src/priorityq.py index 476cb9d..04fa8a3 100644 --- a/src/priorityq.py +++ b/src/priorityq.py @@ -26,21 +26,16 @@ def pop(self): if len(self._que) == 0: raise IndexError('There are no value to pop.') else: - self._que[self._highest].pop(0) + val = self._que[self._highest].pop(0) if self._que[self._highest] == []: self._que.pop(self._highest) if len(self._que) == 0: self._highest = 0 self._lowest = 0 - high = max(self._highest) - self._highest = high - if self._que[self._lowest] == []: - self._que.pop(self._lowest) - if len(self._que) == 0: - self._lowest = 0 - self._lowest = 0 - high = max(self._lowest) - self._highest = high + else: + high = min(self._que) + self._highest = high + return val def peek(self): """View the hightest priority item.""" diff --git a/src/test_priorityq.py b/src/test_priorityq.py index 4f31593..8ff64fb 100644 --- a/src/test_priorityq.py +++ b/src/test_priorityq.py @@ -22,3 +22,101 @@ def test_insert_n_values_returns_n_length(pq): total_length += len(pq._que[key]) assert total_length == 20 + +def test_priorityq_insert_with_many_priority_returns_higest(pq): + """Test ipriorityq insert with many priority returns higest pirority.""" + pq.insert('hi', -10) + pq.insert('one', 1) + pq.insert('four', 4) + pq.insert('eight', 8) + assert pq._highest == -10 + + +def test_priorityq_insert_with_many_priority_returns_lowest(pq): + """Test ipriorityq insert with many priority returns lowest pirority.""" + pq.insert('hi', -10) + pq.insert('one', 1) + pq.insert('four', 4) + pq.insert('eight', 8) + assert pq._lowest == 8 + + +def test_priorityq_insert_with_same_priority_returns_list_of_val(pq): + """Test if inserted with same priority returns list of val in pirority.""" + pq.insert('hi', -10) + pq.insert('one', -10) + pq.insert('four', 4) + pq.insert('eight', 4) + assert pq._que[-10] == ['hi', 'one'] + + +def test_if_pop_raises_indexerror_on_empty_prorityq(pq): + """Test if pop reises indexerroe.""" + with pytest.raises(IndexError): + pq.pop() + + +def test_priorityq_pop_function_pops(pq): + """Test ipriorityq if it pops a val.""" + pq.insert('hi', -10) + pq.insert('one', 1) + pq.pop() + pq.pop() + assert pq._que == {} + + +def test_priorityq_pop_function_returns_val(pq): + """Test ipriorityq if it pops and returns the right val.""" + pq.insert('hi', -10) + pq.insert('one', 1) + assert pq.pop() == 'hi' + + +def test_pop_if_more_poped_then_inserted_raises_error(pq): + """Test if inserted with same priority returns list of val in pirority.""" + pq.insert('hi', -10) + pq.insert('one', -10) + pq.insert('four', 4) + pq.pop() + pq.pop() + pq.pop() + with pytest.raises(IndexError): + pq.pop() + + +def test_pop_if_returns_in_prioety_order(pq): + """Test if inserted with priority returns in priority order.""" + pq.insert('hi', -10) + pq.insert('one', -1) + pq.insert('four', 4) + pop1 = pq.pop() + pop2 = pq.pop() + pop3 = pq.pop() + assert pop1 == 'hi' + assert pop2 == 'one' + assert pop3 == 'four' + + +def test_pop_if_returns_in_prioety_order_pop_in_and_out(pq): + """Test if inserted val with priority returns in priority order.""" + pq.insert('hi', -10) + pq.insert('four', 4) + pq.insert('one', -1) + pop1 = pq.pop() + pop2 = pq.pop() + pq.insert('first', -20) + pop3 = pq.pop() + pq.insert('last', 20) + pop4 = pq.pop() + assert pop1 == 'hi' + assert pop2 == 'one' + assert pop3 == 'first' + assert pop4 == 'four' + + +def test_peek_returns_the_higest_priority_val(pq): + """Test if peek returns the val in line to be poped.""" + pq.insert('hi', -10) + pq.insert('one', 1) + pq.insert('four', 4) + assert pq.peek() == 'hi' From ac42137bf264f9c69894b85df59c321a73cf435e Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 5 Nov 2017 13:51:57 -0800 Subject: [PATCH 08/40] created graph --- src/graph.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++ src/test_graph.py | 0 2 files changed, 86 insertions(+) create mode 100644 src/graph.py create mode 100644 src/test_graph.py diff --git a/src/graph.py b/src/graph.py new file mode 100644 index 0000000..3407833 --- /dev/null +++ b/src/graph.py @@ -0,0 +1,86 @@ +"""Implement a class for a graph data structure.""" + + +class Graph(object): + """Graph class.""" + + def __init__(self): + """Initialize a graph.""" + self._graph = {} + + def nodes(self): + """Return a list of all nodes in graph.""" + return self._graph.keys() + + def edges(self): + """Return a list of edges in graph.""" + edges = [] + for key in self._graph: + for i in key: + edges.append((key, i)) + return edges + + def add_node(self, val): + """Add a node with value of val to graph.""" + self._graph[val] + + def add_edge(self, val1, val2): + """Add a new edge to graph between val1 & val2 as well as add vals.""" + if val1 in self._graph and val2 in self._graph: + if val2 not in self._graph[val1]: + self._graph[val1].append(val2) + if val1 not in self._graph[val2]: + self._graph[val2].append(val1) + if val1 in self._graph and val2 not in self._graph: + self._graph[val1].append(val2) + self._graph[val2] = [val1] + if val2 in self._graph and val1 not in self._graph: + self._graph[val2].append(val1) + self._graph[val1] = [val2] + else: + self._graph[val1] = [val2] + self._graph[val2] = [val1] + + def del_node(self, val): + """Delete node w/val from graph, raises exception if not exist.""" + if self._graph[val]: + self._graph.remove(val) + for key in self._graph: + for i in self._graph: + if i == val: + i.remove(val) + raise ValueError('There is no node of that value in the graph.') + + def del_edge(self, val1, val2): + """Delete edge between val1 & val2 from graph.""" + if self._graph[val1] and self._graph[val2]: + for edge in self._graph[val1]: + if edge == val2: + edge.remove(val2) + for edge in self._graph[val2]: + if edge == val1: + edge.remove(val1) + raise ValueError('These edges do not exist.') + + def has_node(self, val): + """Return true or false if node has value.""" + if self._graph[val]: + return True + else: + return False + + def neighbors(self, val): + """Return list of nodes connected node(val).""" + return self._graph[val] + + def adjacent(self, val1, val2): + """Return true if edge between vals, else false, & error if no val.""" + if self._graph[val1] and self._graph[val2]: + for edge in self._graph[val1]: + if edge == val2: + return True + for edge in self._graph[val2]: + if edge == val1: + return True + return False + raise ValueError('These edges do not exist.') diff --git a/src/test_graph.py b/src/test_graph.py new file mode 100644 index 0000000..e69de29 From 03b0444fb9b1f1eca60c5ea287fafffd3913b4a2 Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Sun, 5 Nov 2017 16:21:48 -0800 Subject: [PATCH 09/40] Writing test --- src/conftest.py | 7 +++++ src/graph.py | 19 +++++++------- src/test_graph.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/conftest.py b/src/conftest.py index 5f89810..81d0af3 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -14,3 +14,10 @@ def pq(): """Initialize a empty pq.""" from priorityq import Priorityq return Priorityq() + + +@pytest.fixture +def g(): + """Initialize a empty pq.""" + from graph import Graph + return Graph() diff --git a/src/graph.py b/src/graph.py index 3407833..d993b2a 100644 --- a/src/graph.py +++ b/src/graph.py @@ -10,36 +10,37 @@ def __init__(self): def nodes(self): """Return a list of all nodes in graph.""" - return self._graph.keys() + return list(self._graph.keys()) def edges(self): """Return a list of edges in graph.""" edges = [] for key in self._graph: - for i in key: + print(edges) + for i in self._graph[key]: edges.append((key, i)) return edges def add_node(self, val): """Add a node with value of val to graph.""" - self._graph[val] + self._graph[val] = [] def add_edge(self, val1, val2): """Add a new edge to graph between val1 & val2 as well as add vals.""" if val1 in self._graph and val2 in self._graph: if val2 not in self._graph[val1]: self._graph[val1].append(val2) - if val1 not in self._graph[val2]: - self._graph[val2].append(val1) + print(self._graph[val1]) if val1 in self._graph and val2 not in self._graph: + self._graph[val2] = [] self._graph[val1].append(val2) - self._graph[val2] = [val1] if val2 in self._graph and val1 not in self._graph: - self._graph[val2].append(val1) - self._graph[val1] = [val2] + self._graph[val1] = [] + self._graph[val1].append(val2) else: self._graph[val1] = [val2] - self._graph[val2] = [val1] + self._graph[val2] = [] + def del_node(self, val): """Delete node w/val from graph, raises exception if not exist.""" diff --git a/src/test_graph.py b/src/test_graph.py index e69de29..61c6b66 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -0,0 +1,66 @@ +"""Testing graph.py.""" +# import pytest + + +def test_graph_iinitialize_empty_dict(g): + """Test initialize an empty pq.""" + assert g._graph == {} + + +def test_add_node(g): + """Test it adds note.""" + g.add_node(5) + assert g._graph[5] == [] + + +def test_node_returns_list_of_all_nodes(g): + """Test if nodes return list of all nodes.""" + g.add_node(5) + g.add_node(7) + g.add_node(8) + assert g.nodes() == [5, 7, 8] + + +def test_edges_return_list_of_all_nodes_edges(g): + """Test it adds note.""" + g.add_node(5) + g.add_node(7) + g.edges() + assert g.edges() == [] + +def test_add_nodel_adds_node(g): + """Test if add_node add nodes.""" + g.add_node(5) + g.add_node(7) + g.add_node(8) + assert g.nodes() == [5, 7, 8] + + + +def test_add_edges_adds_edges(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + assert g.edges() == [(5, 7)] + + +def test_add_edges_with_e(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_edge(5, 7) + g.add_edge(7, 9) + assert g.edges() == [(5, 7), (7, 9)] + +def test_add_edges_will_add_mutipul_edges_to_a_node_(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7) + g.add_edge(5, 9) + g.add_edge(5, 10) + assert g.edges() == [(5, 7), (5, 9), (5, 10)] From 2c6e481e7833f7b27b7ffdb13cc733edc945972c Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Sun, 5 Nov 2017 18:17:39 -0800 Subject: [PATCH 10/40] test are passing --- src/graph.py | 65 +++++++++++++++------------ src/test_graph.py | 112 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 146 insertions(+), 31 deletions(-) diff --git a/src/graph.py b/src/graph.py index d993b2a..fcb81d0 100644 --- a/src/graph.py +++ b/src/graph.py @@ -16,7 +16,6 @@ def edges(self): """Return a list of edges in graph.""" edges = [] for key in self._graph: - print(edges) for i in self._graph[key]: edges.append((key, i)) return edges @@ -30,58 +29,66 @@ def add_edge(self, val1, val2): if val1 in self._graph and val2 in self._graph: if val2 not in self._graph[val1]: self._graph[val1].append(val2) - print(self._graph[val1]) - if val1 in self._graph and val2 not in self._graph: + elif val1 in self._graph and val2 not in self._graph: self._graph[val2] = [] self._graph[val1].append(val2) - if val2 in self._graph and val1 not in self._graph: + elif val2 in self._graph and val1 not in self._graph: self._graph[val1] = [] self._graph[val1].append(val2) else: self._graph[val1] = [val2] self._graph[val2] = [] - def del_node(self, val): """Delete node w/val from graph, raises exception if not exist.""" - if self._graph[val]: - self._graph.remove(val) + if val in self._graph: + del self._graph[val] for key in self._graph: - for i in self._graph: + for i in self._graph[key]: if i == val: i.remove(val) - raise ValueError('There is no node of that value in the graph.') + else: + raise ValueError('There is no node of that value in the graph.') def del_edge(self, val1, val2): """Delete edge between val1 & val2 from graph.""" - if self._graph[val1] and self._graph[val2]: - for edge in self._graph[val1]: - if edge == val2: - edge.remove(val2) - for edge in self._graph[val2]: - if edge == val1: - edge.remove(val1) - raise ValueError('These edges do not exist.') + try: + if val2 in self._graph[val1]: + self._graph[val1].remove(val2) + else: + raise ValueError('These edges do not exist.') + except KeyError: + raise ValueError('These edges do not exist.') def has_node(self, val): """Return true or false if node has value.""" - if self._graph[val]: - return True - else: + try: + if val in self._graph: + return True + else: + return False + except KeyError: return False def neighbors(self, val): """Return list of nodes connected node(val).""" - return self._graph[val] + try: + neighbors = self._graph[val] + for key in self._graph: + if val in self._graph[key]: + if self._graph[key] not in neighbors: + neighbors.append(self._graph[key]) + return neighbors + except KeyError: + raise ValueError('This node dosent exit') def adjacent(self, val1, val2): """Return true if edge between vals, else false, & error if no val.""" - if self._graph[val1] and self._graph[val2]: - for edge in self._graph[val1]: - if edge == val2: - return True - for edge in self._graph[val2]: - if edge == val1: - return True + if val1 in self._graph or val2 in self._graph: + if val1 in self._graph[val2]: + return True + if val2 in self._graph[val1]: + return True return False - raise ValueError('These edges do not exist.') + else: + raise ValueError('These edges do not exist.') diff --git a/src/test_graph.py b/src/test_graph.py index 61c6b66..05dec78 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -1,5 +1,5 @@ """Testing graph.py.""" -# import pytest +import pytest def test_graph_iinitialize_empty_dict(g): @@ -28,6 +28,7 @@ def test_edges_return_list_of_all_nodes_edges(g): g.edges() assert g.edges() == [] + def test_add_nodel_adds_node(g): """Test if add_node add nodes.""" g.add_node(5) @@ -36,7 +37,6 @@ def test_add_nodel_adds_node(g): assert g.nodes() == [5, 7, 8] - def test_add_edges_adds_edges(g): """Test if edges are added if 2 val are given.""" g.add_node(5) @@ -54,6 +54,7 @@ def test_add_edges_with_e(g): g.add_edge(7, 9) assert g.edges() == [(5, 7), (7, 9)] + def test_add_edges_will_add_mutipul_edges_to_a_node_(g): """Test if edges are added if 2 val are given.""" g.add_node(5) @@ -64,3 +65,110 @@ def test_add_edges_will_add_mutipul_edges_to_a_node_(g): g.add_edge(5, 9) g.add_edge(5, 10) assert g.edges() == [(5, 7), (5, 9), (5, 10)] + + +def test_del_node_delets_the_node(g): + """Test del node delets the node.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.del_node(7) + assert g.nodes() == [5, 9] + + +def test_del_node_raises_valueerroe(g): + """Test del node delets the node.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.del_node(5) + g.del_node(7) + g.del_node(9) + with pytest.raises(ValueError): + g.del_node(5) + + +def test_del_edge_delets_edges(g): + """Test if del_edges delets edges.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.del_edge(5, 7) + assert g.edges() == [] + + +def test_del_edge_raises_value_error(g): + """Test if del_edges raises value error.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.del_edge(5, 7) + with pytest.raises(ValueError): + g.del_edge(5, 7) + + +def test_del_edge_raises_key_error(g): + """Test if del_edges raises value error.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.del_edge(5, 7) + with pytest.raises(ValueError): + g.del_edge(9, 0) + + +def test_has_node_has_no_node(g): + """Test false if there is no node.""" + assert g.has_node(2) is False + + +def test_has_node_has_node(g): + """Test false if there is no node.""" + g.add_node(5) + assert g.has_node(5) is True + +def test_neighbors_return_list_of_nodes_connected(g): + """Test neighbor retutns the list of nodes connected.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + g.add_edge(5, 8) + assert g.neighbors(5) == [7, 9, 8] + + +def test_neighbors_raises_valueerror(g): + """Test neighbor retutns the list of nodes connected.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + with pytest.raises(ValueError): + g.neighbors(20) + + +def test_adjacent_has_any_edges(g): + """Test adjacent has edges returns true.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + assert g.adjacent(5, 7) is True + + +def test_adjacent_has_no_edges(g): + """Test adjacent has no edges returns false.""" + g.add_node(5) + g.add_node(7) + assert g.adjacent(5, 7) is False + + +def test_adjacent_raises_valueerror(g): + """Test adjacent has no edges returns false.""" + g.add_node(5) + g.add_node(7) + with pytest.raises(ValueError): + g.adjacent(9, 0) From bc22f461ac746b9791b5f50569a097defd7ed2ee Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 5 Nov 2017 19:04:37 -0800 Subject: [PATCH 11/40] tests passing and cleaned up code --- README.md | 31 +++++++++++++ src/graph.py | 11 ++--- src/test_graph.py | 113 +++++++++++++++++++++++++++++++--------------- 3 files changed, 111 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 45cc5eb..1e3fa74 100644 --- a/README.md +++ b/README.md @@ -185,4 +185,35 @@ new.insert(val) new.pop() new.peek() +``` + +### Graph Class Usage + +``` +To create an instance of a Graph() class contained in package, from python: + +new = Graph() *you may not call Graph() with any parameters.* + +Graph() contains the following methods: +* _nodes() (O1)_ - will return a list of all nodes in the graph. +* _edges() (On2)_ - will return a list of edges in graph. +*_add_node(val) (01)_ - adds a new node to the graph. +*_add_edge(val1, val2) (0n)_ - add a new edge to nodes in the graph. +* _del_node(val) (On2)_ - delete node from graph. +* _del_edge(val1, val2) (On)_ - delete edge between two nodes. +* _has_node(val) (O1)_ - returns true if node exists and false if not. +* _neighbors(val) (On2)_ - return a list of nodes with edges to input node. +* _adjacent(val1, val2) (On)_ - return true if edge exists between two inputs, otherwise false. + +To access any contained methods: +new.nodes() +new.edges() +new.add_node(val) +new.add_edge(val1, val2) +new.del_node(val) +new.del_edge(val1, val2) +new.has_node(val) +new.neighbors(val) +new.adjacent(val1, val2) + ``` \ No newline at end of file diff --git a/src/graph.py b/src/graph.py index fcb81d0..2608f18 100644 --- a/src/graph.py +++ b/src/graph.py @@ -46,7 +46,7 @@ def del_node(self, val): for key in self._graph: for i in self._graph[key]: if i == val: - i.remove(val) + self._graph[key].remove(val) else: raise ValueError('There is no node of that value in the graph.') @@ -62,12 +62,9 @@ def del_edge(self, val1, val2): def has_node(self, val): """Return true or false if node has value.""" - try: - if val in self._graph: - return True - else: - return False - except KeyError: + if val in self._graph: + return True + else: return False def neighbors(self, val): diff --git a/src/test_graph.py b/src/test_graph.py index 05dec78..6f09fd9 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -13,31 +13,27 @@ def test_add_node(g): assert g._graph[5] == [] -def test_node_returns_list_of_all_nodes(g): - """Test if nodes return list of all nodes.""" +def test_add_node_returns_list_of_all_nodes(g): + """Test if add node return list of all nodes.""" g.add_node(5) g.add_node(7) g.add_node(8) - assert g.nodes() == [5, 7, 8] + assert 5 in g.nodes() + assert 7 in g.nodes() + assert 8 in g.nodes() + assert type(g.nodes()) == list -def test_edges_return_list_of_all_nodes_edges(g): - """Test it adds note.""" + +def test_edges_return_list_of_tuples_of_all_edges(g): + """Test if edges returns list of tuples with pairings of each edge.""" g.add_node(5) g.add_node(7) g.edges() assert g.edges() == [] -def test_add_nodel_adds_node(g): - """Test if add_node add nodes.""" - g.add_node(5) - g.add_node(7) - g.add_node(8) - assert g.nodes() == [5, 7, 8] - - -def test_add_edges_adds_edges(g): +def test_add_edges_returns_list_of_edges(g): """Test if edges are added if 2 val are given.""" g.add_node(5) g.add_node(7) @@ -45,7 +41,7 @@ def test_add_edges_adds_edges(g): assert g.edges() == [(5, 7)] -def test_add_edges_with_e(g): +def test_add_two_edges(g): """Test if edges are added if 2 val are given.""" g.add_node(5) g.add_node(7) @@ -55,7 +51,7 @@ def test_add_edges_with_e(g): assert g.edges() == [(5, 7), (7, 9)] -def test_add_edges_will_add_mutipul_edges_to_a_node_(g): +def test_add_edges_will_add_mutipule_edges_to_a_node(g): """Test if edges are added if 2 val are given.""" g.add_node(5) g.add_node(7) @@ -67,19 +63,61 @@ def test_add_edges_will_add_mutipul_edges_to_a_node_(g): assert g.edges() == [(5, 7), (5, 9), (5, 10)] -def test_del_node_delets_the_node(g): +def test_add_edges_with_one_val_in_graph_add_second_val(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7) + g.add_edge(5, 9) + g.add_edge(5, 15) + assert g.edges() == [(5, 7), (5, 9), (5, 15)] + + +def test_add_edges_with_first_val_new_and_second_val_in_graph(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7) + g.add_edge(5, 9) + g.add_edge(15, 5) + assert (15, 5) in g.edges() + assert (5, 7) in g.edges() + assert (5, 9) in g.edges() + + +def test_add_edges_with_two_new_nodes(g): + """Test add edges when both nodes are new to graph.""" + g.add_edge(19, 100) + assert g.edges() == [(19, 100)] + + +def test_del_node_deletes_the_node_given(g): """Test del node delets the node.""" g.add_node(5) g.add_node(7) g.add_node(9) g.del_node(7) - assert g.nodes() == [5, 9] + assert 7 not in g.nodes() -def test_del_node_raises_valueerroe(g): +def test_del_node_deletes_the_node_given(g): """Test del node delets the node.""" g.add_node(5) g.add_node(7) + g.add_edge(5, 7) + g.add_node(9) + g.del_node(7) + assert 7 not in g.nodes() + + +def test_del_node_raises_value_error(g): + """Test del node raises value error if node has been deleted.""" + g.add_node(5) + g.add_node(7) g.add_node(9) g.del_node(5) g.del_node(7) @@ -88,8 +126,8 @@ def test_del_node_raises_valueerroe(g): g.del_node(5) -def test_del_edge_delets_edges(g): - """Test if del_edges delets edges.""" +def test_del_edge_deletes_all_edges(g): + """Test if del_edges deletes all edges.""" g.add_node(5) g.add_node(7) g.add_edge(5, 7) @@ -97,7 +135,7 @@ def test_del_edge_delets_edges(g): assert g.edges() == [] -def test_del_edge_raises_value_error(g): +def test_del_edge_raises_value_error_if_too_many_edges_deleted(g): """Test if del_edges raises value error.""" g.add_node(5) g.add_node(7) @@ -107,8 +145,8 @@ def test_del_edge_raises_value_error(g): g.del_edge(5, 7) -def test_del_edge_raises_key_error(g): - """Test if del_edges raises value error.""" +def test_del_edge_raises_value_error_if_values_not_in_dict(g): + """Test if del_edges raises value error for values not in dict.""" g.add_node(5) g.add_node(7) g.add_edge(5, 7) @@ -117,18 +155,19 @@ def test_del_edge_raises_key_error(g): g.del_edge(9, 0) -def test_has_node_has_no_node(g): - """Test false if there is no node.""" +def test_has_node_is_false_on_empty_graph(g): + """Test false if there are no nodes in graph.""" assert g.has_node(2) is False -def test_has_node_has_node(g): - """Test false if there is no node.""" +def test_has_node_if_value_in_graph(g): + """Test false if node does not exist.""" g.add_node(5) assert g.has_node(5) is True -def test_neighbors_return_list_of_nodes_connected(g): - """Test neighbor retutns the list of nodes connected.""" + +def test_neighbors_return_list_of_nodes_connected_to_input(g): + """Test neighbor retutns the list of nodes connected input value.""" g.add_node(5) g.add_node(7) g.add_edge(5, 7) @@ -139,8 +178,8 @@ def test_neighbors_return_list_of_nodes_connected(g): assert g.neighbors(5) == [7, 9, 8] -def test_neighbors_raises_valueerror(g): - """Test neighbor retutns the list of nodes connected.""" +def test_neighbors_raises_valueerror_if_node_not_exist(g): + """Test neighbor raises value error if input not a node.""" g.add_node(5) g.add_node(7) g.add_edge(5, 7) @@ -151,23 +190,23 @@ def test_neighbors_raises_valueerror(g): g.neighbors(20) -def test_adjacent_has_any_edges(g): - """Test adjacent has edges returns true.""" +def test_adjacent_if_two_nodes_have_edge_is_true(g): + """Test adjacent has edges returns true when edge exists.""" g.add_node(5) g.add_node(7) g.add_edge(5, 7) assert g.adjacent(5, 7) is True -def test_adjacent_has_no_edges(g): +def test_adjacent_when_edge_when_no_edges(g): """Test adjacent has no edges returns false.""" g.add_node(5) g.add_node(7) assert g.adjacent(5, 7) is False -def test_adjacent_raises_valueerror(g): - """Test adjacent has no edges returns false.""" +def test_adjacent_raises_valueerror_if_input_not_node(g): + """Test adjacent raises value error if node does not exist.""" g.add_node(5) g.add_node(7) with pytest.raises(ValueError): From b244da29bfc3dcc4f60e993ef32b2fcc51ea7913 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Tue, 7 Nov 2017 14:36:18 -0800 Subject: [PATCH 12/40] depth first tested and finished --- src/graph.py | 18 +++++++++++++ src/test_graph.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/graph.py b/src/graph.py index 2608f18..db62fe0 100644 --- a/src/graph.py +++ b/src/graph.py @@ -89,3 +89,21 @@ def adjacent(self, val1, val2): return False else: raise ValueError('These edges do not exist.') + + def depth_first_traversal(self, start_val): + """Traverse the graph from first edge of each node until ultimate.""" + if start_val in self._graph: + depth_traversal = [] + path = [start_val] + while path: + val = path.pop() + if val not in depth_traversal: + depth_traversal.append(val) + path = path + self._graph[val] + return depth_traversal + else: + raise ValueError('Value is not in graph.') + + def breadth_first_traversal(self, start_val): + """Traverse the graph by node's edges before moving to next node.""" + pass diff --git a/src/test_graph.py b/src/test_graph.py index 6f09fd9..e2d0d95 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -211,3 +211,67 @@ def test_adjacent_raises_valueerror_if_input_not_node(g): g.add_node(7) with pytest.raises(ValueError): g.adjacent(9, 0) + + +def test_depth_first_one_node_many_edges(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(5, 7) + g.add_edge(5, 8) + g.add_edge(5, 9) + assert g.depth_first_traversal(5) == [5, 9, 8, 7, 6] + + +def test_depth_first_node_no_edge_return_only_self(g): + """Test depth first traversal returns start val if no edges.""" + g.add_node(8) + g.add_node(9) + g.add_node(10) + g.add_edge(10, 8) + assert g.depth_first_traversal(8) == [8] + + +def test_depth_first_one_edge_per_node(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(6, 7) + g.add_edge(7, 8) + g.add_edge(8, 9) + assert g.depth_first_traversal(5) == [5, 6, 7, 8, 9] + + +def test_df_raises_value_error_node_not_exist(g): + """Test df traversal raises value error if val not in graph.""" + with pytest.raises(ValueError): + g.depth_first_traversal('blerg') + + +def test_depth_first_many_node_many_edges(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + g.add_edge(6, 9) + g.add_edge(7, 8) + g.add_edge(8, 5) + g.add_edge(9, 5) + g.add_edge(9, 6) + g.add_edge(9, 8) + g.add_edge(6, 5) + g.add_edge(7, 5) + g.add_edge(8, 9) + g.add_edge(6, 8) + assert g.depth_first_traversal(8) == [8, 9, 6, 5] From 69d06dafdf7eb92201759c06de531ec7c5ccbb98 Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Tue, 7 Nov 2017 15:05:13 -0800 Subject: [PATCH 13/40] implemented breadth_first_traversal and depth_first_traversal function and written test --- src/graph.py | 12 +++++++- src/test_graph.py | 75 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/src/graph.py b/src/graph.py index db62fe0..62c5711 100644 --- a/src/graph.py +++ b/src/graph.py @@ -106,4 +106,14 @@ def depth_first_traversal(self, start_val): def breadth_first_traversal(self, start_val): """Traverse the graph by node's edges before moving to next node.""" - pass + if start_val in self._graph: + depth_traversal = [] + path = [start_val] + while path: + val = path.pop(0) + if val not in depth_traversal: + depth_traversal.append(val) + path = path + self._graph[val] + return depth_traversal + else: + raise ValueError('Value is not in graph.') diff --git a/src/test_graph.py b/src/test_graph.py index e2d0d95..f9bbd80 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -24,7 +24,6 @@ def test_add_node_returns_list_of_all_nodes(g): assert type(g.nodes()) == list - def test_edges_return_list_of_tuples_of_all_edges(g): """Test if edges returns list of tuples with pairings of each edge.""" g.add_node(5) @@ -104,16 +103,6 @@ def test_del_node_deletes_the_node_given(g): assert 7 not in g.nodes() -def test_del_node_deletes_the_node_given(g): - """Test del node delets the node.""" - g.add_node(5) - g.add_node(7) - g.add_edge(5, 7) - g.add_node(9) - g.del_node(7) - assert 7 not in g.nodes() - - def test_del_node_raises_value_error(g): """Test del node raises value error if node has been deleted.""" g.add_node(5) @@ -275,3 +264,67 @@ def test_depth_first_many_node_many_edges(g): g.add_edge(8, 9) g.add_edge(6, 8) assert g.depth_first_traversal(8) == [8, 9, 6, 5] + + +def test_breadth_first_one_node_many_edges(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(5, 7) + g.add_edge(5, 8) + g.add_edge(5, 9) + assert g.breadth_first_traversal(5) == [5, 6, 7, 8, 9] + + +def test_breadth_first_node_no_edge_return_only_self(g): + """Test breadth first traversal returns start val if no edges.""" + g.add_node(8) + g.add_node(9) + g.add_node(10) + g.add_edge(10, 8) + assert g.depth_first_traversal(8) == [8] + + +def test_breadth_first_one_edge_per_node(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(6, 7) + g.add_edge(7, 8) + g.add_edge(8, 9) + assert g.depth_first_traversal(5) == [5, 6, 7, 8, 9] + + +def test_bf_raises_value_error_node_not_exist(g): + """Test bf traversal raises value error if val not in graph.""" + with pytest.raises(ValueError): + g.depth_first_traversal('blerg') + + +def test_bf_first_many_node_many_edges(g): + """Test bf first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + g.add_edge(6, 9) + g.add_edge(7, 8) + g.add_edge(8, 5) + g.add_edge(9, 5) + g.add_edge(9, 6) + g.add_edge(9, 8) + g.add_edge(6, 5) + g.add_edge(7, 5) + g.add_edge(8, 9) + g.add_edge(6, 8) + assert g.depth_first_traversal(8) == [8, 9, 6, 5] From 9b64dc231311d9c1f49ad57a464b9aa22e383486 Mon Sep 17 00:00:00 2001 From: "Chaitanya.Narukulla" Date: Tue, 7 Nov 2017 15:17:04 -0800 Subject: [PATCH 14/40] tested for cyclic graphs --- README.md | 7 ++++++- src/test_graph.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1e3fa74..fd6a44f 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,10 @@ Graph() contains the following methods: * _del_edge(val1, val2) (On)_ - delete edge between two nodes. * _has_node(val) (O1)_ - returns true if node exists and false if not. * _neighbors(val) (On2)_ - return a list of nodes with edges to input node. -* _adjacent(val1, val2) (On)_ - return true if edge exists between two inputs, otherwise false. +* _adjacent(val1, val2) (On)_ - return true if edge exists between two inputs, + otherwise false. +* _depth_first_traversal(start_val) (Ologn)_ - will return full visited path of traversal completed. +* _breadth_first_traversal(start_val)(Ologn)_ - will return full visited path of traversal completed. To access any contained methods: new.nodes() @@ -215,5 +218,7 @@ new.del_edge(val1, val2) new.has_node(val) new.neighbors(val) new.adjacent(val1, val2) +new.depth_first_traversal(start_val) +new.breadth_first_traversal(start_val) ``` \ No newline at end of file diff --git a/src/test_graph.py b/src/test_graph.py index f9bbd80..80c2760 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -328,3 +328,21 @@ def test_bf_first_many_node_many_edges(g): g.add_edge(8, 9) g.add_edge(6, 8) assert g.depth_first_traversal(8) == [8, 9, 6, 5] + + +def test_breadth_first_works_on_cyclic_graph(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_edge(5, 6) + g.add_edge(6, 5) + assert g.breadth_first_traversal(5) == [5, 6] + + +def test_depth_first_works_on_cyclic_graph(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_edge(5, 6) + g.add_edge(6, 5) + assert g.breadth_first_traversal(5) == [5, 6] From a73a4b173be04d9ea8a2673b2b215fe3447c3176 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 8 Nov 2017 14:36:16 -0800 Subject: [PATCH 15/40] converted graph to dictionaries and added weight to edges --- src/graph.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/graph.py b/src/graph.py index 62c5711..3f1d921 100644 --- a/src/graph.py +++ b/src/graph.py @@ -14,30 +14,30 @@ def nodes(self): def edges(self): """Return a list of edges in graph.""" - edges = [] + edges = {} for key in self._graph: for i in self._graph[key]: - edges.append((key, i)) + edges[(key, i)] = self._graph[key][i] return edges def add_node(self, val): """Add a node with value of val to graph.""" - self._graph[val] = [] + self._graph[val] = {} - def add_edge(self, val1, val2): + def add_edge(self, val1, val2, weight=0): """Add a new edge to graph between val1 & val2 as well as add vals.""" if val1 in self._graph and val2 in self._graph: if val2 not in self._graph[val1]: - self._graph[val1].append(val2) + self._graph[val1][val2] = weight elif val1 in self._graph and val2 not in self._graph: - self._graph[val2] = [] - self._graph[val1].append(val2) + self._graph[val2] = {} + self._graph[val1][val2] = weight elif val2 in self._graph and val1 not in self._graph: - self._graph[val1] = [] - self._graph[val1].append(val2) + self._graph[val1] = {} + self._graph[val1][val2] = weight else: - self._graph[val1] = [val2] - self._graph[val2] = [] + self._graph[val1] = {val2: weight} + self._graph[val2] = {} def del_node(self, val): """Delete node w/val from graph, raises exception if not exist.""" @@ -46,7 +46,7 @@ def del_node(self, val): for key in self._graph: for i in self._graph[key]: if i == val: - self._graph[key].remove(val) + del self._graph[key] else: raise ValueError('There is no node of that value in the graph.') @@ -54,12 +54,13 @@ def del_edge(self, val1, val2): """Delete edge between val1 & val2 from graph.""" try: if val2 in self._graph[val1]: - self._graph[val1].remove(val2) + del self._graph[val1] else: raise ValueError('These edges do not exist.') except KeyError: raise ValueError('These edges do not exist.') + def has_node(self, val): """Return true or false if node has value.""" if val in self._graph: @@ -70,7 +71,7 @@ def has_node(self, val): def neighbors(self, val): """Return list of nodes connected node(val).""" try: - neighbors = self._graph[val] + neighbors = [] for key in self._graph: if val in self._graph[key]: if self._graph[key] not in neighbors: @@ -99,7 +100,7 @@ def depth_first_traversal(self, start_val): val = path.pop() if val not in depth_traversal: depth_traversal.append(val) - path = path + self._graph[val] + path = path + list(self._graph[val].keys()) return depth_traversal else: raise ValueError('Value is not in graph.') @@ -113,7 +114,7 @@ def breadth_first_traversal(self, start_val): val = path.pop(0) if val not in depth_traversal: depth_traversal.append(val) - path = path + self._graph[val] + path = path + list(self._graph[val].keys()) return depth_traversal else: raise ValueError('Value is not in graph.') From 12f72b6f7a0cb909747bd60a975e12a594487fdd Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 8 Nov 2017 15:29:55 -0800 Subject: [PATCH 16/40] refactored tests for python 2 --- src/graph.py | 9 +++---- src/test_graph.py | 61 +++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/graph.py b/src/graph.py index 3f1d921..cd8b85d 100644 --- a/src/graph.py +++ b/src/graph.py @@ -60,7 +60,6 @@ def del_edge(self, val1, val2): except KeyError: raise ValueError('These edges do not exist.') - def has_node(self, val): """Return true or false if node has value.""" if val in self._graph: @@ -70,12 +69,10 @@ def has_node(self, val): def neighbors(self, val): """Return list of nodes connected node(val).""" + neighbors = [] try: - neighbors = [] - for key in self._graph: - if val in self._graph[key]: - if self._graph[key] not in neighbors: - neighbors.append(self._graph[key]) + for key in self._graph[val]: + neighbors.append(key) return neighbors except KeyError: raise ValueError('This node dosent exit') diff --git a/src/test_graph.py b/src/test_graph.py index 80c2760..e269442 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -10,11 +10,11 @@ def test_graph_iinitialize_empty_dict(g): def test_add_node(g): """Test it adds note.""" g.add_node(5) - assert g._graph[5] == [] + assert g._graph[5] == {} -def test_add_node_returns_list_of_all_nodes(g): - """Test if add node return list of all nodes.""" +def test_nodes_returns_list_of_all_nodes(g): + """Test nodes return list of all nodes.""" g.add_node(5) g.add_node(7) g.add_node(8) @@ -24,20 +24,20 @@ def test_add_node_returns_list_of_all_nodes(g): assert type(g.nodes()) == list -def test_edges_return_list_of_tuples_of_all_edges(g): - """Test if edges returns list of tuples with pairings of each edge.""" +def test_edges_return_dict_of_tuples_of_all_edge(g): + """Test if edges returns dict of tuples as edge and weight as value.""" g.add_node(5) g.add_node(7) g.edges() - assert g.edges() == [] + assert g.edges() == {} -def test_add_edges_returns_list_of_edges(g): - """Test if edges are added if 2 val are given.""" +def test_add_edges_returns_dict_of_edges_as_tuple_keys(g): + """Test if edges are added if 2 val are given with default weight.""" g.add_node(5) g.add_node(7) g.add_edge(5, 7) - assert g.edges() == [(5, 7)] + assert g.edges() == {(5, 7): 0} def test_add_two_edges(g): @@ -47,7 +47,7 @@ def test_add_two_edges(g): g.add_node(9) g.add_edge(5, 7) g.add_edge(7, 9) - assert g.edges() == [(5, 7), (7, 9)] + assert g.edges() == {(5, 7): 0, (7, 9): 0} def test_add_edges_will_add_mutipule_edges_to_a_node(g): @@ -59,7 +59,7 @@ def test_add_edges_will_add_mutipule_edges_to_a_node(g): g.add_edge(5, 7) g.add_edge(5, 9) g.add_edge(5, 10) - assert g.edges() == [(5, 7), (5, 9), (5, 10)] + assert g.edges() == {(5, 7): 0, (5, 9): 0, (5, 10): 0} def test_add_edges_with_one_val_in_graph_add_second_val(g): @@ -71,7 +71,7 @@ def test_add_edges_with_one_val_in_graph_add_second_val(g): g.add_edge(5, 7) g.add_edge(5, 9) g.add_edge(5, 15) - assert g.edges() == [(5, 7), (5, 9), (5, 15)] + assert g.edges() == {(5, 7): 0, (5, 9): 0, (5, 15): 0} def test_add_edges_with_first_val_new_and_second_val_in_graph(g): @@ -91,7 +91,7 @@ def test_add_edges_with_first_val_new_and_second_val_in_graph(g): def test_add_edges_with_two_new_nodes(g): """Test add edges when both nodes are new to graph.""" g.add_edge(19, 100) - assert g.edges() == [(19, 100)] + assert g.edges() == {(19, 100): 0} def test_del_node_deletes_the_node_given(g): @@ -121,7 +121,7 @@ def test_del_edge_deletes_all_edges(g): g.add_node(7) g.add_edge(5, 7) g.del_edge(5, 7) - assert g.edges() == [] + assert g.edges() == {} def test_del_edge_raises_value_error_if_too_many_edges_deleted(g): @@ -164,7 +164,7 @@ def test_neighbors_return_list_of_nodes_connected_to_input(g): g.add_node(9) g.add_edge(5, 9) g.add_edge(5, 8) - assert g.neighbors(5) == [7, 9, 8] + assert sorted(g.neighbors(5)) == [7, 8, 9] def test_neighbors_raises_valueerror_if_node_not_exist(g): @@ -213,7 +213,12 @@ def test_depth_first_one_node_many_edges(g): g.add_edge(5, 7) g.add_edge(5, 8) g.add_edge(5, 9) - assert g.depth_first_traversal(5) == [5, 9, 8, 7, 6] + assert 5 in g.depth_first_traversal(5) + assert 9 in g.depth_first_traversal(5) + assert 6 in g.depth_first_traversal(5) + assert 7 in g.depth_first_traversal(5) + assert 8 in g.depth_first_traversal(5) + assert type(g.depth_first_traversal(5)) == list def test_depth_first_node_no_edge_return_only_self(g): @@ -252,18 +257,19 @@ def test_depth_first_many_node_many_edges(g): g.add_node(7) g.add_node(8) g.add_node(9) + g.add_edge(8, 9) + g.add_edge(8, 5) g.add_edge(5, 9) g.add_edge(6, 9) g.add_edge(7, 8) - g.add_edge(8, 5) g.add_edge(9, 5) g.add_edge(9, 6) g.add_edge(9, 8) g.add_edge(6, 5) g.add_edge(7, 5) - g.add_edge(8, 9) g.add_edge(6, 8) - assert g.depth_first_traversal(8) == [8, 9, 6, 5] + dt = g.depth_first_traversal(8) + assert dt == [8, 5, 9, 6] or dt == [8, 9, 6 , 5] or dt == [8, 9, 5, 6] def test_breadth_first_one_node_many_edges(g): @@ -277,7 +283,12 @@ def test_breadth_first_one_node_many_edges(g): g.add_edge(5, 7) g.add_edge(5, 8) g.add_edge(5, 9) - assert g.breadth_first_traversal(5) == [5, 6, 7, 8, 9] + assert 5 in g.breadth_first_traversal(5) + assert 9 in g.breadth_first_traversal(5) + assert 6 in g.breadth_first_traversal(5) + assert 7 in g.breadth_first_traversal(5) + assert 8 in g.breadth_first_traversal(5) + assert type(g.breadth_first_traversal(5)) == list def test_breadth_first_node_no_edge_return_only_self(g): @@ -289,8 +300,8 @@ def test_breadth_first_node_no_edge_return_only_self(g): assert g.depth_first_traversal(8) == [8] -def test_breadth_first_one_edge_per_node(g): - """Test breadth first traversal returns all paths from one node.""" +def test_depth_first_one_edge_per_node(g): + """Test depth first traversal returns all paths from one node.""" g.add_node(5) g.add_node(6) g.add_node(7) @@ -327,7 +338,11 @@ def test_bf_first_many_node_many_edges(g): g.add_edge(7, 5) g.add_edge(8, 9) g.add_edge(6, 8) - assert g.depth_first_traversal(8) == [8, 9, 6, 5] + assert 5 in g.depth_first_traversal(8) + assert 9 in g.depth_first_traversal(8) + assert 6 in g.depth_first_traversal(8) + assert 5 in g.depth_first_traversal(8) + # assert g.depth_first_traversal(8) == [8, 9, 6, 5] def test_breadth_first_works_on_cyclic_graph(g): From 67c69e53b94ef3d13d5e5bdfe00d6db01f8a03aa Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 8 Nov 2017 16:30:08 -0800 Subject: [PATCH 17/40] added testing for weights of edges --- src/test_graph.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/test_graph.py b/src/test_graph.py index e269442..ea9aae0 100644 --- a/src/test_graph.py +++ b/src/test_graph.py @@ -361,3 +361,65 @@ def test_depth_first_works_on_cyclic_graph(g): g.add_edge(5, 6) g.add_edge(6, 5) assert g.breadth_first_traversal(5) == [5, 6] + + +def test_add_edges_w_weights_returns_dict_of_edges_as_tuple_keys(g): + """Test if edges are added if 2 val are given with default weight.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7, 6) + assert g.edges() == {(5, 7): 6} + + +def test_add_two_edges_w_weights_(g): + """Test if edges are added if 2 val are given with weights returns dict.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_edge(5, 7, 100) + g.add_edge(7, 9, 1) + assert g.edges() == {(5, 7): 100, (7, 9): 1} + + +def test_add_edges_w_weights_will_add_mutipule_edges_to_a_node(g): + """Test if edges are added if 2 val are given with weights.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7, 4) + g.add_edge(5, 9) + g.add_edge(5, 10, 12039) + assert g.edges() == {(5, 7): 4, (5, 9): 0, (5, 10): 12039} + + +def test_add_edges_with_weights_with_one_val_in_graph_add_second_val(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7, 0) + g.add_edge(5, 9, 3) + g.add_edge(5, 15, 6) + assert g.edges() == {(5, 7): 0, (5, 9): 3, (5, 15): 6} + + +def test_add_edges_w_weights_with_first_val_new_and_second_val_in_graph(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7, 4) + g.add_edge(5, 9, 7) + g.add_edge(15, 5, 9) + assert (15, 5) in g.edges() + assert (5, 7) in g.edges() + assert (5, 9) in g.edges() + + +def test_add_edges_with_weights_with_two_new_nodes(g): + """Test add edges when both nodes are new to graph.""" + g.add_edge(19, 100, 1000) + assert g.edges() == {(19, 100): 1000} From 0700ebc81aa3088383925607b850b28169bca804 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 8 Nov 2017 16:32:55 -0800 Subject: [PATCH 18/40] updated readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fd6a44f..c8dd3d7 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ new.peek() ``` -### Graph Class Usage +### Graph Class (weighted) Usage ``` To create an instance of a Graph() class contained in package, from python: @@ -196,9 +196,9 @@ new = Graph() *you may not call Graph() with any parameters.* Graph() contains the following methods: * _nodes() (O1)_ - will return a list of all nodes in the graph. -* _edges() (On2)_ - will return a list of edges in graph. +* _edges() (On2)_ - will return a dictionary of edges in graph with weights. *_add_node(val) (01)_ - adds a new node to the graph. -*_add_edge(val1, val2) (0n)_ - add a new edge to nodes in the graph. +*_add_edge(val1, val2, weight) (0n)_ - add a new edge to nodes in the graph. * _del_node(val) (On2)_ - delete node from graph. * _del_edge(val1, val2) (On)_ - delete edge between two nodes. * _has_node(val) (O1)_ - returns true if node exists and false if not. @@ -212,7 +212,7 @@ To access any contained methods: new.nodes() new.edges() new.add_node(val) -new.add_edge(val1, val2) +new.add_edge(val1, val2, weight) new.del_node(val) new.del_edge(val1, val2) new.has_node(val) From b9c8b6f083fe2f1a2bdf343663717d1f6d7007a0 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Thu, 9 Nov 2017 10:53:15 -0800 Subject: [PATCH 19/40] added if name=main and adjusted for new dictionary setup --- src/graph.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/graph.py b/src/graph.py index cd8b85d..89dde44 100644 --- a/src/graph.py +++ b/src/graph.py @@ -115,3 +115,42 @@ def breadth_first_traversal(self, start_val): return depth_traversal else: raise ValueError('Value is not in graph.') + + +if __name__ == '__main__': + graph_data = { + 'A': {'B': 0, 'D': 0}, + 'B': {'A': 0, 'E': 0, 'D': 0}, + 'C': {'A': 0, 'B': 0}, + 'D': {}, + 'E': {'D': 0, 'C': 0, 'B': 0, 'A': 0} + } + g = Graph() + g._graph = graph_data + print('Using the following graph:\n\n{}\n\nthe lists below show a ' + 'depth_first_traversal followed by a ' + 'breadth_first_traversal:'.format(graph_data)) + print('\nDepth First Traversal:') + print(g.depth_first_traversal('A')) + print('\nBreadth First Traversal:') + print(g.breadth_first_traversal('A')) + + graph_data = { + 'George': {'Steve': 0, 'Jane': 0, 'Phil': 0}, + 'Anne': {'Abe': 0, 'Uma': 0, 'George': 0}, + 'George': {'Abe': 0, 'Steve': 0}, + 'Steve': {'Anne': 0}, + 'Abe': {'George': 0, 'Uma': 0, 'Steve': 0, 'Phil': 0}, + 'Uma': {'Steve': 0}, + 'Phil': {'Uma': 0, 'George': 0, 'Phil': 0}, + 'Jane': {'Anne': 0}, + } + g2 = Graph() + g2._graph = graph_data + print('\n\nHere is another example on the following graph:\n\n{}\n\nthe ' + 'lists below, again, show a depth_first_traversal followed by a ' + 'breadth_first_traversal:'.format(graph_data)) + print('\nDepth First Traversal:') + print(g2.depth_first_traversal('Abe')) + print('\nBreadth First Traversal:') + print(g2.breadth_first_traversal('Abe')) From ad445ab18a72f64c33a701ca21316a7c4a9952cd Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 12 Nov 2017 13:11:18 -0800 Subject: [PATCH 20/40] for loop over q not working, changing size mid loop not working --- src/dijykstra.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/dijykstra.py diff --git a/src/dijykstra.py b/src/dijykstra.py new file mode 100644 index 0000000..7fe1404 --- /dev/null +++ b/src/dijykstra.py @@ -0,0 +1,63 @@ +"""Using Dijkstra's algorithm to solve the shortest path.""" +from graph import Graph + +from priorityq import Priorityq + +graph = { + 'A': {'C': 4, + 'B': 2, + 'D': 5 + }, + 'C': {'G': 20, + 'D': 8 + }, + 'B': {'E': 8 + }, + 'D': {'C': 4, + 'B': 3, + 'F': 10, + 'G': 15 + }, + 'E': {'F': 8, + 'D': 8 + }, + 'F': {'G': 5 + }, + 'G': {} +} + + +g = Graph() +g._graph = graph + + +def dijkstra(graph, start, end): + """Dijkysta algorithm to calculate the shortest path.""" + distance = {} + parents = {} + q = {} + # q = Priorityq() + + q[start] = 0 + + for val in q: + distance[val] = q[val] + if val == end: + break + for edge in graph._graph[val]: + length = distance[val] + graph._graph[val][edge] + if edge not in q or length < q[edge]: + q[edge] = length + parents[edge] = val + + return parents + + +def shortest_distance(graph, start, end): + """Utilize dijkstra to find the shortest path.""" + d = dijkstra(graph, start, end) + path = [start, end] + while end != start: + path.insert(1, d[end]) + end = d[end] + return path From f8bab38a174aefcfa7922c8358a9ba8692aa95f4 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 19 Nov 2017 10:19:32 -0800 Subject: [PATCH 21/40] refactoring back to priority q --- src/dijykstra.py | 8 +++++--- src/priorityq.py | 8 +++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/dijykstra.py b/src/dijykstra.py index 7fe1404..00c5f70 100644 --- a/src/dijykstra.py +++ b/src/dijykstra.py @@ -35,11 +35,13 @@ def dijkstra(graph, start, end): """Dijkysta algorithm to calculate the shortest path.""" distance = {} parents = {} - q = {} - # q = Priorityq() + # q = {} + q = Priorityq() - q[start] = 0 + q.insert(start, 0) + while q: + for val in q: distance[val] = q[val] if val == end: diff --git a/src/priorityq.py b/src/priorityq.py index 04fa8a3..e9912c4 100644 --- a/src/priorityq.py +++ b/src/priorityq.py @@ -12,6 +12,9 @@ def __init__(self): def insert(self, val, priority=0): """Insert a new value into the queue.""" + if len(self._que) == 0: + self._highest = priority + self._lowest = priority if priority <= self._highest: self._highest = priority if priority >= self._lowest: @@ -39,4 +42,7 @@ def pop(self): def peek(self): """View the hightest priority item.""" - return self._que[self._highest][0] + if len(self._que) == 0: + raise IndexError('There are no items to view.') + else: + return self._que[self._highest][0] From 7ed931d061c4298e246fe7961994bffea2ae4399 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 19 Nov 2017 13:35:16 -0800 Subject: [PATCH 22/40] shortest path working --- src/dijykstra.py | 59 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/dijykstra.py b/src/dijykstra.py index 00c5f70..62fc976 100644 --- a/src/dijykstra.py +++ b/src/dijykstra.py @@ -1,7 +1,6 @@ """Using Dijkstra's algorithm to solve the shortest path.""" from graph import Graph -from priorityq import Priorityq graph = { 'A': {'C': 4, @@ -26,31 +25,50 @@ 'G': {} } +test_graph = { + 'A': {'B': 5, + 'C': 6 + }, + 'B': {'D': 2 + }, + 'C': {'E': 8, + 'F': 4 + }, + 'D': {'E': 2, + 'G': 10 + }, + 'E': {'G': 10, + 'F': 4 + }, +} g = Graph() -g._graph = graph +g._graph = test_graph def dijkstra(graph, start, end): """Dijkysta algorithm to calculate the shortest path.""" - distance = {} + distance = {start: 0} parents = {} - # q = {} - q = Priorityq() - q.insert(start, 0) + not_visited = list(graph._graph.keys()) + + while not_visited: + min_node = None + for val in not_visited: + if val in distance: + if min_node is None: + min_node = val + elif distance[val] < distance[min_node]: + min_node = val + + not_visited.remove(min_node) - while q: - - for val in q: - distance[val] = q[val] - if val == end: - break - for edge in graph._graph[val]: - length = distance[val] + graph._graph[val][edge] - if edge not in q or length < q[edge]: - q[edge] = length - parents[edge] = val + for edge in graph._graph[min_node]: + length = distance[min_node] + graph._graph[min_node][edge] + if edge not in distance or length < distance[edge]: + distance[edge] = length + parents[edge] = min_node return parents @@ -58,8 +76,11 @@ def dijkstra(graph, start, end): def shortest_distance(graph, start, end): """Utilize dijkstra to find the shortest path.""" d = dijkstra(graph, start, end) - path = [start, end] + path = [end] while end != start: - path.insert(1, d[end]) + path.insert(0, d[end]) end = d[end] return path + + +ans = shortest_distance(g, 'A', 'E') From c8ab7c38301462f3aaa7f20bca896f16b4886db3 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 19 Nov 2017 14:40:15 -0800 Subject: [PATCH 23/40] fixed no children error --- src/dijykstra.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/dijykstra.py b/src/dijykstra.py index 62fc976..4c8246c 100644 --- a/src/dijykstra.py +++ b/src/dijykstra.py @@ -43,7 +43,7 @@ } g = Graph() -g._graph = test_graph +g._graph = graph def dijkstra(graph, start, end): @@ -61,14 +61,15 @@ def dijkstra(graph, start, end): min_node = val elif distance[val] < distance[min_node]: min_node = val - not_visited.remove(min_node) - - for edge in graph._graph[min_node]: - length = distance[min_node] + graph._graph[min_node][edge] - if edge not in distance or length < distance[edge]: - distance[edge] = length - parents[edge] = min_node + if graph._graph[min_node] == {}: + return parents + else: + for edge in graph._graph[min_node]: + length = distance[min_node] + graph._graph[min_node][edge] + if edge not in distance or length < distance[edge]: + distance[edge] = length + parents[edge] = min_node return parents @@ -76,11 +77,10 @@ def dijkstra(graph, start, end): def shortest_distance(graph, start, end): """Utilize dijkstra to find the shortest path.""" d = dijkstra(graph, start, end) + if d == {}: + raise ValueError('This start node has no edges.') path = [end] while end != start: path.insert(0, d[end]) end = d[end] return path - - -ans = shortest_distance(g, 'A', 'E') From 8ed41a1bd82972b8e0537e4ec7a47806e9b81f54 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 19 Nov 2017 14:50:29 -0800 Subject: [PATCH 24/40] changed filename to match --- src/{dijykstra.py => shortest_dis.py} | 2 ++ 1 file changed, 2 insertions(+) rename src/{dijykstra.py => shortest_dis.py} (98%) diff --git a/src/dijykstra.py b/src/shortest_dis.py similarity index 98% rename from src/dijykstra.py rename to src/shortest_dis.py index 4c8246c..4f127c4 100644 --- a/src/dijykstra.py +++ b/src/shortest_dis.py @@ -43,6 +43,8 @@ } g = Graph() +tg = Graph() +tg._graph = test_graph g._graph = graph From b6897ad061397b72927794ab09a74f85ea01fe18 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 19 Nov 2017 15:07:41 -0800 Subject: [PATCH 25/40] Updated Test for Shortest_dis --- src/dijykstra.py | 86 ------------------------------ src/graph.py | 2 +- src/shortest_dis.py | 41 +++++++++++++++ src/test_shortest_dis.py | 110 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 87 deletions(-) delete mode 100644 src/dijykstra.py create mode 100644 src/shortest_dis.py create mode 100644 src/test_shortest_dis.py diff --git a/src/dijykstra.py b/src/dijykstra.py deleted file mode 100644 index 4c8246c..0000000 --- a/src/dijykstra.py +++ /dev/null @@ -1,86 +0,0 @@ -"""Using Dijkstra's algorithm to solve the shortest path.""" -from graph import Graph - - -graph = { - 'A': {'C': 4, - 'B': 2, - 'D': 5 - }, - 'C': {'G': 20, - 'D': 8 - }, - 'B': {'E': 8 - }, - 'D': {'C': 4, - 'B': 3, - 'F': 10, - 'G': 15 - }, - 'E': {'F': 8, - 'D': 8 - }, - 'F': {'G': 5 - }, - 'G': {} -} - -test_graph = { - 'A': {'B': 5, - 'C': 6 - }, - 'B': {'D': 2 - }, - 'C': {'E': 8, - 'F': 4 - }, - 'D': {'E': 2, - 'G': 10 - }, - 'E': {'G': 10, - 'F': 4 - }, -} - -g = Graph() -g._graph = graph - - -def dijkstra(graph, start, end): - """Dijkysta algorithm to calculate the shortest path.""" - distance = {start: 0} - parents = {} - - not_visited = list(graph._graph.keys()) - - while not_visited: - min_node = None - for val in not_visited: - if val in distance: - if min_node is None: - min_node = val - elif distance[val] < distance[min_node]: - min_node = val - not_visited.remove(min_node) - if graph._graph[min_node] == {}: - return parents - else: - for edge in graph._graph[min_node]: - length = distance[min_node] + graph._graph[min_node][edge] - if edge not in distance or length < distance[edge]: - distance[edge] = length - parents[edge] = min_node - - return parents - - -def shortest_distance(graph, start, end): - """Utilize dijkstra to find the shortest path.""" - d = dijkstra(graph, start, end) - if d == {}: - raise ValueError('This start node has no edges.') - path = [end] - while end != start: - path.insert(0, d[end]) - end = d[end] - return path diff --git a/src/graph.py b/src/graph.py index 89dde44..6215399 100644 --- a/src/graph.py +++ b/src/graph.py @@ -117,7 +117,7 @@ def breadth_first_traversal(self, start_val): raise ValueError('Value is not in graph.') -if __name__ == '__main__': +if __name__ == '__main__': # pragma: no cover graph_data = { 'A': {'B': 0, 'D': 0}, 'B': {'A': 0, 'E': 0, 'D': 0}, diff --git a/src/shortest_dis.py b/src/shortest_dis.py new file mode 100644 index 0000000..266aea5 --- /dev/null +++ b/src/shortest_dis.py @@ -0,0 +1,41 @@ +"""Shorest distance.""" + + +def dijkstra(graph, start, end): + """Dijkysta algorithm to calculate the shortest path.""" + distance = {start: 0} + parents = {} + + not_visited = list(graph._graph.keys()) + + while not_visited: + min_node = None + for val in not_visited: + if val in distance: + if min_node is None: + min_node = val + elif distance[val] < distance[min_node]: + min_node = val + + not_visited.remove(min_node) + if graph._graph[min_node] == {}: + return parents + for edge in graph._graph[min_node]: + length = distance[min_node] + graph._graph[min_node][edge] + if edge not in distance or length < distance[edge]: + distance[edge] = length + parents[edge] = min_node + + return parents + + +def shortest_distance(graph, start, end): + """Utilize dijkstra to find the shortest path.""" + d = dijkstra(graph, start, end) + if d == {}: + raise ValueError('This start node has no edges.') + path = [end] + while end != start: + path.insert(0, d[end]) + end = d[end] + return path diff --git a/src/test_shortest_dis.py b/src/test_shortest_dis.py new file mode 100644 index 0000000..8648f6f --- /dev/null +++ b/src/test_shortest_dis.py @@ -0,0 +1,110 @@ +"""Test for Shorest distance graph.""" +import pytest + +from graph import Graph +from shortest_dis import dijkstra, shortest_distance + + +graph = { + 'A': {'C': 4, + 'B': 2, + 'D': 5 + }, + 'C': {'G': 20, + 'D': 8 + }, + 'B': {'E': 8 + }, + 'D': {'C': 4, + 'B': 3, + 'F': 10, + 'G': 15 + }, + 'E': {'F': 8, + 'D': 8 + }, + 'F': {'G': 5 + }, + 'G': {} +} + +test_graph = { + 'A': {'B': 5, + 'C': 6 + }, + 'B': {'D': 2 + }, + 'C': {'E': 8, + 'F': 4 + }, + 'D': {'E': 2, + 'G': 10 + }, + 'E': {'G': 10, + 'F': 4 + }, +} + + +def test_dijkstra_returns_best_parents(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = test_graph + path = dijkstra(g, 'A', 'E') + assert path == {'B': 'A', 'C': 'A', 'D': 'B', 'E': 'D', 'F': 'C', 'G': 'D'} + + +def test_dijkstra_returns_best_parents_on_diffrent_path(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = test_graph + path = dijkstra(g, 'A', 'F') + assert path == {'B': 'A', 'C': 'A', 'D': 'B', 'E': 'D', 'F': 'C', 'G': 'D'} + + +def test_dijkstra_returns_best_parents_of_the_node(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = graph + path = dijkstra(g, 'A', 'E') + assert path == {'B': 'A', 'C': 'A', 'D': 'A', 'E': 'B', 'F': 'D', 'G': 'D'} + + +def test_shortest_distance(): + """Test for Shortest path from start node to end node.""" + g = Graph() + g._graph = graph + path = shortest_distance(g, 'A', 'F') + assert path == ['A', 'D', 'F'] + + +def test_shortest_distance_returns_list(): + """Test for Shortest path from start node to end node.""" + g = Graph() + g._graph = test_graph + path = shortest_distance(g, 'A', 'E') + assert path == ['A', 'B', 'D', 'E'] + + +def test_shortest_distance_returns_list_from_path_a_g(): + """Test for Shortest path from start node to end node.""" + g = Graph() + g._graph = test_graph + path = shortest_distance(g, 'A', 'G') + assert path == ['A', 'B', 'D', 'G'] + + +def test_dijkstra_that_start_no_children_return_emty_dict(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = graph + path = dijkstra(g, 'G', 'E') + assert path == {} + + +def test_shortest_distance_raises_valueerror_if_start_has_no_edges(): + """Test shortest distance raises ValueError if start has no edges.""" + g = Graph() + g._graph = test_graph + with pytest.raises(ValueError): + shortest_distance(g, 'G', 'A') From b467ebc3b3b43a44c8fd254bcfc87487fc00521c Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Sun, 19 Nov 2017 15:42:02 -0800 Subject: [PATCH 26/40] updated readme --- README.md | 12 ++++++++++ src/shortest_dis.py | 47 ---------------------------------------- src/test_shortest_dis.py | 3 ++- 3 files changed, 14 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index c8dd3d7..d649b82 100644 --- a/README.md +++ b/README.md @@ -221,4 +221,16 @@ new.adjacent(val1, val2) new.depth_first_traversal(start_val) new.breadth_first_traversal(start_val) +``` + +### Shortest Distance Problem + +``` +The problem of the traveling salesperson finding the shortest distance between points can be solved in a variety of ways. We have used Dijkstra's Algorith as our first method to a solution. + +_Dijkstra's Algorithm_ (On) - finds the shortest path exploring all edges of a graph from it's starting point. By comparing the distance or weight of the edges from the start to each child as it traverses the graph it is able to calculate the shortest route by saving the parent path which will have the lowest cost from start to finish. + +To use this function: +dijkstra(graph, start, end) + ``` \ No newline at end of file diff --git a/src/shortest_dis.py b/src/shortest_dis.py index 4f127c4..0c40a95 100644 --- a/src/shortest_dis.py +++ b/src/shortest_dis.py @@ -1,51 +1,4 @@ """Using Dijkstra's algorithm to solve the shortest path.""" -from graph import Graph - - -graph = { - 'A': {'C': 4, - 'B': 2, - 'D': 5 - }, - 'C': {'G': 20, - 'D': 8 - }, - 'B': {'E': 8 - }, - 'D': {'C': 4, - 'B': 3, - 'F': 10, - 'G': 15 - }, - 'E': {'F': 8, - 'D': 8 - }, - 'F': {'G': 5 - }, - 'G': {} -} - -test_graph = { - 'A': {'B': 5, - 'C': 6 - }, - 'B': {'D': 2 - }, - 'C': {'E': 8, - 'F': 4 - }, - 'D': {'E': 2, - 'G': 10 - }, - 'E': {'G': 10, - 'F': 4 - }, -} - -g = Graph() -tg = Graph() -tg._graph = test_graph -g._graph = graph def dijkstra(graph, start, end): diff --git a/src/test_shortest_dis.py b/src/test_shortest_dis.py index 8648f6f..5498915 100644 --- a/src/test_shortest_dis.py +++ b/src/test_shortest_dis.py @@ -1,7 +1,8 @@ """Test for Shorest distance graph.""" +from graph import Graph + import pytest -from graph import Graph from shortest_dis import dijkstra, shortest_distance From 59540c5eb5e62493dcc35f1e607e60f3af79fb61 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Mon, 20 Nov 2017 18:01:31 -0800 Subject: [PATCH 27/40] insert, search, size done in bst --- src/bst.py | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ src/test_bst.py | 0 2 files changed, 101 insertions(+) create mode 100644 src/bst.py create mode 100644 src/test_bst.py diff --git a/src/bst.py b/src/bst.py new file mode 100644 index 0000000..af2795a --- /dev/null +++ b/src/bst.py @@ -0,0 +1,101 @@ +"""Binary Search Tree.""" + + +class Node(object): + """Node object for bst.""" + def __init__(self, data, lc=None, rc=None) + self.data = data + self.left_child = lc + self.right_child = rc + + +class BST(object): + """Binary Search Tree class.""" + + def __init__(self, iterable=None): + """Initiate a new instance of bst.""" + self._tree = {} + self.root = None + self._size = 0 + if isinstance(iterable, (list, tuple)): + for item in iterable: + self.insert(item) + + def insert(self, val): + """Insert a value into bst.""" + if self.root = None: + self.root = Node(val) + self._size += 1 + if val == self.root: + raise ValueError('This node already exists.') + if val < self.root: + current = self.root.left_child + while current != None: + if val == current: + raise ValueError('This node already exists.') + if val < current: + current = current.left_child + if val > current: + current = current.right_child + if val < current: + current.left_child = Node(val) + self._size += 1 + if val > current: + current.right_child = Node(val) + self._size += 1 + if val > self.root: + current = self.root.right_child + while current != None: + if val == current: + raise ValueError('This node already exists.') + if val < current: + current = current.left_child + if val > current: + current = current.right_child + if val < current: + current.left_child = Node(val) + self._size += 1 + if val > current: + current.right_child = Node(val) + self._size += 1 + + def search(self, val): + """Search bst for val and return node of this val, else none.""" + if val == self.root: + return self.root + if val < self.root: + current = self.root.left_child + while current != None: + if val == current: + return current + if val < current: + current = current.left_child + if val > current: + current = current.right_child + if val > self.root: + current = self.root.right_child + while current != None: + if val == current: + return current + if val < current: + current = current.left_child + if val > current: + current = current.right_child + else: + return None + + def size(self): + """Returns size of bst.""" + return self._size + + def depth(self): + """Returns total number of levels in bst.""" + right = 0 + left = 0 + depth = 0 + current = self.root + while current != None: + if current.left_child: + current = current.left_child + left += 1 + \ No newline at end of file diff --git a/src/test_bst.py b/src/test_bst.py new file mode 100644 index 0000000..e69de29 From 4fe47528f2e5d4504f01db41a3328492c080d7c9 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Mon, 20 Nov 2017 22:04:14 -0800 Subject: [PATCH 28/40] rough bst completed, no tests --- src/bst.py | 71 ++++++++++++++++++++++++++++++------------------- src/test_bst.py | 2 ++ 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/bst.py b/src/bst.py index af2795a..7e8623a 100644 --- a/src/bst.py +++ b/src/bst.py @@ -5,8 +5,8 @@ class Node(object): """Node object for bst.""" def __init__(self, data, lc=None, rc=None) self.data = data - self.left_child = lc - self.right_child = rc + self.left = lc + self.right = rc class BST(object): @@ -29,34 +29,34 @@ def insert(self, val): if val == self.root: raise ValueError('This node already exists.') if val < self.root: - current = self.root.left_child + current = self.root.left while current != None: if val == current: raise ValueError('This node already exists.') if val < current: - current = current.left_child + current = current.left if val > current: - current = current.right_child + current = current.right if val < current: - current.left_child = Node(val) + current.left = Node(val) self._size += 1 if val > current: - current.right_child = Node(val) + current.right = Node(val) self._size += 1 if val > self.root: - current = self.root.right_child + current = self.root.right while current != None: if val == current: raise ValueError('This node already exists.') if val < current: - current = current.left_child + current = current.left if val > current: - current = current.right_child + current = current.right if val < current: - current.left_child = Node(val) + current.left = Node(val) self._size += 1 if val > current: - current.right_child = Node(val) + current.right = Node(val) self._size += 1 def search(self, val): @@ -64,23 +64,23 @@ def search(self, val): if val == self.root: return self.root if val < self.root: - current = self.root.left_child + current = self.root.left while current != None: if val == current: return current if val < current: - current = current.left_child + current = current.left if val > current: - current = current.right_child + current = current.right if val > self.root: - current = self.root.right_child + current = self.root.right while current != None: if val == current: return current if val < current: - current = current.left_child + current = current.left if val > current: - current = current.right_child + current = current.right else: return None @@ -88,14 +88,29 @@ def size(self): """Returns size of bst.""" return self._size - def depth(self): + def depth(self, root=self.root): """Returns total number of levels in bst.""" - right = 0 - left = 0 - depth = 0 - current = self.root - while current != None: - if current.left_child: - current = current.left_child - left += 1 - \ No newline at end of file + if root is None: + return 0 + if not root.left and not root.right: + return 1 + elif root.left and not root.right: + return depth(root.left) + 1 + elif root.right and not root.left: + return depth(root.right) + 1 + else: + return max(depth(root.left), depth(root.right)) + 1 + + def contains(self, val): + """Search for val in tree and return boolean.""" + return self.search(val) + + def balance(self): + """Returns integer indicating balance of tree.""" + if self.root is None: + return 'There are no nodes in this tree.' + return depth(self.root.right) - depth(self.root.left) + + + + diff --git a/src/test_bst.py b/src/test_bst.py index e69de29..369ae2e 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -0,0 +1,2 @@ +"""Test binary search tree.""" + From cf5f4ecf545216567b53fbd8ae0892e163166b68 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Tue, 21 Nov 2017 00:27:18 -0800 Subject: [PATCH 29/40] tested bst and all tests pass --- README.md | 25 +++++++ src/bst.py | 185 ++++++++++++++++++++++++++++++------------------ src/conftest.py | 36 ++++++++++ src/test_bst.py | 158 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 336 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index d649b82..25fdaa0 100644 --- a/README.md +++ b/README.md @@ -233,4 +233,29 @@ _Dijkstra's Algorithm_ (On) - finds the shortest path exploring all edges of a g To use this function: dijkstra(graph, start, end) +``` + +### Binary Search Tree + +``` +To create an instance if the binary search tree, Bst(), from python: + +new = Bst() *you may not call Bst() no parameters or with an iterable item containing unique integers.* + +Bst() contains the following methods: +* _insert(val) (Olog(n))_ - will insert the value ‘val’ in the appropriate position in the tree. +* _search(val) (0log(n))_ - will find a given value in the tree, if not in tree will return none. +* _size() (O(1))_ - will return the size of the tree. +* _depth(root) (0(log(n)))_ - returns the depth of the tree. +* _contains(val) (Olog(n))_ - will return true or false if value is in tree. +*_ balance() (0(n))_ - returns the depth of the left minus the right side of the tree as an integer. + +To access any contained methods: +new.insert(val) +new.search(val) +new.size() +new.depth(root) +new.contains(val) +new.balance() + ``` \ No newline at end of file diff --git a/src/bst.py b/src/bst.py index 7e8623a..11a72a5 100644 --- a/src/bst.py +++ b/src/bst.py @@ -3,18 +3,19 @@ class Node(object): """Node object for bst.""" - def __init__(self, data, lc=None, rc=None) + + def __init__(self, data, lc=None, rc=None): + """Initialize a new node.""" self.data = data self.left = lc self.right = rc -class BST(object): +class Bst(object): """Binary Search Tree class.""" def __init__(self, iterable=None): """Initiate a new instance of bst.""" - self._tree = {} self.root = None self._size = 0 if isinstance(iterable, (list, tuple)): @@ -23,94 +24,142 @@ def __init__(self, iterable=None): def insert(self, val): """Insert a value into bst.""" - if self.root = None: + if not isinstance(val, int): + raise ValueError('Sorry, I only take numbers right now.') + if self.root is None: self.root = Node(val) self._size += 1 - if val == self.root: + elif val == self.root.data: raise ValueError('This node already exists.') - if val < self.root: - current = self.root.left - while current != None: - if val == current: - raise ValueError('This node already exists.') - if val < current: - current = current.left - if val > current: - current = current.right - if val < current: - current.left = Node(val) - self._size += 1 - if val > current: - current.right = Node(val) - self._size += 1 - if val > self.root: - current = self.root.right - while current != None: - if val == current: - raise ValueError('This node already exists.') - if val < current: - current = current.left - if val > current: - current = current.right - if val < current: - current.left = Node(val) - self._size += 1 - if val > current: - current.right = Node(val) - self._size += 1 + elif val < self.root.data: + if self.root.left: + current = self.root.left + while current: + if val == current.data: + raise ValueError('This node already exists.') + elif val < current.data: + if current.left: + current = current.left + else: + current.left = Node(val) + self._size += 1 + break + elif val > current.data: + if current.right: + current = current.right + else: + current.right = Node(val) + self._size += 1 + break + else: + if val < self.root.data: + self.root.left = Node(val) + self._size += 1 + if val > self.root.data: + self.root.right = Node(val) + self._size += 1 + elif val > self.root.data: + if self.root.right: + current = self.root.right + while current: + if val == current.data: + raise ValueError('This node already exists.') + elif val < current.data: + if current.left: + current = current.left + else: + current.left = Node(val) + self._size += 1 + break + elif val > current.data: + if current.right: + current = current.right + else: + current.right = Node(val) + self._size += 1 + break + else: + if val < self.root.data: + self.root.left = Node(val) + self._size += 1 + if val > self.root.data: + self.root.right = Node(val) + self._size += 1 def search(self, val): """Search bst for val and return node of this val, else none.""" - if val == self.root: + if self.root is None or not isinstance(val, int): + return None + if val == self.root.data: return self.root - if val < self.root: - current = self.root.left - while current != None: - if val == current: - return current - if val < current: - current = current.left - if val > current: - current = current.right - if val > self.root: - current = self.root.right - while current != None: - if val == current: - return current - if val < current: - current = current.left - if val > current: - current = current.right + if val < self.root.data: + if self.root.left: + current = self.root.left + while current: + if val == current.data: + return current + elif val < current.data: + try: + current = current.left + except AttributeError: + break + elif val > current.data: + try: + current = current.right + except AttributeError: + break + if val > self.root.data: + if self.root.right: + current = self.root.right + while current: + if val == current.data: + return current + elif val < current.data: + try: + current = current.left + except AttributeError: + break + elif val > current.data: + try: + current = current.right + except AttributeError: + break else: return None def size(self): - """Returns size of bst.""" + """Return size of bst.""" return self._size - def depth(self, root=self.root): - """Returns total number of levels in bst.""" + def depth(self, root): + """Return total number of levels in bst.""" if root is None: return 0 if not root.left and not root.right: - return 1 + return 0 elif root.left and not root.right: - return depth(root.left) + 1 + return self.depth(root.left) + 1 elif root.right and not root.left: - return depth(root.right) + 1 + return self.depth(root.right) + 1 else: - return max(depth(root.left), depth(root.right)) + 1 + return max(self.depth(root.left), self.depth(root.right)) + 1 def contains(self, val): """Search for val in tree and return boolean.""" - return self.search(val) + if self.search(val) is not None: + return True + else: + return False def balance(self): - """Returns integer indicating balance of tree.""" + """Return integer indicating balance of tree.""" if self.root is None: return 'There are no nodes in this tree.' - return depth(self.root.right) - depth(self.root.left) - - - - + elif not self.root.left and not self.root.right: + return 0 + elif self.root.left and not self.root.right: + return self.depth(self.root.left) + elif self.root.right and not self.root.left: + return self.depth(self.root.right) + else: + return self.depth(self.root.right) - self.depth(self.root.left) diff --git a/src/conftest.py b/src/conftest.py index 81d0af3..07f0426 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -21,3 +21,39 @@ def g(): """Initialize a empty pq.""" from graph import Graph return Graph() + + +@pytest.fixture +def bst(): + """Initialize empty bst for tests.""" + from bst import Bst + return Bst() + + +@pytest.fixture +def bst_full(): + """Create bst will values for tests.""" + from bst import Bst + new = Bst() + new.insert(3254) + new.insert(908543) + new.insert(58490543) + return new + + +@pytest.fixture +def bst_big(): + """Create bst will values for tests.""" + from bst import Bst + new = Bst() + new.insert(50) + new.insert(40) + new.insert(68) + new.insert(10) + new.insert(110) + new.insert(1) + new.insert(500) + new.insert(18) + new.insert(80) + new.insert(5000) + return new diff --git a/src/test_bst.py b/src/test_bst.py index 369ae2e..30acfc0 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -1,2 +1,160 @@ """Test binary search tree.""" +import pytest + +def test_initialize_bst_returns_empty_bst(bst): + """Test initialize empty bst returns empty bst.""" + assert bst.root is None + + +def test_initialize_bst_iteratble_returns_size_n(): + """Test initialize with iterable returns proper size tree.""" + from bst import Bst + tree = Bst([2, 6, 3, 7, 8, 1]) + assert tree._size == 6 + + +def test_initialize_bst_iteratble_root_is_first_item(): + """Test initialize with iterable root is first item in.""" + from bst import Bst + tree = Bst([2, 6, 3, 7, 8, 1]) + assert tree.root.data == 2 + + +def test_size_returns_0_if_bst_is_empty(bst): + """Test size method returns 0 on empty tree.""" + assert bst.size() == 0 + + +def test_size_returns_appropriate_size_after_iterable(): + """Test size returns proper size when initiated by iterable.""" + from bst import Bst + tree = Bst([2, 6, 3, 7, 8, 1]) + assert tree.size() == 6 + + +def test_size_returns_10_if_insert_manually_10_items(bst_big): + """Test if 10 items inserted that size is 10.""" + assert bst_big.size() == 10 + + +def test_insert_10_items_returns_first_in_as_root(bst_big): + """Test that root is first in if 10 items inserted.""" + assert bst_big.root.data == 50 + + +def test_insert_identicle_values_raise_valueerror(bst): + """Test insert same values raises value error.""" + bst.insert(1) + with pytest.raises(ValueError): + bst.insert(1) + + +def test_insert_non_int_raises_valueerror(bst): + """Test insert non number raises value error.""" + with pytest.raises(ValueError): + bst.insert('howdy') + + +def test_insert_iterable_raises_valueerror(bst): + """Test insert iterable raises appropriate error.""" + with pytest.raises(ValueError): + bst.insert([1, 3, 5, 9]) + + +def test_search_node_on_empty_tree_returns_none(bst): + """Test search on empty tree returns none.""" + assert bst.search(10) is None + + +def test_search_node_not_in_tree_returns_none(bst_full): + """Test search for node not in tree returns none.""" + assert bst_full.search(1) is None + + +def test_search_for_non_int_returns_non(bst_full): + """Test search for non number returns none.""" + assert bst_full.search('cake') is None + + +def test_search_val_in_tree_returns_node(bst_full): + """Test search for val in tree returns node.""" + bst_full.insert(5) + assert isinstance(bst_full.search(5), object) + + +def test_search_val_in_tree_retruns_node_with_data(bst_full): + """Test search for val in tree returns node with data of val.""" + bst_full.insert(5) + assert bst_full.search(5).data == 5 + + +def test_depth_returns_int_of_total_depth_of_tree(bst_full): + """Test depth returns integer of depth of tree.""" + assert bst_full.depth(bst_full.root) == 2 + + +def test_depth_empty_tree_returns_none(bst): + """Test depth on epmty tree returns None.""" + assert bst.depth(bst.root) == 0 + + +def test_depth_on_large_tree_returns_full_size(bst_big): + """Test depth on large tree returns actual size.""" + assert bst_big.depth(bst_big.root) == 4 + + +def test_depth_of_tree_with_only_root_is_0(bst): + """Test if only root in tree that depth is 0.""" + bst.insert(1) + assert bst.depth(bst.root) == 0 + + +def test_contains_returns_false_if_val_not_in_tree(bst_big): + """Test contains returns false if val not in tree.""" + assert bst_big.contains(102948686) is False + + +def test_contains_returns_false_if_non_int_entered(bst_big): + """"Test contains returns false if non int entered.""" + assert bst_big.contains('pie') is False + + +def test_contains_returns_true_if_val_in_tree(bst_big): + """Test contains returns true if val in tree.""" + assert bst_big.contains(40) is True + assert bst_big.contains(50) is True + assert bst_big.contains(68) is True + + +def test_balance_returns_string_if_bst_is_empty(bst): + """Test balance returns none if bst is empty.""" + assert bst.balance() == 'There are no nodes in this tree.' + + +def test_balance_returns_int(bst_big): + """Test balancde returns int..""" + assert isinstance(bst_big.balance(), int) + + +def test_balance_returns_int_of_r_minus_l_of_tree(bst_big): + """Test balance returns int of left minus right sides of tree.""" + assert bst_big.balance() == 1 + + +def test_balance_returns_int_of_r_minus_l_of_tree_two(bst_full, bst): + """Test balance returns int of left minus right sides of tree.""" + bst.insert(2) + bst.insert(1) + bst.insert(3) + assert bst.balance() == 0 + assert bst_full.balance() == 1 + + +def test_balance_returns_int_of_r_minus_l_of_tree_three(bst): + """Test balance returns int of left minus right sides of tree.""" + bst.insert(1) + bst.insert(2) + bst.insert(3) + bst.insert(4) + assert bst.balance() == 2 From 672e83e92b2d466c5f88bf4c9a6c2a3b9cf286aa Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Tue, 21 Nov 2017 09:56:59 -0800 Subject: [PATCH 30/40] added if name is main --- src/bst.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/bst.py b/src/bst.py index 11a72a5..5fc7ce3 100644 --- a/src/bst.py +++ b/src/bst.py @@ -163,3 +163,25 @@ def balance(self): return self.depth(self.root.right) else: return self.depth(self.root.right) - self.depth(self.root.left) + + +if __name__ == '__main__': # pragma: no cover + import timeit + bst_ub = Bst([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) + insert_time_ub = timeit.timeit(bst_ub.insert(17)) + search_time_ub = timeit.timeit(bst_ub.search(16)) + search_time_root_ub = timeit.timeit(bst_ub.search(1)) + bst_b = Bst([10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19, 2]) + insert_time_b = timeit.timeit(bst_b.insert(20)) + search_time_b = timeit.timeit(bst_b.search(2)) + search_time_root_b = timeit.timeit(bst_b.search(10)) + print('The following times relate to the unbalanced binary search' + 'tree:\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].') + print('Insert 17: {}'.format(insert_time_ub)) + print('Search 16: {}'.format(search_time_ub)) + print('Search 1: {}'.format(search_time_root_ub)) + print('The following times relate to the unbalanced binary search tree' + ':\n[10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19, 2].') + print('Insert 20: {}'.format(insert_time_b)) + print('Search 19: {}'.format(search_time_b)) + print('Search 10: {}'.format(search_time_root_b)) From 3230112f0a5701cc334d0babbeca97885118735e Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Tue, 21 Nov 2017 11:37:53 -0800 Subject: [PATCH 31/40] refactored insert and search --- src/bst.py | 119 +++++++++++++---------------------------------------- 1 file changed, 28 insertions(+), 91 deletions(-) diff --git a/src/bst.py b/src/bst.py index 5fc7ce3..63c34d0 100644 --- a/src/bst.py +++ b/src/bst.py @@ -24,108 +24,45 @@ def __init__(self, iterable=None): def insert(self, val): """Insert a value into bst.""" - if not isinstance(val, int): + if not isinstance(val, (int, float)): raise ValueError('Sorry, I only take numbers right now.') if self.root is None: self.root = Node(val) self._size += 1 + return elif val == self.root.data: raise ValueError('This node already exists.') - elif val < self.root.data: - if self.root.left: - current = self.root.left - while current: - if val == current.data: - raise ValueError('This node already exists.') - elif val < current.data: - if current.left: - current = current.left - else: - current.left = Node(val) - self._size += 1 - break - elif val > current.data: - if current.right: - current = current.right - else: - current.right = Node(val) - self._size += 1 - break - else: - if val < self.root.data: - self.root.left = Node(val) + current = self.root + while current: + if val == current.data: + raise ValueError('This node already exists.') + elif val < current.data: + if current.left: + current = current.left + else: + current.left = Node(val) self._size += 1 - if val > self.root.data: - self.root.right = Node(val) - self._size += 1 - elif val > self.root.data: - if self.root.right: - current = self.root.right - while current: - if val == current.data: - raise ValueError('This node already exists.') - elif val < current.data: - if current.left: - current = current.left - else: - current.left = Node(val) - self._size += 1 - break - elif val > current.data: - if current.right: - current = current.right - else: - current.right = Node(val) - self._size += 1 - break - else: - if val < self.root.data: - self.root.left = Node(val) - self._size += 1 - if val > self.root.data: - self.root.right = Node(val) + break + elif val > current.data: + if current.right: + current = current.right + else: + current.right = Node(val) self._size += 1 + break def search(self, val): """Search bst for val and return node of this val, else none.""" - if self.root is None or not isinstance(val, int): - return None - if val == self.root.data: - return self.root - if val < self.root.data: - if self.root.left: - current = self.root.left - while current: - if val == current.data: - return current - elif val < current.data: - try: - current = current.left - except AttributeError: - break - elif val > current.data: - try: - current = current.right - except AttributeError: - break - if val > self.root.data: - if self.root.right: - current = self.root.right - while current: - if val == current.data: - return current - elif val < current.data: - try: - current = current.left - except AttributeError: - break - elif val > current.data: - try: - current = current.right - except AttributeError: - break - else: - return None + if self.root is None or not isinstance(val, (int, float)): + return + current = self.root + while current: + if val == current.data: + return current + elif val < current.data: + current = current.left + elif val > current.data: + current = current.right def size(self): """Return size of bst.""" From 1e41236c43c99652149b4407e8c41ff46f3cf2a1 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Tue, 21 Nov 2017 18:02:17 -0800 Subject: [PATCH 32/40] timeit works once --- src/bst.py | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/bst.py b/src/bst.py index 63c34d0..714c696 100644 --- a/src/bst.py +++ b/src/bst.py @@ -105,20 +105,29 @@ def balance(self): if __name__ == '__main__': # pragma: no cover import timeit bst_ub = Bst([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) - insert_time_ub = timeit.timeit(bst_ub.insert(17)) - search_time_ub = timeit.timeit(bst_ub.search(16)) - search_time_root_ub = timeit.timeit(bst_ub.search(1)) - bst_b = Bst([10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19, 2]) - insert_time_b = timeit.timeit(bst_b.insert(20)) - search_time_b = timeit.timeit(bst_b.search(2)) - search_time_root_b = timeit.timeit(bst_b.search(10)) - print('The following times relate to the unbalanced binary search' - 'tree:\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].') - print('Insert 17: {}'.format(insert_time_ub)) - print('Search 16: {}'.format(search_time_ub)) - print('Search 1: {}'.format(search_time_root_ub)) - print('The following times relate to the unbalanced binary search tree' - ':\n[10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19, 2].') - print('Insert 20: {}'.format(insert_time_b)) - print('Search 19: {}'.format(search_time_b)) - print('Search 10: {}'.format(search_time_root_b)) + bst_b = Bst([10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19]) + + # insert_time_ub = wrapper(bst_ub.insert, 17) + # search_time_ub = wrapper(bst_ub.search, 16) + # search_time_root_ub = wrapper(bst_ub.search, 1) + # insert_time_b = wrapper(bst_b.insert, 20) + # search_time_b = wrapper(bst_b.search, 2) + # search_time_root_b = wrapper(bst_b.search, 10) + num = (x for x in range(17, 1000)) + a = timeit.timeit('bst_ub.insert(next(num))', + setup='from __main__ import bst_ub, num', + number=983) + # b = timeit.timeit(search_time_ub, number=1000) + # c = timeit.timeit(search_time_root_ub, number=1000) + # d = timeit.timeit(insert_time_b, number=1000) + # e = timeit.timeit(search_time_b, number=1000) + # f = timeit.timeit(search_time_root_b, number=1000) + print('The following times relate worst case insert.') + print('Insert: {}'.format(a)) + # print('Search 16: {}'.format(b)) + # print('Search 1: {}'.format(c)) + # print('The following times relate to the unbalanced binary search tree' + # ':\n[10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19, 2].') + # print('Insert 20: {}'.format(d)) + # print('Search 19: {}'.format(e)) + # print('Search 10: {}'.format(f)) From 6826163edd5660eef64b8a37481d259b3a5640f5 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Tue, 21 Nov 2017 21:24:50 -0800 Subject: [PATCH 33/40] added print times comparisons --- README.md | 2 +- src/bst.py | 78 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 25fdaa0..e7c7674 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ Bst() contains the following methods: * _insert(val) (Olog(n))_ - will insert the value ‘val’ in the appropriate position in the tree. * _search(val) (0log(n))_ - will find a given value in the tree, if not in tree will return none. * _size() (O(1))_ - will return the size of the tree. -* _depth(root) (0(log(n)))_ - returns the depth of the tree. +* _depth(root) (0(n))_ - returns the depth of the tree. * _contains(val) (Olog(n))_ - will return true or false if value is in tree. *_ balance() (0(n))_ - returns the depth of the left minus the right side of the tree as an integer. diff --git a/src/bst.py b/src/bst.py index 714c696..1b66dc6 100644 --- a/src/bst.py +++ b/src/bst.py @@ -104,30 +104,54 @@ def balance(self): if __name__ == '__main__': # pragma: no cover import timeit - bst_ub = Bst([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) - bst_b = Bst([10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19]) - - # insert_time_ub = wrapper(bst_ub.insert, 17) - # search_time_ub = wrapper(bst_ub.search, 16) - # search_time_root_ub = wrapper(bst_ub.search, 1) - # insert_time_b = wrapper(bst_b.insert, 20) - # search_time_b = wrapper(bst_b.search, 2) - # search_time_root_b = wrapper(bst_b.search, 10) - num = (x for x in range(17, 1000)) - a = timeit.timeit('bst_ub.insert(next(num))', - setup='from __main__ import bst_ub, num', - number=983) - # b = timeit.timeit(search_time_ub, number=1000) - # c = timeit.timeit(search_time_root_ub, number=1000) - # d = timeit.timeit(insert_time_b, number=1000) - # e = timeit.timeit(search_time_b, number=1000) - # f = timeit.timeit(search_time_root_b, number=1000) - print('The following times relate worst case insert.') - print('Insert: {}'.format(a)) - # print('Search 16: {}'.format(b)) - # print('Search 1: {}'.format(c)) - # print('The following times relate to the unbalanced binary search tree' - # ':\n[10, 14, 7, 12, 8, 16, 6, 11, 4, 15, 5, 18, 2, 17, 3, 19, 2].') - # print('Insert 20: {}'.format(d)) - # print('Search 19: {}'.format(e)) - # print('Search 10: {}'.format(f)) + + insert_time_ub = Bst() + num = (x for x in range(1000)) + a = timeit.timeit('insert_time_ub.insert(next(num))', + setup='from __main__ import insert_time_ub, num', + number=1000) + search_time_ub = Bst() + for i in range(100): + search_time_ub.insert(i) + b = timeit.timeit('search_time_ub.search(99)', + setup='from __main__ import search_time_ub', + number=1000) + c = timeit.timeit('search_time_ub.search(0)', + setup='from __main__ import search_time_ub', + number=1000) + insert_time_b = Bst() + + def insert_time(val): + """.""" + if (500 + val) % 2 == 0: + insert_time_b.insert(500 + val) + else: + insert_time_b.insert(500 - val) + + num_b = (x for x in range(1000)) + d = timeit.timeit('insert_time(next(num_b))', + setup='from __main__ import insert_time, num_b', + number=1000) + + search_time_b = Bst() + for i in range(1000): + if (500 + i) % 2 == 0: + search_time_b.insert(500 + i) + else: + search_time_b.insert(500 - i) + e = timeit.timeit('search_time_b.search(999)', + setup='from __main__ import search_time_b', + number=1000) + f = timeit.timeit('search_time_b.search(500)', + setup='from __main__ import search_time_b', + number=1000) + + print('The following time relates to worst case insert.') + print('Insert unbalanced: {}'.format(a)) + print('Insert balanced: {}'.format(d)) + print('\nThe following time relates to worst case search.') + print('Search unbalanced leaf: {}'.format(b)) + print('Search balanced leaf: {}'.format(e)) + print('\nThe following time relates to best base search.') + print('Search unbalanced head: {}'.format(c)) + print('Search balanced head: {}'.format(f)) From 7c30144a43c714713cab5d9e6c0a21302228f61a Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 22 Nov 2017 16:29:45 -0800 Subject: [PATCH 34/40] breadth and in order --- src/bst.py | 96 +++++++++++++++++++++++++++++- src/doubly_linked_list.py | 121 ++++++++++++++++++++++++++++++++++++++ src/que_.py | 36 ++++++++++++ src/test_bst.py | 28 +++++++++ 4 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 src/doubly_linked_list.py create mode 100644 src/que_.py diff --git a/src/bst.py b/src/bst.py index 1b66dc6..ac761f4 100644 --- a/src/bst.py +++ b/src/bst.py @@ -68,10 +68,13 @@ def size(self): """Return size of bst.""" return self._size - def depth(self, root): + def depth(self, root=None): """Return total number of levels in bst.""" if root is None: - return 0 + if self.root is None: + return 0 + else: + root = self.root if not root.left and not root.right: return 0 elif root.left and not root.right: @@ -101,6 +104,95 @@ def balance(self): else: return self.depth(self.root.right) - self.depth(self.root.left) + def breadth_first(self): + """Return generator of breadth first search.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') + bfs = [self.root] + while bfs: + current = bfs.pop(0) + if current.left: + bfs.append(current.left) + if current.right: + bfs.append(current.right) + yield current.data + + def in_order(self): + """Return generator of in order search.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') + + current = self.root + ios = [] + while current or ios: + if current: + ios.append(current) + current = current.left + else: + current = ios.pop() + yield current.data + current = current.right + + def pre_order(self): + """Return generator of pre order search.""" + pass + + def post_order(self): + """Return generator of post order wearch.""" + pass + + +# notes +class Solution: + """ + @param root: The root of binary tree. + @return: Preorder in ArrayList which contains node values. + """ + def preorderTraversal(self, root): + ret = [] + + stack = [] + while stack or root: + if root: + ret.append(root.val) + if root.right: + stack.append(root.right) + root = root.left + else: + root = stack.pop() + return ret + + def postorderTraversal(self, root): + ret = [] + stack = [] + + last = None + while stack or root: + if root: + stack.append(root) + root = root.left + else: + if stack[-1].right and stack[-1].right is not last: + root = stack[-1].right + else: + last = stack.pop() + ret.append(last.val) + return ret + + def inorderTraversal(self, root): + ret = [] + stack = [] + while stack or root: + if root: + stack.append(root) + root = root.left + else: + root = stack.pop() + ret.append(root.val) + root = root.right + return ret + + if __name__ == '__main__': # pragma: no cover import timeit diff --git a/src/doubly_linked_list.py b/src/doubly_linked_list.py new file mode 100644 index 0000000..48b03dd --- /dev/null +++ b/src/doubly_linked_list.py @@ -0,0 +1,121 @@ +"""Create a new instance of a doubly linked list.""" + + +class Node(object): + """Create new instance of Node class.""" + + def __init__(self, data=None, prve=None, next=None, tail=None): + """Initiate new Node with no defined values.""" + self.data = data + self.next = next + self.prve = prve + + def get_prve(self): + """Get previous node.""" + return self.prve + + def get_data(self): + """Get data of current node.""" + return self.data + + def get_next(self): + """Get next node.""" + return self.next + + def set_next(self, new_next): + """Set next node.""" + self.next = new_next + + def set_prve(self, new_prve): + """Set previous node.""" + self.prve = new_prve + + +class Dll(object): + """Create instance of doubly linked link class.""" + + def __init__(self): + """Initialize doubly linked list with no default values.""" + self.head = None + self.tail = None + self._size = 0 + + def push(self, val): + """Push value to top of list.""" + new_node = Node(val) + if self._size < 1: + self.head = new_node + self.tail = new_node + else: + new_node.set_next(self.head) + self.head.set_prve(new_node) + self.head = new_node + self._size += 1 + + def append(self, val): + """Append value to tail of list.""" + new_node = Node(val) + if self._size < 1: + self.head = new_node + self.tail = new_node + else: + new_node.set_prve(self.tail) + self.tail.set_next(new_node) + self.tail = new_node + self._size += 1 + + def pop(self): + """Remove current head of list.""" + current = self.head + if self.head is None: + raise IndexError('This is an empty list. No values to pop.') + elif self._size == 1: + self.tail = None + self.head = None + self._size -= 1 + elif self._size > 1: + self.head = current.get_next() + self.head.set_prve(None) + self._size -= 1 + return current.data + + def shift(self): + """Remove current tail of list.""" + current = self.tail + if self.tail is None: + raise IndexError('This is an empty list. No values to shift.') + elif self._size == 1: + self.tail = None + self.head = None + self._size -= 1 + elif self._size > 1: + self.tail = current.get_prve() + self.tail.set_next(None) + self._size -= 1 + return current.data + + def remove(self, val): + """Remove inputted value.""" + current = self.head + while current: + if current.get_data() == val: + if current == self.head: + self.pop() + return None + if current == self.tail: + self.shift() + return None + else: + npn = current.get_prve() + nnn = current.get_next() + npn.set_next(nnn) + nnn.set_prve(npn) + self._size -= 1 + return None + current = current.get_next() + else: + raise ValueError('Your node does not exist in this linked list.') + + def __len__(self): + """Display size of list.""" + return self._size diff --git a/src/que_.py b/src/que_.py new file mode 100644 index 0000000..9e0510c --- /dev/null +++ b/src/que_.py @@ -0,0 +1,36 @@ +"""Create queue class composed from instance of Dll().""" +from doubly_linked_list import Dll + + +class Queue(object): + """Simple class from Dll() instance.""" + + def __init__(self): + """Initiate an empty stack.""" + self.func = Dll() + + def enqueue(self, val): + """Push an item into stack.""" + return self.func.push(val) + + def dequeue(self): + """Remove first item pushed into stack.""" + try: + return self.func.shift().data + except IndexError: + raise IndexError('There are no nodes to dequeue.') + + def peek(self): + """Return the value of the next node to be removed.""" + try: + return self.func.tail.data + except AttributeError: + return None + + def size(self): + """Return the size.""" + return self.func._size + + def __len__(self): + """Print length of stack.""" + return self.func._size diff --git a/src/test_bst.py b/src/test_bst.py index 30acfc0..3f10040 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -158,3 +158,31 @@ def test_balance_returns_int_of_r_minus_l_of_tree_three(bst): bst.insert(3) bst.insert(4) assert bst.balance() == 2 + + +def test_breadth_first_returns_object(bst_big): + """Test breadth first returns generator object.""" + b = bst_big.breadth_first() + assert isinstance(b, object) + + +def test_breadth_first_is_valid_generator(bst_big): + """Test breadth first returns valid generator.""" + g = bst_big.breadth_first() + assert next(g) == 50 + + +def test_breadth_first_return_children_l_to_r(bst_big): + """Test breadth first returns all children l to r.""" + g = bst_big.breadth_first() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [50, 40, 68, 10, 110, 1, 18, 80, 500, 5000] + + +def test_breadth_first_on_empty_bst_raises_value_error(bst): + """Test breadth first search raises value error if bst empty.""" + g = bst.breadth_first() + with pytest.raises(ValueError): + next(g) From 47f50afc4bfd184843e315ec20edeb114ad645fc Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Wed, 22 Nov 2017 22:09:00 -0800 Subject: [PATCH 35/40] started pre order --- src/bst.py | 13 +++++++++++-- src/test_bst.py | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/bst.py b/src/bst.py index ac761f4..a4d3d76 100644 --- a/src/bst.py +++ b/src/bst.py @@ -121,7 +121,6 @@ def in_order(self): """Return generator of in order search.""" if self.root is None: raise ValueError('There are no nodes in this tree.') - current = self.root ios = [] while current or ios: @@ -135,13 +134,23 @@ def in_order(self): def pre_order(self): """Return generator of pre order search.""" - pass + current = self.root + ios = [] + while current or ios: + if current: + ios.append(current) + current = current.left + else: + current = ios.pop() + yield current.data + current = current.right def post_order(self): """Return generator of post order wearch.""" pass + # notes class Solution: """ diff --git a/src/test_bst.py b/src/test_bst.py index 3f10040..ccf485c 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -186,3 +186,24 @@ def test_breadth_first_on_empty_bst_raises_value_error(bst): g = bst.breadth_first() with pytest.raises(ValueError): next(g) + + +def test_in_order_returns_object(bst): + """Test in order returns object.""" + g = bst.in_order() + assert isinstance(g, object) + + +def test_in_order_is_valid_generator(bst_big): + """Test in order returns valid generator.""" + g = bst_big.in_order() + assert next(g) == 1 + + +def test_in_order_returns_tree_in_ascending_order(bst_big): + """Test in order returns ordered vals.""" + g = bst_big.in_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [1, 10, 18, 40, 50, 68, 80, 110, 500, 5000] From 5e9ab828f0ed8ceffca50339d51e0e97b6ddf2ef Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Thu, 23 Nov 2017 11:00:59 -0800 Subject: [PATCH 36/40] pre order done and tested, post order in process --- src/bst.py | 59 ++++++++++++++++++------------------------------- src/test_bst.py | 30 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/bst.py b/src/bst.py index a4d3d76..3263708 100644 --- a/src/bst.py +++ b/src/bst.py @@ -134,43 +134,39 @@ def in_order(self): def pre_order(self): """Return generator of pre order search.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') current = self.root ios = [] while current or ios: if current: - ios.append(current) + yield current.data + if current.right: + ios.append(current.right) current = current.left else: current = ios.pop() - yield current.data - current = current.right def post_order(self): """Return generator of post order wearch.""" - pass - + if self.root is None: + raise ValueError('There are no nodes in this tree.') + current = self.root + previous = None + ios = [] + while current or ios: + if current: + ios.append(current) + current = current.left + else: + if : + current = current.right.left + current = current.right + else: + current = ios.pop() + yield current.data -# notes -class Solution: - """ - @param root: The root of binary tree. - @return: Preorder in ArrayList which contains node values. - """ - def preorderTraversal(self, root): - ret = [] - - stack = [] - while stack or root: - if root: - ret.append(root.val) - if root.right: - stack.append(root.right) - root = root.left - else: - root = stack.pop() - return ret - def postorderTraversal(self, root): ret = [] stack = [] @@ -187,19 +183,6 @@ def postorderTraversal(self, root): last = stack.pop() ret.append(last.val) return ret - - def inorderTraversal(self, root): - ret = [] - stack = [] - while stack or root: - if root: - stack.append(root) - root = root.left - else: - root = stack.pop() - ret.append(root.val) - root = root.right - return ret diff --git a/src/test_bst.py b/src/test_bst.py index ccf485c..f11cb70 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -207,3 +207,33 @@ def test_in_order_returns_tree_in_ascending_order(bst_big): for i in range(10): output.append(next(g)) assert output == [1, 10, 18, 40, 50, 68, 80, 110, 500, 5000] + + +def test_pre_order_retuns_object(bst): + """Test pre order returns object.""" + g = bst.pre_order() + assert isinstance(g, object) + + +def test_pre_order_returns_valid_generator(bst_big): + """Test pre order returns valid generator object.""" + g = bst_big.pre_order() + assert next(g) == 50 + + +def test_pre_order_returns_left_side_of_all_nodes_first(bst_big): + """Test pre order returns left side of each node first.""" + g = bst_big.pre_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [50, 40, 10, 1, 18, 68, 110, 80, 500, 5000] + + +def test_pre_order_returns_proper_order_unabalances(bst_full): + """Test pre order returns proper order from unbalanced tree.""" + g = bst_full.pre_order() + output = [] + for i in range(3): + output.append(next(g)) + assert output == [3254, 908543, 58490543] From 094d8f6f7f5c7e9af06a5e4442a872acbe11c94f Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Fri, 24 Nov 2017 10:10:49 -0800 Subject: [PATCH 37/40] pre, post, breadth, and in order finished and tested --- README.md | 9 +++++++- src/bst.py | 30 +++++-------------------- src/test_bst.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e7c7674..d261192 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,10 @@ Bst() contains the following methods: * _depth(root) (0(n))_ - returns the depth of the tree. * _contains(val) (Olog(n))_ - will return true or false if value is in tree. *_ balance() (0(n))_ - returns the depth of the left minus the right side of the tree as an integer. +* _in_order() (O(n))_ - returns a generator the entire tree in order of lowest to highest value. +* _pre_order() (0(n)(log(n)))_ - returns a generator of parent followed by children from left side to right side. +* _post_order() (O(n)log(n)))_ - returns a generator of children followed by parent from left side to right side. +*_ breadth_first() (0(n))_ - returns generator of tree ordered from root one level at a time. To access any contained methods: new.insert(val) @@ -257,5 +261,8 @@ new.size() new.depth(root) new.contains(val) new.balance() - +new.in_order() +new.pre_order() +new.post_order() +new.breadth_first() ``` \ No newline at end of file diff --git a/src/bst.py b/src/bst.py index 3263708..af7e5ba 100644 --- a/src/bst.py +++ b/src/bst.py @@ -152,38 +152,18 @@ def post_order(self): if self.root is None: raise ValueError('There are no nodes in this tree.') current = self.root - previous = None + child = None ios = [] while current or ios: if current: ios.append(current) current = current.left else: - if : - current = current.right.left - current = current.right - else: - current = ios.pop() - yield current.data - - - def postorderTraversal(self, root): - ret = [] - stack = [] - - last = None - while stack or root: - if root: - stack.append(root) - root = root.left - else: - if stack[-1].right and stack[-1].right is not last: - root = stack[-1].right + if ios[-1].right and ios[-1].right is not child: + current = ios[-1].right else: - last = stack.pop() - ret.append(last.val) - return ret - + child = ios.pop() + yield child.data if __name__ == '__main__': # pragma: no cover diff --git a/src/test_bst.py b/src/test_bst.py index f11cb70..cb40694 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -209,6 +209,13 @@ def test_in_order_returns_tree_in_ascending_order(bst_big): assert output == [1, 10, 18, 40, 50, 68, 80, 110, 500, 5000] +def test_in_order_on_empty_bst_raises_value_error(bst): + """Test in order search raises value error if bst empty.""" + g = bst.in_order() + with pytest.raises(ValueError): + next(g) + + def test_pre_order_retuns_object(bst): """Test pre order returns object.""" g = bst.pre_order() @@ -237,3 +244,56 @@ def test_pre_order_returns_proper_order_unabalances(bst_full): for i in range(3): output.append(next(g)) assert output == [3254, 908543, 58490543] + + +def test_pre_order_on_empty_bst_raises_value_error(bst): + """Test pre order search raises value error if bst empty.""" + g = bst.pre_order() + with pytest.raises(ValueError): + next(g) + + +def test_post_order_returns_object(bst): + """Test post order returns object.""" + g = bst.post_order() + assert isinstance(g, object) + + +def test_post_order_returns_valid_generator(bst_big): + """Test post order returns valid generator object.""" + g = bst_big.post_order() + assert next(g) == 1 + + +def test_post_order_returns_root_last(bst_big): + """Test post order returns left side then right with root in middle.""" + g = bst_big.post_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output[-1] == 50 + + +def test_post_order_returns_proper_order_unbalanced(bst_full): + """Test post order returns proper order on unbalanced tree.""" + g = bst_full.post_order() + output = [] + for i in range(3): + output.append(next(g)) + assert output == [58490543, 908543, 3254] + + +def test_post_order_returns_proper_order_on_balanced(bst_big): + """Test post order returns proper order on balanced tree.""" + g = bst_big.post_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [1, 18, 10, 40, 80, 5000, 500, 110, 68, 50] + + +def test_post_order_on_empty_bst_raises_value_error(bst): + """Test post order search raises value error if bst empty.""" + g = bst.post_order() + with pytest.raises(ValueError): + next(g) From 49a9153aac29e691737f0d6dea7d1e7e0ef28bff Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Fri, 24 Nov 2017 10:40:54 -0800 Subject: [PATCH 38/40] added travis ci integration --- .travis.yml | 8 ++++++++ requirements.txt | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 .travis.yml create mode 100644 requirements.txt diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3f621ab --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: python +python: + - "2.7" + - "3.6" +install: + - pip install . + - pip install -r requirements.txt +script: py.test \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fc699bf --- /dev/null +++ b/requirements.txt @@ -0,0 +1,30 @@ +appnope==0.1.0 +argh==0.26.2 +colorama==0.3.9 +coverage==4.4.2 +-e git+https://github.com/markreynoso/data-structures.git@094d8f6f7f5c7e9af06a5e4442a872acbe11c94f#egg=data_structures +decorator==4.1.2 +docopt==0.6.2 +ipython==6.2.1 +ipython-genutils==0.2.0 +jedi==0.11.0 +parso==0.1.0 +pathtools==0.1.2 +pexpect==4.3.0 +pickleshare==0.7.4 +pluggy==0.5.2 +prompt-toolkit==1.0.15 +ptyprocess==0.5.2 +py==1.5.2 +Pygments==2.2.0 +pytest==3.2.5 +pytest-cov==2.5.1 +pytest-watch==4.1.0 +PyYAML==3.12 +simplegeneric==0.8.1 +six==1.11.0 +tox==2.9.1 +traitlets==4.3.2 +virtualenv==15.1.0 +watchdog==0.8.3 +wcwidth==0.1.7 From aaa7b0c3630230b6a0d2aff97e6c44dd57fcb9d1 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Fri, 24 Nov 2017 10:48:11 -0800 Subject: [PATCH 39/40] added travis ci integration --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index fc699bf..4f44f7d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,6 @@ coverage==4.4.2 -e git+https://github.com/markreynoso/data-structures.git@094d8f6f7f5c7e9af06a5e4442a872acbe11c94f#egg=data_structures decorator==4.1.2 docopt==0.6.2 -ipython==6.2.1 ipython-genutils==0.2.0 jedi==0.11.0 parso==0.1.0 From f0763a4c1c95c08a119a981e55cae8918e10a745 Mon Sep 17 00:00:00 2001 From: Mark Reynoso Date: Fri, 24 Nov 2017 10:51:35 -0800 Subject: [PATCH 40/40] added travis badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d261192..2eaeedb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Data-Structures +[![Build Status](https://travis-ci.org/markreynoso/data-structures.svg?branch=bst_traversals)](https://travis-ci.org/markreynoso/data-structures) + Where a variety of data-structures found in python are being explored, such as: * A simple class LinkedList for building linked lists as well as a subclass Stack to implement a stack.