Skip to content

Commit a37062e

Browse files
authoredOct 27, 2019
Merge pull request knaxus#142 from knaxus/codeCoverage
Increase code coverage
2 parents 1ecf0de + 980dcf9 commit a37062e

File tree

27 files changed

+474
-204
lines changed

27 files changed

+474
-204
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Node {
2+
constructor(data, previous, next) {
3+
this.data = data;
4+
this.previous = previous;
5+
this.next = next;
6+
}
7+
}
8+
module.exports = Node;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const DLL = require('.');
2+
3+
describe('Doubly Linked List', () => {
4+
it('Doubly linked list should be class', () => {
5+
expect(typeof DLL.prototype.constructor).toEqual('function');
6+
});
7+
8+
const doublyLinkedList = new DLL();
9+
10+
it('It should create a DLL', () => {
11+
expect(doublyLinkedList.head.next).toEqual(doublyLinkedList.tail);
12+
expect(doublyLinkedList.tail.previous).toEqual(doublyLinkedList.head);
13+
expect(doublyLinkedList.length()).toEqual(0);
14+
});
15+
16+
it('It should add at beginning (addAtBeginning)', () => {
17+
doublyLinkedList.addAtBeginning(1);
18+
doublyLinkedList.addAtBeginning(2);
19+
doublyLinkedList.addAtBeginning(3);
20+
expect(doublyLinkedList.traverse()).toEqual([3, 2, 1]);
21+
});
22+
23+
it('It should add at end (addAtEnd)', () => {
24+
doublyLinkedList.addAtEnd(1);
25+
doublyLinkedList.addAtEnd(2);
26+
doublyLinkedList.addAtEnd(3);
27+
expect(doublyLinkedList.traverse()).toEqual([3, 2, 1, 1, 2, 3]);
28+
});
29+
30+
it('It should remove at beginning (removeAtBeginning)', () => {
31+
doublyLinkedList.removeAtBeginning();
32+
doublyLinkedList.removeAtBeginning();
33+
doublyLinkedList.removeAtBeginning();
34+
expect(doublyLinkedList.traverse()).toEqual([1, 2, 3]);
35+
});
36+
37+
it('It should remove at end (removeAtEnd)', () => {
38+
doublyLinkedList.removeAtEnd();
39+
doublyLinkedList.removeAtEnd();
40+
doublyLinkedList.removeAtEnd();
41+
42+
expect(doublyLinkedList.traverse()).toEqual([]);
43+
});
44+
});

‎src/_DataStructures_/DoublyLinkedList/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
/* eslint-disable class-methods-use-this */
2-
class Node {
3-
constructor(data, previous, next) {
4-
this.data = data;
5-
this.previous = previous;
6-
this.next = next;
7-
}
8-
}
2+
const Node = require('./Node');
93

104
class DoublyLinkedList {
115
constructor() {

‎src/_DataStructures_/LinkedList/LinkedList.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('Data Structures: Linked Lists', () => {
6868
list.addAtEnd(10);
6969

7070
expect(list.length()).toEqual(4);
71+
expect(list.traverseList()).toEqual([15, 23, 33, 10]);
7172
});
7273
});
7374

‎src/_DataStructures_/Queue/Queue.test.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Queue = require('.');
22

