From 372ab2d4bec0071a87c24a3d4ce96c822ee265e0 Mon Sep 17 00:00:00 2001 From: FeschenkoNatalia Date: Thu, 5 Apr 2018 16:47:17 +0300 Subject: [PATCH 1/4] Fix binary search --- .eslintrc.js | 1 + .gitignore | 1 + README.md | 1 + lib/binary-search/binary-search.test.js | 1 + lib/binary-search/index.js | 1 + lib/index.js | 1 + package-lock.json | 1 + package.json | 1 + 8 files changed, 8 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 27c17f7..3e6aa50 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,3 +1,4 @@ + module.exports = { "extends": "airbnb-base", "rules": { diff --git a/.gitignore b/.gitignore index bb5c8c1..0f67d91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ + /node_modules .idea \ No newline at end of file diff --git a/README.md b/README.md index 52bbe82..92491b3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # Algorithms/services implemented by Feschenko Natalia Instalation: diff --git a/lib/binary-search/binary-search.test.js b/lib/binary-search/binary-search.test.js index 1bfbfab..ec79872 100644 --- a/lib/binary-search/binary-search.test.js +++ b/lib/binary-search/binary-search.test.js @@ -1,3 +1,4 @@ + const should = require('should'); const algo = require('../index.js'); diff --git a/lib/binary-search/index.js b/lib/binary-search/index.js index 406b368..6ea8aff 100644 --- a/lib/binary-search/index.js +++ b/lib/binary-search/index.js @@ -1,3 +1,4 @@ + module.exports = (sequance, el) => { let start = 0; let end = sequance.length - 1; diff --git a/lib/index.js b/lib/index.js index ea9836e..dedfb83 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ + const binarySearch = require('./binary-search'); const stack = require('./data-structures/stack'); const queue = require('./data-structures/queue'); diff --git a/package-lock.json b/package-lock.json index 889c4cd..ff0af36 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,3 +1,4 @@ + { "name": "algorithms-feschenko", "version": "1.0.0", diff --git a/package.json b/package.json index 235060d..514cc59 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,4 @@ + { "name": "algorithms-feschenko", "version": "1.0.0", From f024711847d795c15b04e0d44881ec0c4d4a2913 Mon Sep 17 00:00:00 2001 From: FeschenkoNatalia Date: Thu, 5 Apr 2018 17:08:33 +0300 Subject: [PATCH 2/4] Fix data structures --- lib/data-structures/linkedlist/index.js | 19 +++++++++++++++++++ lib/data-structures/queue/index.js | 8 ++++++++ lib/data-structures/queue/queue.test.js | 10 ++++++++++ lib/data-structures/stack/index.js | 8 ++++++++ lib/data-structures/stack/stack.test.js | 10 ++++++++++ 5 files changed, 55 insertions(+) create mode 100644 lib/data-structures/linkedlist/index.js create mode 100644 lib/data-structures/queue/index.js create mode 100644 lib/data-structures/queue/queue.test.js create mode 100644 lib/data-structures/stack/index.js create mode 100644 lib/data-structures/stack/stack.test.js diff --git a/lib/data-structures/linkedlist/index.js b/lib/data-structures/linkedlist/index.js new file mode 100644 index 0000000..68cebcc --- /dev/null +++ b/lib/data-structures/linkedlist/index.js @@ -0,0 +1,19 @@ +function LinkedList() { + this.head = null; +} + +LinkedList.push = function f(val) { + const node = { + value: val, + next: null, + }; + if (!this.head) { + this.head = node; + } else { + current = this.head; + while (current.next) { + current = current.next; + } + current.next = node; + } +}; diff --git a/lib/data-structures/queue/index.js b/lib/data-structures/queue/index.js new file mode 100644 index 0000000..42d1944 --- /dev/null +++ b/lib/data-structures/queue/index.js @@ -0,0 +1,8 @@ +module.exports = (firstElement, secondElement, findElement) => { + const queue = []; + queue.push(findElement); + queue.push(secondElement); + findElement = queue.shift(); + console.log(findElement); + return findElement; +}; diff --git a/lib/data-structures/queue/queue.test.js b/lib/data-structures/queue/queue.test.js new file mode 100644 index 0000000..947fb78 --- /dev/null +++ b/lib/data-structures/queue/queue.test.js @@ -0,0 +1,10 @@ + +const should = require('should'); +const data = require('../../index.js'); + +describe('Data structures queue', () => { + it('should return 3', () => { + const element = data.queue(3, 10, 3); + should(element).be.eql(3); + }); +}); diff --git a/lib/data-structures/stack/index.js b/lib/data-structures/stack/index.js new file mode 100644 index 0000000..f435174 --- /dev/null +++ b/lib/data-structures/stack/index.js @@ -0,0 +1,8 @@ +module.exports = (firstElement, secondElement, findElement) => { + const stack = []; + stack.push(firstElement); + stack.push(secondElement); + findElement = stack.pop(); + console.log(findElement); + return findElement; +}; diff --git a/lib/data-structures/stack/stack.test.js b/lib/data-structures/stack/stack.test.js new file mode 100644 index 0000000..c36fa82 --- /dev/null +++ b/lib/data-structures/stack/stack.test.js @@ -0,0 +1,10 @@ + +const should = require('should'); +const data = require('../../index.js'); + +describe('Data structures stack', () => { + it('should return 10', () => { + const element = data.stack(3, 10, 10); + should(element).be.eql(10); + }); +}); From fc1caf300d5f713832e32cd415129eb7cb1acd09 Mon Sep 17 00:00:00 2001 From: FeschenkoNatalia Date: Thu, 5 Apr 2018 17:18:20 +0300 Subject: [PATCH 3/4] Fix data structures --- lib/data-structures/linkedlist/index.js | 1 + lib/data-structures/queue/index.js | 1 + lib/data-structures/queue/queue.test.js | 1 - lib/data-structures/stack/index.js | 1 + lib/data-structures/stack/stack.test.js | 1 - 5 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/data-structures/linkedlist/index.js b/lib/data-structures/linkedlist/index.js index 68cebcc..4396329 100644 --- a/lib/data-structures/linkedlist/index.js +++ b/lib/data-structures/linkedlist/index.js @@ -1,3 +1,4 @@ + function LinkedList() { this.head = null; } diff --git a/lib/data-structures/queue/index.js b/lib/data-structures/queue/index.js index 42d1944..0422ee3 100644 --- a/lib/data-structures/queue/index.js +++ b/lib/data-structures/queue/index.js @@ -1,3 +1,4 @@ + module.exports = (firstElement, secondElement, findElement) => { const queue = []; queue.push(findElement); diff --git a/lib/data-structures/queue/queue.test.js b/lib/data-structures/queue/queue.test.js index 947fb78..add63ff 100644 --- a/lib/data-structures/queue/queue.test.js +++ b/lib/data-structures/queue/queue.test.js @@ -1,4 +1,3 @@ - const should = require('should'); const data = require('../../index.js'); diff --git a/lib/data-structures/stack/index.js b/lib/data-structures/stack/index.js index f435174..3693a6a 100644 --- a/lib/data-structures/stack/index.js +++ b/lib/data-structures/stack/index.js @@ -1,3 +1,4 @@ + module.exports = (firstElement, secondElement, findElement) => { const stack = []; stack.push(firstElement); diff --git a/lib/data-structures/stack/stack.test.js b/lib/data-structures/stack/stack.test.js index c36fa82..375e95d 100644 --- a/lib/data-structures/stack/stack.test.js +++ b/lib/data-structures/stack/stack.test.js @@ -1,4 +1,3 @@ - const should = require('should'); const data = require('../../index.js'); From 06de222a0eaa484afeff7221bc34bb938e02d2cc Mon Sep 17 00:00:00 2001 From: FeschenkoNatalia Date: Thu, 5 Apr 2018 17:25:24 +0300 Subject: [PATCH 4/4] Fix dynamic connectivity --- .../dynamic-connectivity.test.js | 38 +++++++++++++++++++ lib/dynamic-connectivity/index.js | 36 ++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 lib/dynamic-connectivity/dynamic-connectivity.test.js create mode 100644 lib/dynamic-connectivity/index.js diff --git a/lib/dynamic-connectivity/dynamic-connectivity.test.js b/lib/dynamic-connectivity/dynamic-connectivity.test.js new file mode 100644 index 0000000..8c7d9c2 --- /dev/null +++ b/lib/dynamic-connectivity/dynamic-connectivity.test.js @@ -0,0 +1,38 @@ +const should = require('should'); +const algo = require('./../index.js'); + +const elements = 6; +function getUnqueElement(count, notEqual) { + const _result = Math.floor(Math.random() * count); + return _result === notEqual ? getUnqueElement(count, notEqual) : _result; +} +describe('Dynamic Connectivity', () => { + it('Should connect p and q', () => { + const dynamicConnectivity = new algo.DynamicConnectivity(elements); + for (let i = 0; i <= 5; i += 1) { + const p = getUnqueElement(elements, null); + const q = getUnqueElement(elements, p); + dynamicConnectivity.connect(p, q); + } + }); +}); + +describe('Dynamic Connectivity', () => { + it('Should check if p and q are connected and return true', () => { + const dynamicConnectivity = new algo.DynamicConnectivity(elements); + dynamicConnectivity.connect(1, 2); + dynamicConnectivity.connect(3, 4); + dynamicConnectivity.connect(1, 4); + should(dynamicConnectivity.isConnected(2, 4)).be.eql(true); + }); +}); + +describe('Dynamic Connectivity', () => { + it('Should check if p and q are connected and return false', () => { + const dynamicConnectivity = new algo.DynamicConnectivity(elements); + dynamicConnectivity.connect(1, 2); + dynamicConnectivity.connect(3, 4); + dynamicConnectivity.connect(1, 4); + should(dynamicConnectivity.isConnected(1, 7)).be.eql(false); + }); +}); diff --git a/lib/dynamic-connectivity/index.js b/lib/dynamic-connectivity/index.js new file mode 100644 index 0000000..0177c19 --- /dev/null +++ b/lib/dynamic-connectivity/index.js @@ -0,0 +1,36 @@ +module.exports = class { + constructor(elements) { + this.size = []; + this.id = []; + for (let i = 0; i <= elements; i += 1) { + this.size.push(1); + this.id.push(i); + } + } + + root(i) { + while (i !== this.id[i]) { + this.id[i] = this.id[this.id[i]]; + i = this.id[i]; + } + return i; + } + + isConnected(p, q) { + return this.root(p) === this.root(q); + } + + connect(p, q) { + const i = this.root(p); + const j = this.root(q); + if (i === j) return; + if (this.size[i] < this.size[j]) { + this.id[i] = j; + this.size[j] += this.size[i]; + } else { + this.id[j] = i; + this.size[i] += this.size[j]; + } + } +}; +