Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e755882
finished adding autocmplete, left over from last week
chelseadole Dec 12, 2017
c1a9d65
adding two new tests to autocmplete in trie.
chelseadole Dec 12, 2017
ceba63c
one more test
chelseadole Dec 12, 2017
6ebce10
assert StopIteration when done.
chelseadole Dec 12, 2017
9cae6ed
adding error handling for non string input.
chelseadole Dec 12, 2017
27d365c
last insertion sort stuff.
chelseadole Dec 13, 2017
7658d49
finished insertion sort.
chelseadole Dec 14, 2017
917a87a
renaming insertion sort files to use underscore.
chelseadole Dec 14, 2017
aa86758
adding first tests for insertion sort.
chelseadole Dec 14, 2017
3ff4b28
changing test paramerization.
chelseadole Dec 14, 2017
5364b43
adding last test for randomized long lsts.
chelseadole Dec 14, 2017
40f09c3
adding test to check for negative numbers.
chelseadole Dec 14, 2017
1f40505
adding to README.
chelseadole Dec 14, 2017
591ff2e
adding timeit and if name is main to insertion sort.
chelseadole Dec 14, 2017
63d10db
creating files for LL and DLL in js-src file
chelseadole Dec 14, 2017
f406f80
adding constructor to LL.
chelseadole Dec 15, 2017
7145bdc
added semi colon bois
chelseadole Dec 15, 2017
94ac35e
adding more curlybois and adding push and pop fns.
chelseadole Dec 15, 2017
842cc34
added pop function completed.
chelseadole Dec 15, 2017
826cd56
added size() on LL and DLL
chelseadole Dec 15, 2017
43bdd12
adding remove and serach to DLL and LL.
chelseadole Dec 15, 2017
9505bd5
more remove method work.
chelseadole Dec 15, 2017
1c63770
adding more methods to DLL, fixing tail method of LL.
chelseadole Dec 15, 2017
e51d0d5
adding first two sketchy af chai tests.
chelseadole Dec 15, 2017
300e9d4
adding more tests to js-linked-list.
chelseadole Dec 15, 2017
fd73506
adding more to linked-list test.
chelseadole Dec 15, 2017
29004cf
adding node_modules to gitignore and also more test bois
chelseadole Dec 15, 2017
27073c0
fixing tests for linked-list in JS.
chelseadole Dec 15, 2017
b4503b2
fixing syntax of JS errors.
chelseadole Dec 15, 2017
8948b6f
broke some shit
chelseadole Dec 15, 2017
28037ac
all tests passing :squidab: :ho-ho-ho:
chelseadole Dec 15, 2017
bd158a0
adding changes to README.
chelseadole Dec 15, 2017
e843145
getting rid of semicolon bois :/
chelseadole Dec 16, 2017
64d3d56
turning return errors into throw errors.
chelseadole Dec 16, 2017
414d202
fixing LL tests.
chelseadole Dec 16, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ ENV/

# mypy
.mypy_cache/
node_modules/
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

**Testing Tools**: pytest, pytest-cov

## Sorting Algorithms:

* **Insertion Sort** - *The insertion sort algorithm sorts a list of numbers by looping through each individual number in the list one by one, and (if they are smaller than the last index) swapping the two numbers. Unlike bubble sort, after this swap the sort continues to swap the number down into lower indexed positions until it finds the right position. The runtime for this function is at worst-case O(n^2), because in that scenario the list would be exactly the reverse of what it should be.*

## Data Structures:

* **Binary Search Tree** — *a BST is a "tree shaped" data structure containing nodes. Each node can have a maximum of two children or "leaves," and all node values are properly located based on its parents and siblings values. Nodes to the left of the "root"/head node have values smaller than the root. Those to the right have values larger than the root. There are no duplicate values.*