33
describe('Data Structure : Queue', () => {
4-
it('Should be class', () => {
4+
it('Queue should be class', () => {
55
expect(typeof Queue.prototype.constructor).toEqual('function');
66
});
77

@@ -53,6 +53,30 @@ describe('Data Structure : Queue', () => {
5353
expect(queue.dequeue()).toEqual(1);
5454
expect(queue.dequeue()).toEqual(4);
5555
expect(queue.dequeue()).toEqual(3);
56+
expect(queue.dequeue()).toEqual(null);
57+
});
58+
59+
it('Shoud return size of Queue', () => {
60+
const queue2 = new Queue();
61+
queue2.enqueue(2);
62+
queue2.enqueue(1);
63+
queue2.enqueue(4);
64+
queue2.enqueue(3);
65+
expect(queue2.length()).toEqual(4);
66+
});
67+
68+
it('Should Destroy Queue', () => {
69+
queue.destroy();
70+
expect(queue.length()).toEqual(0);
71+
});
72+
73+
it('Override and throw error for other LL methods', () => {
74+
expect(() => { queue.addAtBeginning(); }).toThrowError('Not Allowed');
75+
expect(() => { queue.addAt(); }).toThrowError('Not Allowed');
76+
expect(() => { queue.removeFromEnd(); }).toThrowError('Not Allowed');
77+
expect(() => { queue.getLast(); }).toThrowError('Not Allowed');
78+
expect(() => { queue.getAt(); }).toThrowError('Not Allowed');
79+
expect(() => { queue.removeAt(); }).toThrowError('Not Allowed');
5680
});
5781
});
5882
});

‎src/_DataStructures_/Stack/postfix-expression-evaluation/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function evaluatePostfixExpression(expression) {
4343
s.push(val2 / val1);
4444
break;
4545
default:
46-
break;
46+
throw new Error('Operation is not valid');
4747
}
4848
}
4949
}

‎src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ describe('Postfix expression evaluation', () => {
5858
test.each(invalidExpressions)('running for %p', (expression) => {
5959
expect(() => evaluatePostfixExpression(expression)).toThrow(ERROR_STRING);
6060
});
61+
62+
expect(() => evaluatePostfixExpression('1&2')).toThrow('Operation is not valid');
63+
6164
});
6265
});

‎src/_DataStructures_/Trees/BinarySearchTree/BinarySearchTree.test.js

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Is Empty', () => {
5+
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
it('should return False when BST is not empty', () => {
9+
expect(bst.isEmpty()).toEqual(false);
10+
});
11+
12+
it('should return True when BST is empty', () => {
13+
keys.push(6);
14+
keys.forEach(el => bst.remove(el));
15+
expect(bst.isEmpty()).toEqual(true);
16+
});
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Find maximum value in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
9+
it('It should expect maximum key', () => {
10+
expect(bst.getMaximum()).toEqual(12);
11+
});
12+
13+
it('It should expect new maximum key added in BST', () => {
14+
bst.add(20);
15+
expect(bst.getMaximum()).toEqual(20);
16+
});
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('It should Find the minimum value in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
9+
it('It should expect minimum key', () => {
10+
expect(bst.getMinimum()).toEqual(2);
11+
});
12+
13+
it('It should expect new minimum key added to BST', () => {
14+
bst.add(1);
15+
expect(bst.getMinimum()).toEqual(1);
16+
});
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const BST = require('.');
2+
3+
describe('Data Structure : Binary Search Tree', () => {
4+
it('Binary Search Tree should be a Class', () => {
5+
expect(typeof BST.prototype.constructor).toEqual('function');
6+
});
7+
8+
describe('Binary Search Tree API', () => {
9+
let bst = null;
10+
11+
beforeEach(() => {
12+
bst = new BST(5);
13+
});
14+
15+
it('Should delete() an element from Binary Search Tree', () => {
16+
bst.add(4);
17+
bst.add(9);
18+
bst.add(2);
19+
bst.delete(bst.root, 4);
20+
expect(bst.traverseInorder()).toEqual([2, 5, 9]);
21+
bst.delete(bst.root, 2);
22+
expect(bst.traverseInorder()).toEqual([5, 9]);
23+
});
24+
25+
it('Should return NULL if root is empty', () => {
26+
const bst2 = new BST(6);
27+
bst2.remove(6);
28+
bst2.remove(9);
29+
expect(bst2.root).toEqual(null);
30+
});
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('It should Find the key in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
9+
it('It should return true for 8', () => {
10+
expect(bst.searchFor(8)).toEqual(true);
11+
});
12+
13+
it('It should return false for 100', () => {
14+
expect(bst.searchFor(100)).toEqual(false);
15+
});
16+
});
17+
});

‎src/_DataStructures_/Trees/BinarySearchTree/bst-deletion.test.js ‎src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/height-of-bst.test.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const BinarySearchTree = require('./index');
2-
const heightOfBST = require('./height-of-bst/index');
1+
const BinarySearchTree = require('../index');
2+
const heightOfBST = require('./index');
33

44
describe('Binary search tree traversals', () => {
55
let bst;
@@ -8,12 +8,8 @@ describe('Binary search tree traversals', () => {
88
it('should create BST', () => {
99
// Creates BST
1010
bst = new BinarySearchTree(6);
11-
bst.add(4);
12-
bst.add(9);
13-
bst.add(2);
14-
bst.add(5);
15-
bst.add(8);
16-
bst.add(12);
11+
const keys = [4, 9, 2, 5, 8, 12];
12+
keys.forEach(el => bst.add(el));
1713
});
1814
});
1915

@@ -48,4 +44,14 @@ describe('Binary search tree traversals', () => {
4844
expect(heightOfBST(bst.root)).toEqual(2);
4945
});
5046
});
47+
48+
describe('When root left subtree height is greater than right', () => {
49+
const bst2 = new BinarySearchTree(10);
50+
const keys = [11, 20, 9, 8, 7, 6, 5, 4];
51+
keys.forEach(el => bst2.add(el));
52+
53+
it('should return height of BST ', () => {
54+
expect(heightOfBST(bst2.root)).toEqual(7);
55+
});
56+
});
5157
});

