Skip to content

Commit 2e9912b

Browse files
committedMar 29, 2019
--wip-- [skip ci]

File tree

17 files changed

+166
-157
lines changed

17 files changed

+166
-157
lines changed
 

‎.eslintignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/runtimes/**
2+
src/data-structures/maps/hash-maps/hashing.js
3+
src/data-structures/maps/hash-maps/*-test.js
4+
src/data-structures/maps/hash-maps/hash-map-*.js
5+
src/data-structures/linked-lists/linked-list-*.js
6+
src/data-structures/custom/lru-cache-*.js

‎.eslintrc.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
module.exports = {
2-
"extends": "airbnb-base",
3-
"env": {
4-
"jest": true
2+
extends: 'airbnb-base',
3+
env: {
4+
jest: true
5+
},
6+
globals: {
7+
BigInt: true,
58
},
69
rules: {
710
// https://github.com/airbnb/javascript/issues/1089
811

912
// https://stackoverflow.com/a/35637900/684957
1013
// allow to add properties to arguments
11-
"no-param-reassign": [2, { "props": false }],
14+
'no-param-reassign': [2, { 'props': false }],
1215

1316
// https://eslint.org/docs/rules/no-plusplus
1417
// allows unary operators ++ and -- in the afterthought (final expression) of a for loop.
15-
"no-plusplus": [2, { "allowForLoopAfterthoughts": true }],
18+
'no-plusplus': [2, { 'allowForLoopAfterthoughts': true }],
1619

1720
// Allow for..of
18-
"no-restricted-syntax": [0, "ForOfStatement"],
19-
},
20-
globals: {
21-
BigInt: true,
21+
'no-restricted-syntax': [0, 'ForOfStatement'],
2222
}
2323
};

‎package-lock.json

Lines changed: 104 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
"version": "1.1.0",
44
"description": "Data Structures & Algorithms in JS",
55
"main": "./src/index.js",
6-
"dependencies": {
7-
8-
},
6+
"dependencies": {},
97
"devDependencies": {
108
"benchmark": "2.1.4",
119
"eslint": "4.19.1",
12-
"eslint-config-airbnb-base": "12.1.0",
13-
"eslint-plugin-import": "2.12.0",
10+
"eslint-config-airbnb-base": "^13.1.0",
11+
"eslint-plugin-import": "^2.16.0",
1412
"eslint-plugin-jest": "21.17.0",
1513
"jest": "23.6.0",
1614
"textlint-plugin-asciidoctor": "1.0.2"
@@ -19,7 +17,8 @@
1917
"test": "jest src/",
2018
"watch": "jest src/ --watch --coverage",
2119
"coverage": "jest src/ --coverage && open coverage/lcov-report/index.html",
22-
"linter": "npx eslint --fix -f codeframe src/"
20+
"linter": "npx eslint --fix --format codeframe src/",
21+
"ci": "npm run linter && npm test"
2322
},
2423
"keywords": [
2524
"algorithms",

‎src/algorithms/sorting/merge-sort.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function merge(array1, array2 = []) {
1616
// merge elements on a and b in asc order. Run-time O(a + b)
1717
for (let index = 0, i1 = 0, i2 = 0;
1818
index < mergedLength; index++) { // <1>
19-
if (i2 >= array2.length ||
20-
(i1 < array1.length && array1[i1] <= array2[i2])) {
19+
if (i2 >= array2.length
20+
|| (i1 < array1.length && array1[i1] <= array2[i2])) {
2121
mergedArray[index] = array1[i1]; // <2>
2222
i1 += 1;
2323
} else {
@@ -45,7 +45,7 @@ function splitSort(array) {
4545
// base case
4646
if (size < 2) {
4747
return array;
48-
} else if (size === 2) {
48+
} if (size === 2) {
4949
return array[0] < array[1] ? array : [array[1], array[0]]; // <1>
5050
}
5151

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* Design and implement a data structure for Least Recently Used (LRU) cache.
43
* It should support the following operations: get and put.
@@ -31,46 +30,41 @@
3130
*
3231
* @param {number} capacity
3332
*/
34-
const LRUCache = (capacity) => {
35-
this.map = new Map();
36-
this.capacity = capacity;
37-
};
33+
class LRUCache {
34+
constructor(capacity) {
35+
this.map = new Map();
36+
this.capacity = capacity;
37+
}
3838

39-
/**
40-
* @param {number} key
41-
* @return {number}
42-
*/
43-
LRUCache.prototype.get = (key) => {
44-
const value = this.map.get(key);
45-
if (value) {
46-
this.moveToTop(key);
47-
return value;
39+
get(key) {
40+
const value = this.map.get(key);
41+
if (value) {
42+
this.moveToTop(key);
43+
return value;
44+
}
45+
return -1;
4846
}
49-
return -1;
50-
};
5147

52-
/**
53-
* @param {number} key
54-
* @param {number} value
55-
* @return {void}
56-
*/
57-
LRUCache.prototype.put = (key, value) => {
58-
this.map.set(key, value);
59-
this.rotate(key);
60-
};
48+
put(key, value) {
49+
this.map.set(key, value);
50+
this.rotate(key);
51+
}
6152

62-
LRUCache.prototype.rotate = (key) => {
63-
this.moveToTop(key);
64-
while (this.map.size > this.capacity) {
65-
const it = this.map.keys();
66-
this.map.delete(it.next().value);
53+
rotate(key) {
54+
this.moveToTop(key);
55+
while (this.map.size > this.capacity) {
56+
const it = this.map.keys();
57+
this.map.delete(it.next().value);
58+
}
6759
}
68-
};
6960

70-
LRUCache.prototype.moveToTop = (key) => {
71-
if (this.map.has(key)) {
72-
const value = this.map.get(key);
73-
this.map.delete(key);
74-
this.map.set(key, value);
61+
moveToTop(key) {
62+
if (this.map.has(key)) {
63+
const value = this.map.get(key);
64+
this.map.delete(key);
65+
this.map.set(key, value);
66+
}
7567
}
76-
};
68+
}
69+
70+
module.exports = LRUCache;

‎src/data-structures/linked-lists/linked-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const Node = require('./node');
21
const util = require('util');
2+
const Node = require('./node');
33

44
// tag::constructor[]
55
/**

‎src/data-structures/maps/hash-maps/hash-map-1.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,3 @@ module.exports = HashMap;
7474
// assert.equal(hashMap.get('cat'), 8); // got overwritten by art 😱
7575
// assert.equal(hashMap.get('rat'), 8); // got overwritten by art 😱
7676
// assert.equal(hashMap.get('dog'), 8); // got overwritten by art 😱
77-

‎src/data-structures/queues/queue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Queue {
3232
dequeue() {
3333
return this.items.removeFirst();
3434
}
35+
3536
// end::dequeue[]
3637
/**
3738
* Size of the queue
@@ -66,4 +67,3 @@ queue.dequeue(); //↪️ b
6667
queue.dequeue(); //↪️ c
6768
// end::snippet[]
6869
// */
69-

‎src/data-structures/sets/array-set.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,3 @@ class ArraySet {
8585
}
8686

8787
module.exports = ArraySet;
88-

‎src/data-structures/sets/hash-set.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,3 @@ class HashMapSet {
8787
}
8888

8989
module.exports = HashMapSet;
90-

‎src/data-structures/sets/map-set.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,3 @@ class MapSet {
7272
}
7373

7474
module.exports = MapSet;
75-

‎src/data-structures/stacks/stack-1.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@ Stack.prototype.push = Stack.prototype.add;
3737
Stack.prototype.pop = Stack.prototype.remove;
3838

3939
module.exports = Stack;
40-

‎src/data-structures/stacks/stack.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,3 @@ stack.remove(); //↪️ c
6767
stack.remove(); //↪️ a
6868
// end::snippet[]
6969
// */
70-

‎src/data-structures/trees/avl-tree.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ function balance(node) {
2424
// left subtree is higher than right subtree
2525
if (node.left.balanceFactor > 0) {
2626
return rightRotation(node);
27-
} else if (node.left.balanceFactor < 0) {
27+
} if (node.left.balanceFactor < 0) {
2828
return leftRightRotation(node);
2929
}
3030
} else if (node.balanceFactor < -1) {
3131
// right subtree is higher than left subtree
3232
if (node.right.balanceFactor < 0) {
3333
return leftRotation(node);
34-
} else if (node.right.balanceFactor > 0) {
34+
} if (node.right.balanceFactor > 0) {
3535
return rightLeftRotation(node);
3636
}
3737
}

‎src/data-structures/trees/binary-search-tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BinarySearchTree {
6969
findNodeAndParent(value, node = this.root, parent = null) {
7070
if (!node || node.value === value) {
7171
return { found: node, parent };
72-
} else if (value < node.value) {
72+
} if (value < node.value) {
7373
return this.findNodeAndParent(value, node.left, node);
7474
}
7575
return this.findNodeAndParent(value, node.right, node);

‎src/runtimes/02-binary-search.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ function binarySearchRecursive(array, search, offset = 0) {
1818

1919
if (current === search) {
2020
return offset + half;
21-
} else if (array.length < 2) {
21+
} if (array.length < 2) {
2222
return -1;
23-
} else if (search > current) {
23+
} if (search > current) {
2424
const right = array.slice(half);
2525
return binarySearchRecursive(right, search, offset + half);
2626
}
@@ -49,7 +49,7 @@ function binarySearchIterative(array, search) {
4949

5050
if (current === search) {
5151
return currentIndex;
52-
} else if (search > current) {
52+
} if (search > current) {
5353
start = currentIndex;
5454
} else if (search < current) {
5555
end = currentIndex;

0 commit comments

Comments
 (0)
Please sign in to comment.