* **Trie Tree** - *a Trie Tree is a "tree shaped" data structure containing nodes with references to letters. These nodes string together (using each node's "children" and "parent" attriutes) to form words. This tree allows for quick lookup time of words, and is used for things such as word suggestion/auto-complete.*

* **Linked List (JavaScript)** - *A singly linked list is a data structure made of nodes, all of which have a single "next" pointer pointing from the head to the tail. The runtime of traversing the LL is O(n) because it grows proportionally with the length of the list.*

## Time Complexities:

* balance() = *This BST function returns the balance (or size difference) between the left and right parts of the tree. Its runtime is O(1), because it always takes the same amount of time to run regardless of tree size, and only performs simple subtraction.*
Expand Down
131 changes: 131 additions & 0 deletions js-src/js-doubly-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"use strict";

class Node {
constructor(data, nextNode=null, prevNode=null) {
this.data = data;
this.nextNode = nextNode;
this.prevNode = prevNode;
}
}

class DLL {
constructor(iter=null) {
this.head = null;
this.tail = null;
this.counter = 0;
if (Array.isArray(iter)) {
iter.forEach(item => this.push(item));
}
else if (iter !== null) {
throw 'LinkedList only takes arrays as inputs.';
}
}

push(val) {
this.head = new Node(val, this.head);
this.counter ++;
if (this.size() === 1) {
this.tail = this.head;
} else {
this.head.nextNode.prevNode = this.head;
}
}

pop() {
if (this.size() === 0) {
throw 'Cannot pop() from empty DoublyLinkedList.';
}
this.counter --;
var output = this.head.data;
this.head = this.head.nextNode;
if (this.head) {
this.head.prevNode = null;
}
return output;
}

size() {
return this.counter;
}

search(val) {
var curr = this.head;
while (curr.data !== val) {
if (curr.nextNode === null) {
throw 'This value is not in the LinkedList.'
}
curr = curr.nextNode;
}
return curr;
}

remove(val) {
var curr = this.head;
var prev = null;
while (curr) {
if (curr.data == val) {
this.counter --;
if (curr == this.head) {
this.head = curr.nextNode;
this.head.prevNode = null;
return curr.data;
}
prev.nextNode = curr.nextNode;
curr.nextNode.prevNode = prev;
return curr.data;
}
if (curr.nextNode == null) {
throw 'Values not in the DoublyLinkedList cannot be removed.';
}
prev = curr;
curr = curr.nextNode;
}
return curr;
}

append(val){
var firstTail = this.tail;
this.tail = new Node(val, this.prevNode=this.tail);
this.counter ++;
if(firstTail){
firstTail.nextNode = this.tail;
} else {
this.head = this.tail;
}
this.tail.nextNode = null;
}

shift(){
if (!this.tail){
throw "Cannot shift() from empty DLL.";
}
var output = this.tail.data;
this.tail = this.tail.prevNode;
if(this.tail) {
this.tail.nextNode = null;
} else {
this.head = null;
}
this.counter --;
return output;
}


display() {
var start_paren = "(";
if (this.head === null) {
return '()';
}
var curr = this.head;
while (curr) {
if (curr.nextNode === null) {
start_paren += curr.data + ')';
return start_paren;
}
else {
start_paren += curr.data + ', ';
curr = curr.nextNode;
}
}
}
}
95 changes: 95 additions & 0 deletions js-src/js-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

class Node {
constructor(data, nextNode=null) {
this.data = data
this.nextNode = nextNode
}
}

class LL {
constructor(iter=null) {
this.head = null
this.counter = 0
if (Array.isArray(iter)) {
iter.forEach(item => this.push(item))
}
else if (iter !== null) {
throw new TypeError('LinkedList only takes arrays as inputs.')
}
}

push(val) {
this.head = new Node(val, this.head)
this.counter ++
}

pop() {
if (this.counter === 0) {
throw new TypeError('Cannot pop() from empty LinkedList.')
}
var output = this.head.data
this.head = this.head.nextNode
this.counter --
return output
}

size() {
return this.counter;
}

search(val) {
var curr = this.head
while (curr.data !== val) {
if (curr.nextNode === null) {
throw new TypeError('This value is not in the LinkedList.')
}
curr = curr.nextNode
}
return curr;
}

remove(val) {
var curr = this.head
if (this.head.data === val) {
this.head = this.head.nextNode
this.counter --
return val
}
if (this.head === null) {
throw new TypeError('Cannot remove vals from empty LinkedList.')
}
while (curr) {
if (curr.nextNode === null) {
throw new TypeError('Values not in the LinkedList cannot be removed.')
}
else if (curr.nextNode.data === val) {
curr.nextNode = curr.nextNode.nextNode
this.counter --
return val
}
curr = curr.nextNode
}
}

display() {
var start_paren = '('
if (this.head === null) {
return '()'
}
var curr = this.head
while (curr) {
if (curr.nextNode === null) {
start_paren += curr.data + ')'
return start_paren;
}
else {
start_paren += curr.data + ', '
curr = curr.nextNode
}
}
}
}

module.exports = LL;

17 changes: 17 additions & 0 deletions js-src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "js-data-structures",
"version": "1.0.0",
"description": "",
"main": "js-linked-list.js",
"dependencies": {
"chai": "^4.1.2"
},
"devDependencies": {
"mocha": "^4.0.1"
},
"scripts": {
"test": "mocha"
},
"author": "Ya bish",
"license": "ISC"
}
79 changes: 79 additions & 0 deletions js-src/test/js-linked-list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