‎src/_DataStructures_/Trees/BinarySearchTree/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable consistent-return */
12
const Node = require('./Node');
23

34
class BinarySearchTree {
@@ -23,7 +24,6 @@ class BinarySearchTree {
2324
root.rightChild = this.insert(root.rightChild, value);
2425
return root;
2526
}
26-
return root;
2727
}
2828

2929
preorder(root) {
@@ -80,7 +80,6 @@ class BinarySearchTree {
8080
if (value > root.value) {
8181
return this.search(root.rightChild, value);
8282
}
83-
return false;
8483
}
8584

8685
delete(root, value) {
@@ -169,7 +168,7 @@ class BinarySearchTree {
169168
}
170169

171170
remove(value) {
172-
return this.delete(this.root, value);
171+
this.root = this.delete(this.root, value);
173172
}
174173
}
175174

‎src/_DataStructures_/Trees/BinarySearchTree/lowest-common-ancestor/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
*
44
* Given values of two values n1 and n2 in a Binary Search Tree, find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree.
55
*/
6-
7-
function lca(node, n1, n2) {
8-
if (node == null)
9-
return null;
6+
7+
function lowestCommonAncestor(node, n1, n2) {
8+
if (node === null) return null;
109

1110
// If both n1 and n2 are smaller than root, then LCA lies in left
12-
if (node.value > n1 && node.value > n2)
13-
return lca(node.leftChild, n1, n2);
11+
if (node.value > n1 && node.value > n2) { return lowestCommonAncestor(node.leftChild, n1, n2); }
1412

1513
// If both n1 and n2 are greater than root, then LCA lies in right
16-
if (node.value < n1 && node.value < n2)
17-
return lca(node.rightChild, n1, n2);
14+
if (node.value < n1 && node.value < n2) { return lowestCommonAncestor(node.rightChild, n1, n2); }
1815

1916
return node;
2017
}
2118

2219
module.exports = {
23-
lca,
20+
lowestCommonAncestor,
2421
};
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
const { lca } = require('.');
1+
const { lowestCommonAncestor } = require('.');
22
const BinarySearchTree = require('../index');
33

44
// Quick JSON equivalent
55
// {"left":{"left":{"data":4},"right":{"left":{"data":10},"right":{"data":14},"data":12},"data":8},"right":{"data":22},"data":20}
66

7-
describe('LCA', () => {
7+
describe('Lowest Common Ancestor in BST', () => {
8+
const bst = new BinarySearchTree(20);
9+
const keys = [22, 8, 12, 4, 14, 10];
10+
keys.forEach(el => bst.add(el));
811

9-
let bst = new BinarySearchTree(20);
10-
bst.add(22);
11-
bst.add(8);
12-
bst.add(12);
13-
bst.add(4);
14-
bst.add(14);
15-
bst.add(10);
16-
17-
it('Should return 12', () => {
18-
expect(lca(bst.root, 10, 14).value).toEqual(12);
19-
});
20-
21-
it('Should return 8', () => {
22-
expect(lca(bst.root, 14, 8).value).toEqual(8);
12+
it('Should return Lowest Common Ancestor Node ', () => {
13+
expect(lowestCommonAncestor(bst.root, 10, 14).value).toEqual(12);
14+
expect(lowestCommonAncestor(bst.root, 14, 8).value).toEqual(8);
15+
expect(lowestCommonAncestor(bst.root, 10, 22).value).toEqual(20);
2316
});
24-
25-
it('Should return 20', () => {
26-
expect(lca(bst.root, 10, 22).value).toEqual(20);
17+
18+
const bst2 = new BinarySearchTree(6);
19+
bst2.remove(6);
20+
21+
it('Should return Null when root is null', () => {
22+
expect(lowestCommonAncestor(bst2.root, 10, 22)).toEqual(null);
2723
});
2824
});

‎src/_DataStructures_/Trees/Trie/total-words-in-trie/total-words-in-trie.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const totalWordsInTrie = require('./index')
2-
const Trie = require('../index');
31
const assert = require('assert');
2+
const totalWordsInTrie = require('./index');
3+
const Trie = require('../index');
44

55
describe('Data Structure : Trie', () => {
66
it('Should be class of type Trie', () => {
@@ -11,24 +11,24 @@ describe('Data Structure : Trie', () => {
1111
describe('Trie', () => {
1212

1313
it('Should return 6.', () => {
14-
let newTrie = new Trie();
14+
const newTrie = new Trie();
1515
const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
1616
words.forEach(word => newTrie.insert(word));
17-
result = totalWordsInTrie(newTrie.root);
17+
const result = totalWordsInTrie(newTrie.root);
1818
assert.equal(result, 6);
1919
});
2020

2121
it('Should return 0.', () => {
22-
let newTrie = new Trie();
23-
result = totalWordsInTrie(newTrie.root);
22+
const newTrie = new Trie();
23+
const result = totalWordsInTrie(newTrie.root);
2424
assert.equal(result, 0);
2525
});
2626

2727
it('Should return 6.', () => {
28-
let newTrie = new Trie();
28+
const newTrie = new Trie();
2929
const words = ['bed', 'ball','', 'apple', 'java', 'javascript', 'bed'];
3030
words.forEach(word => newTrie.insert(word));
31-
result = totalWordsInTrie(newTrie.root);
31+
const result = totalWordsInTrie(newTrie.root);
3232
assert.equal(result, 6);
3333
});
3434
});

‎src/_PathFinder_/AStar/AStar.test.js

+72
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,76 @@ describe('A*', () => {
119119
expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint is unreachable');
120120
});
121121
});
122+
describe('Completes grid successfully when no block', () => {
123+
it('A*', () => {
124+
const inputGrid = [
125+
[1, 1, 1, 1, 1],
126+
[1, 1, 1, 1, 1],
127+
[1, 1, 1, 1, 1],
128+
[1, 1, 1, 1, 1],
129+
[1, 1, 1, 1, 1],
130+
];
131+
const ROW = inputGrid.length;
132+
const COL = inputGrid[0].length;
133+
const start = {
134+
i: 2,
135+
j: 2,
136+
};
137+
const end1 = {
138+
i: 0,
139+
j: 0,
140+
};
141+
const completedPath1 = [[0, 0], [1, 1], [2, 2]];
142+
expect(AStar(start, end1, ROW, COL, inputGrid)).toEqual(completedPath1);
143+
144+
const end2 = {
145+
i: 0,
146+
j: 2,
147+
};
148+
const completedPath2 = [[0, 2], [1, 2], [2, 2]];
149+
expect(AStar(start, end2, ROW, COL, inputGrid)).toEqual(completedPath2);
150+
151+
const end3 = {
152+
i: 0,
153+
j: 4,
154+
};
155+
const completedPath3 = [[0, 4], [1, 3], [2, 2]];
156+
expect(AStar(start, end3, ROW, COL, inputGrid)).toEqual(completedPath3);
157+
158+
const end4 = {
159+
i: 2,
160+
j: 4,
161+
};
162+
const completedPath4 = [[2, 4], [2, 3], [2, 2]];
163+
expect(AStar(start, end4, ROW, COL, inputGrid)).toEqual(completedPath4);
164+
165+
const end5 = {
166+
i: 4,
167+
j: 4,
168+
};
169+
const completedPath5 = [[4, 4], [3, 3], [2, 2]];
170+
expect(AStar(start, end5, ROW, COL, inputGrid)).toEqual(completedPath5);
171+
172+
const end6 = {
173+
i: 4,
174+
j: 2,
175+
};
176+
const completedPath6 = [[4, 2], [3, 2], [2, 2]];
177+
expect(AStar(start, end6, ROW, COL, inputGrid)).toEqual(completedPath6);
178+
179+
const end7 = {
180+
i: 4,
181+
j: 0,
182+
};
183+
const completedPath7 = [[4, 0], [3, 1], [2, 2]];
184+
expect(AStar(start, end7, ROW, COL, inputGrid)).toEqual(completedPath7);
185+
186+
const end8 = {
187+
i: 2,
188+
j: 0,
189+
};
190+
const completedPath8 = [[2, 0], [2, 1], [2, 2]];
191+
expect(AStar(start, end8, ROW, COL, inputGrid)).toEqual(completedPath8);
192+
});
193+
});
122194
});

‎src/_PathFinder_/AStar/index.js

+62-36
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ function AStar(s, e, row, col, inputGrid) {
1111
const end = e;
1212
const path = [];
1313

14-
if (end.i >= inputGrid.length || end.j >= inputGrid[0].length) {
14+
const isValid = (i, j) => i >= 0 && j >= 0 && i < Row && j < Col;
15+
16+
17+
if (!isValid(start.i, start.j) || !isValid(end.i, end.j)) {
1518
throw new Error('Error: Endpoint outside grid bounds');
1619
}
1720

@@ -44,9 +47,6 @@ function AStar(s, e, row, col, inputGrid) {
4447
}
4548
}
4649

47-
48-
const isValid = (i, j) => i >= 0 && j >= 0 && i < Row && j < Col;
49-
5050
const isDestination = (i, j) => end.i === i && end.j === j;
5151

5252
const isBlocked = (i, j) => grid[i][j].cellValue === 0;
@@ -124,10 +124,6 @@ function AStar(s, e, row, col, inputGrid) {
124124
};
125125

126126
const search = () => {
127-
if (!isValid(start.i, start.j) || !isValid(end.i, end.j)) {
128-
return false;
129-
}
130-
131127
let i = start.i;
132128
let j = start.j;
133129
const openList = [];
@@ -185,36 +181,66 @@ function AStar(s, e, row, col, inputGrid) {
185181

186182

187183
// const inputGrid = [
188-
// [1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
189-
// [1, 1, 1, 0, 1, 1, 1, 0, 1, 1],
190-
// [1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
191-
// [0, 0, 1, 0, 1, 0, 0, 0, 0, 1],
192-
// [1, 1, 1, 0, 1, 1, 1, 0, 1, 0],
193-
// [1, 0, 1, 1, 1, 1, 0, 1, 0, 0],
194-
// [1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
195-
// [1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
196-
// [1, 1, 1, 0, 0, 0, 1, 0, 0, 1],
184+
// [1, 1, 1, 1, 1],
185+
// [1, 1, 1, 1, 1],
186+
// [1, 1, 1, 1, 1],
187+
// [1, 1, 1, 1, 1],
188+
// [1, 1, 1, 1, 1],
197189
// ];
190+
// const ROW = inputGrid.length;
191+
// const COL = inputGrid[0].length;
192+
// const start = {
193+
// i: 2,
194+
// j: 2,
195+
// };
196+
// const end1 = {
197+
// i: 0,
198+
// j: 0,
199+
// };
200+
// console.log(AStar(start, end1, ROW, COL, inputGrid));
201+
202+
// const end2 = {
203+
// i: 0,
204+
// j: 2,
205+
// };
206+
// console.log(AStar(start, end2, ROW, COL, inputGrid));
207+
208+
// const end3 = {
209+
// i: 0,
210+
// j: 4,
211+
// };
212+
// console.log(AStar(start, end3, ROW, COL, inputGrid));
213+
214+
// const end4 = {
215+
// i: 2,
216+
// j: 4,
217+
// };
218+
// console.log(AStar(start, end4, ROW, COL, inputGrid));
219+
220+
// const end5 = {
221+
// i: 4,
222+
// j: 4,
223+
// };
224+
// console.log(AStar(start, end5, ROW, COL, inputGrid));
225+
226+
// const end6 = {
227+
// i: 4,
228+
// j: 2,
229+
// };
230+
// console.log(AStar(start, end6, ROW, COL, inputGrid));
231+
232+
// const end7 = {
233+
// i: 4,
234+
// j: 0,
235+
// };
236+
// console.log(AStar(start, end7, ROW, COL, inputGrid));
237+
238+
// const end8 = {
239+
// i: 2,
240+
// j: 0,
241+
// };
242+
// console.log(AStar(start, end8, ROW, COL, inputGrid));
198243

199-
const inputGrid = [
200-
[1, 0, 0, 0, 0, 0],
201-
[1, 1, 1, 1, 1, 1],
202-
[1, 0, 0, 0, 0, 0],
203-
[1, 1, 1, 1, 1, 1],
204-
];
205-
206-
const ROW = inputGrid.length;
207-
const COL = inputGrid[0].length;
208-
const start = {
209-
i: 0,
210-
j: 0,
211-
};
212-
const end = {
213-
i: 3,
214-
j: 5,
215-
};
216-
217-
AStar(start, end, ROW, COL, inputGrid);
218244

219245

220246
module.exports = {

‎src/_Problems_/anagrams/anagrams.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,14 @@ describe('Anagrams', () => {
7575
}),
7676
).toBe(true);
7777
});
78+
79+
it('Should return FALSE for `Hello` & `Hallo`', () => {
80+
expect(
81+
checkAnagrams({
82+
firstString: 'Hello',
83+
secondString: 'Hallo',
84+
}),
85+
).toBe(false);
86+
});
7887
});
7988
});

‎src/_Problems_/get-smallest-common-number/get-smallest-common-number.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ describe('Get common smallest number between two integer arrays', () => {
2222
expect(getSmallestCommonNumber(arr1, arr2)).toEqual(-10);
2323
});
2424

25+
it('Should return common smallest number between unsorted two integer arrays', () => {
26+
const arr1 = [-10, 3, -11];
27+
const arr2 = [-11, 2, -10, 7];
28+
29+
expect(getSmallestCommonNumber(arr1, arr2)).toEqual(-11);
30+
});
31+
2532
it('Should return common smallest number between sorted two integer arrays', () => {
2633
const arr1 = [2, 3];
2734
const arr2 = [2, 5, 7];

‎src/_Problems_/get-smallest-common-number/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Get the common smallest number between two integer arrays
22

33
const getSmallestCommonNumber = (a1, a2) => {
4-
let map = {};
4+
const map = {};
55
let i = 0;
66
let min;
77

@@ -20,7 +20,7 @@ const getSmallestCommonNumber = (a1, a2) => {
2020
}
2121
}
2222

23-
i++;
23+
i += 1;
2424
}
2525

2626
return min || -1;
+29-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
const { jumpSearch, jumpSearchRecursive } = require('.');
1+
const { jumpSearch } = require('.');
22

3-
describe('Jump Search', () => {
4-
    const array = [1, 2, 3, 4, 5, 6, 7, 8];
5-
    describe('When element to find is at 1st position ', () => {
6-
        it('Jump search', () => {
7-
          expect(jumpSearch(array, 1)).toEqual(0);
8-
        });
9-
      });
10-
    describe('When element to find is at last position ', () => {
11-
        it('Jump search', () => {
12-
          expect(jumpSearch(array, 7)).toEqual(6);
13-
        });
14-
      });
15-
    describe('When element to find is at random position ', () => {
16-
        it('Jump search', () => {
17-
          expect(jumpSearch(array, 3)).toEqual(2);
18-
        });
19-
      });
20-
});
3+
describe('Jump Search', () => {
4+
const array = [1, 2, 3, 4, 5, 6, 7, 8, 20];
5+
describe('When element to find is at 1st position ', () => {
6+
it('Jump search', () => {
7+
expect(jumpSearch(array, 1)).toEqual(0);
8+
});
9+
});
10+
describe('When element to find is at last position ', () => {
11+
it('Jump search', () => {
12+
expect(jumpSearch(array, 20)).toEqual(8);
13+
});
14+
});
15+
describe('When element to find is at random position ', () => {
16+
it('Jump search', () => {
17+
expect(jumpSearch(array, 3)).toEqual(2);
18+
expect(jumpSearch(array, 5)).toEqual(4);
19+
expect(jumpSearch(array, 6)).toEqual(5);
20+
expect(jumpSearch(array, 8)).toEqual(7);
21+
});
22+
});
23+
describe('When element is not in array ', () => {
24+
it('Jump search', () => {
25+
expect(jumpSearch(array, 15)).toEqual(-1);
26+
expect(jumpSearch(array, 25)).toEqual(-1);
27+
expect(jumpSearch(array, 9)).toEqual(-1);
28+
});
29+
});
30+
});

‎src/_Searching_/JumpSearch/index.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
/**
22
* Note: Array must be sorted for jump search
3-
* Complexity:
3+
* Complexity:
44
* Worst case time complexity: O(√N)
55
* Average case time complexity: O(√N)
66
* Best case time complexity: O(1)
77
* Space complexity: O(1)
88
*/
99
function jumpSearch(arr, key) {
10-
const n = arr.length;
11-
const jump = Math.floor(Math.sqrt(n));
12-
let step = jump;
10+
const n = arr.length;
11+
const jump = Math.floor(Math.sqrt(n));
12+
let step = jump;
13+
let prev = 0;
1314

14-
let prev = 0;
15-
16-
while(arr[Math.min(step, n) - 1] < key) {
17-
prev = step;
18-
step += jump;
19-
if (prev >= n)
20-
return null;
21-
}
15+
while (arr[Math.min(step, n) - 1] < key) {
16+
prev = step;
17+
step += jump;
18+
if (prev >= n) { return -1; }
19+
}
20+
21+
while (arr[prev] < key && prev < Math.min(step, n)) {
22+
prev += 1;
23+
}
2224

23-
while(arr[prev] < key) {
24-
prev++;
25+
if (arr[prev] === key) { return prev; }
2526

26-
if (prev == Math.min(step, n))
27-
return null;
28-
}
29-
30-
if (arr[prev] == key)
31-
return prev;
27+
return -1;
28+
}
3229

33-
return null;
34-
}
35-
36-
module.exports = {
37-
jumpSearch,
38-
};
30+
module.exports = {
31+
jumpSearch,
32+
};

‎src/_Searching_/TernarySearch/TernarySearch.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ describe('Ternary Search', () => {
2424
describe('When element to find is at random position ', () => {
2525
it('Ternary search with Loop', () => {
2626
expect(ternarySearch(array, 3)).toEqual(2);
27+
expect(ternarySearch(array, 5)).toEqual(4);
2728
});
2829
it('Ternary serach with recursion', () => {
2930
expect(ternarySearchRecursive(array, low, high, 4)).toEqual(3);
3031
});
32+
it('Ternary serach with recursion', () => {
33+
expect(ternarySearchRecursive(array, low, high, 5)).toEqual(4);
34+
});
3135
});
3236
describe('When element to find is no present in array ', () => {
3337
it('Ternary search with Loop', () => {
@@ -37,5 +41,4 @@ describe('Ternary Search', () => {
3741
expect(ternarySearchRecursive(array, low, high, 10)).toEqual(null);
3842
});
3943
});
40-
4144
});
+38-39
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
11
/**
22
* Note: Array must be sorted for ternary search
3-
* Complexity:
3+
* Complexity:
44
* Worst case time complexity: O(log N)
55
* Average case time complexity: O(log N)
66
* Best case time complexity: O(1)
77
* Space complexity: O(1)
88
*/
9-
function ternarySearch(arr, key){
10-
let low = 0;
11-
let high = arr.length - 1;
12-
while(low <= high){
13-
let lowMiddle = low + Math.floor((high - low) / 3);
14-
let highMiddle = low + 2 * Math.floor((high - low) / 3);
15-
if(key == arr[low]){
16-
return low;
17-
} else if(key == arr[high]){
18-
return high;
19-
} else if(key <= arr[lowMiddle]){
20-
high = lowMiddle;
21-
} else if(key > arr[lowMiddle] && key <= arr[highMiddle]){
22-
low = lowMiddle + 1;
23-
high = highMiddle;
24-
} else {
25-
low = highMiddle + 1;
26-
}
9+
function ternarySearch(arr, key) {
10+
let low = 0;
11+
let high = arr.length - 1;
12+
while (low <= high) {
13+
const lowMiddle = low + Math.floor((high - low) / 3);
14+
const highMiddle = low + 2 * Math.floor((high - low) / 3);
15+
if (key === arr[low]) {
16+
return low;
17+
} if (key === arr[high]) {
18+
return high;
19+
} if (key <= arr[lowMiddle]) {
20+
high = lowMiddle;
21+
} else if (key > arr[lowMiddle] && key <= arr[highMiddle]) {
22+
low = lowMiddle + 1;
23+
high = highMiddle;
24+
} else {
25+
low = highMiddle + 1;
2726
}
28-
return null;
27+
}
28+
return null;
2929
}
3030

31-
function ternarySearchRecursive(arr, low, high, key){
32-
if(high >= low){
33-
let highMiddle = low + 2 * Math.floor((high - low) / 3);
34-
let lowMiddle = low + Math.floor((high - low) / 3);
35-
if(key === arr[lowMiddle]){
36-
return lowMiddle;
37-
} else if(key === arr[highMiddle]){
38-
return highMiddle;
39-
} else if(key < arr[lowMiddle]){
40-
return ternarySearchRecursive(arr, low, lowMiddle - 1, key);
41-
} else if(key > arr[lowMiddle] && key < arr[highMiddle]){
42-
return ternarySearchRecursive(arr, lowMiddle + 1, highMiddle - 1, key);
43-
} else {
44-
return ternarySearchRecursive(arr, highMiddle + 1, high, key);
45-
}
46-
}
47-
return null;
31+
function ternarySearchRecursive(arr, low, high, key) {
32+
if (high >= low) {
33+
const highMiddle = low + 2 * Math.floor((high - low) / 3);
34+
const lowMiddle = low + Math.floor((high - low) / 3);
35+
if (key === arr[lowMiddle]) {
36+
return lowMiddle;
37+
} if (key === arr[highMiddle]) {
38+
return highMiddle;
39+
} if (key < arr[lowMiddle]) {
40+
return ternarySearchRecursive(arr, low, lowMiddle - 1, key);
41+
} if (key > arr[lowMiddle] && key < arr[highMiddle]) {
42+
return ternarySearchRecursive(arr, lowMiddle + 1, highMiddle - 1, key);
43+
}
44+
return ternarySearchRecursive(arr, highMiddle + 1, high, key);
45+
}
46+
return null;
4847
}
4948

5049
module.exports = {
51-
ternarySearch,
52-
ternarySearchRecursive
50+
ternarySearch,
51+
ternarySearchRecursive,
5352
};

0 commit comments

Comments
 (0)
Please sign in to comment.