var linkedList = require('../js-linked-list');
var chai = require('chai');
var expect = chai.expect;

describe('linked_list.js tests', () => {
it('creating a new empty LinkedList', () => {
var testList = new linkedList();
expect(testList.size()).to.equal(0);
expect(testList.head).to.equal(null);
});

it('passing iterable into new LinkedList and pushing.', () => {
var testList = new linkedList([100, 200, 300, 400, 500]);
expect(testList.size()).to.equal(5);
expect(testList.head.data).to.equal(500);

});

it('testing push method changes head', () => {
var testList = new linkedList();
testList.push('yo');
expect(testList.head.data).to.equal('yo');
});

it('test push method adds one to size.', () => {
var testList = new linkedList();
testList.push(1);
expect(testList.size()).to.equal(1);
});

it("test pop on non-empty list.", () => {
var testList = new linkedList();
testList.push(5);
testList.push(4);
testList.push(3);
expect(testList.head.data).to.equal(3);
expect(testList.pop()).to.equal(3);
expect(testList.head.data).to.equal(4);
});

it("size function works with push and pop", () => {
var testList = new linkedList([1, 2, 3, 4, 5]);
expect(testList.size()).to.equal(5);
testList.push(0);
expect(testList.size()).to.equal(6)
testList.pop();
expect(testList.size()).to.equal(5);
});

it('correct search method works', () => {
var testList = new linkedList();
testList.push(5);
testList.push(4);
expect(testList.search(5).data).to.equal(5);
});

it("remove method with item in head of list", () => {
var testList = new linkedList();
testList.push(1);
expect(testList.size()).to.equal(1);
expect(testList.remove(1)).to.equal(1);
expect(testList.size()).to.equal(0);
});

it("remove method with item in middle of list", () => {
var testList = new linkedList([1, 2, 3, 4, 5]);
expect(testList.size()).to.equal(5);
expect(testList.remove(3)).to.equal(3);
expect(testList.size()).to.equal(4);
});

it("display method", () => {
var testList = new linkedList([1, 2, 3, 4, 5]);
expect(testList.display()).to.be.string('(5, 4, 3, 2, 1)');
});
});

45 changes: 45 additions & 0 deletions src/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Insertion sort algorithm."""


def insertion_sort(lst):
"""Implementation of insertion sort in python."""
if isinstance(lst, list):
for i in range(1, len(lst)):
curr = lst[i]
if not isinstance(i, int):
raise TypeError('Only integers can be in the list.')
while i > 0 and lst[i] < lst[i - 1]:
lst[i], lst[i - 1] = lst[i - 1], lst[i]
i -= 1
i = curr
return lst
raise TypeError('Only lists can be used with Insertion Sort.')

if __name__ == '__main__': # pragama: no cover
import timeit as ti
import random
best_case = [1, 2, 3, 4, 5]
worst_case = [5, 4, 3, 2, 1]
random = [random.randint(1, 100) for i in range(10)]

time_1 = ti.timeit("insertion_sort(best_case)",
setup="from __main__ import best_case, insertion_sort")
time_2 = ti.timeit("insertion_sort(worst_case)",
setup="from __main__ import worst_case, insertion_sort")
time_3 = ti.timeit("insertion_sort(random)",
setup="from __main__ import random, insertion_sort")
print("""
Insertion sort compares the vals of an index versus the index - 1, and
(if index is smaller) switches. Them it continues switching until index is
bigger than index - 1.

Input:[1, 2, 3, 4, 5]
Sort time: {}

Input:[5, 4, 3, 2, 1]
Sort time: {}

Input:list(range(5, 0, -1))
Sort time: {}
""".format(time_1, time_2, time_3))

Loading