Skip to content

Commit d38ebe5

Browse files
authored
chore: Merge pull request #768 from lvlte/issues/720
Changes for consolidation
2 parents 8fa8244 + 274686a commit d38ebe5

File tree

153 files changed

+1085
-1215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+1085
-1215
lines changed

Backtracking/AllCombinationsOfSizeK.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ class Combinations {
2525
constructor (n, k) {
2626
this.n = n
2727
this.k = k
28-
this.combinationArray = [] // will be used for storing current combination
28+
this.current = [] // will be used for storing current combination
29+
this.combinations = []
2930
}
3031

3132
findCombinations (high = this.n, total = this.k, low = 1) {
3233
if (total === 0) {
33-
console.log(this.combinationArray)
34-
return
34+
this.combinations.push([...this.current])
35+
return this.combinations
3536
}
3637
for (let i = low; i <= high; i++) {
37-
this.combinationArray.push(i)
38+
this.current.push(i)
3839
this.findCombinations(high, total - 1, i + 1)
39-
this.combinationArray.pop()
40+
this.current.pop()
4041
}
42+
return this.combinations
4143
}
4244
}
4345

Backtracking/GeneratePermutations.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Problem Statement: Generate all distinct permutations of a string/array (all permutations should be in sorted order);
2+
* Problem Statement: Generate all distinct permutations of a an array (all permutations should be in sorted order);
33
*
44
* What is permutations?
5-
* - Permutation means possible arrangements in a set (here it is string/array);
5+
* - Permutation means possible arrangements in a set (here it is an array);
66
*
77
* Reference to know more about permutations:
88
* - https://www.britannica.com/science/permutation
@@ -17,17 +17,20 @@ const swap = (arr, i, j) => {
1717
return newArray
1818
}
1919

20-
const permutations = (arr, low, high) => {
21-
if (low === high) {
22-
console.log(arr.join(' '))
23-
return
24-
}
25-
for (let i = low; i <= high; i++) {
26-
arr = swap(arr, low, i)
27-
permutations(arr, low + 1, high)
20+
const permutations = arr => {
21+
const P = []
22+
const permute = (arr, low, high) => {
23+
if (low === high) {
24+
P.push([...arr])
25+
return P
26+
}
27+
for (let i = low; i <= high; i++) {
28+
arr = swap(arr, low, i)
29+
permute(arr, low + 1, high)
30+
}
31+
return P
2832
}
33+
return permute(arr, 0, arr.length - 1)
2934
}
3035

31-
// Driver Code
32-
const input = [1, 2, 3]
33-
permutations(input, 0, input.length - 1)
36+
export { permutations }

Backtracking/KnightTour.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,16 @@ class OpenKnightTour {
5252
return false
5353
}
5454

55-
printBoard () {
55+
printBoard (output = value => console.log(value)) {
5656
// utility function to display the board
5757
for (const row of this.board) {
5858
let string = ''
5959
for (const elem of row) {
6060
string += elem + '\t'
6161
}
62-
console.log(string)
62+
output(string)
6363
}
6464
}
6565
}
6666

67-
function main () {
68-
const board = new OpenKnightTour(5)
69-
70-
board.printBoard()
71-
console.log('\n')
72-
73-
board.solve()
74-
75-
board.printBoard()
76-
}
77-
78-
main()
67+
export { OpenKnightTour }

Backtracking/NQueen.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class NQueen {
3636

3737
solve (col = 0) {
3838
if (col >= this.size) {
39-
this.printBoard()
4039
this.solutionCount++
4140
return true
4241
}
@@ -52,10 +51,12 @@ class NQueen {
5251
return false
5352
}
5453

55-
printBoard () {
56-
console.log('\n')
54+
printBoard (output = value => console.log(value)) {
55+
if (!output._isMockFunction) {
56+
output('\n')
57+
}
5758
for (const row of this.board) {
58-
console.log(...row)
59+
output(row)
5960
}
6061
}
6162
}

Backtracking/Sudoku.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class Sudoku {
6060
return this.board[row].slice(start, end)
6161
}
6262

63-
printBoard () {
63+
printBoard (output = (...v) => console.log(...v)) {
6464
// helper function to display board
6565
for (let i = 0; i < 9; i++) {
66-
if (i % 3 === 0 && i !== 0) console.log('- - - - - - - - - - - -')
67-
console.log(
66+
if (i % 3 === 0 && i !== 0) {
67+
output('- - - - - - - - - - - -')
68+
}
69+
output(
6870
...this.getSection(i, [0, 3]), ' | ',
6971
...this.getSection(i, [3, 6]), ' | ',
7072
...this.getSection(i, [6, 9]))

Backtracking/tests/AllCombinationsOfSizeK.test.mjs renamed to Backtracking/tests/AllCombinationsOfSizeK.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { Combinations } from '../AllCombinationsOfSizeK'
33
describe('AllCombinationsOfSizeK', () => {
44
it('should return 3x2 matrix solution for n = 3 and k = 2', () => {
55
const test1 = new Combinations(3, 2)
6-
expect(test1.findCombinations).toEqual([[1, 2], [1, 3], [2, 3]])
6+
expect(test1.findCombinations()).toEqual([[1, 2], [1, 3], [2, 3]])
77
})
88

9-
it('should return 6x2 matrix solution for n = 3 and k = 2', () => {
9+
it('should return 6x2 matrix solution for n = 4 and k = 2', () => {
1010
const test2 = new Combinations(4, 2)
11-
expect(test2.findCombinations).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
11+
expect(test2.findCombinations()).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
1212
})
1313
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { permutations } from '../GeneratePermutations'
2+
3+
describe('Permutations', () => {
4+
it('Permutations of [1, 2, 3]', () => {
5+
expect(permutations([1, 2, 3])).toEqual([
6+
[1, 2, 3],
7+
[1, 3, 2],
8+
[2, 1, 3],
9+
[2, 3, 1],
10+
[3, 1, 2],
11+
[3, 2, 1]
12+
])
13+
})
14+
})

Backtracking/tests/KnightTour.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { OpenKnightTour } from '../KnightTour'
2+
3+
describe('OpenKnightTour', () => {
4+
it('OpenKnightTour(5)', () => {
5+
const KT = new OpenKnightTour(5)
6+
expect(KT.board).toEqual([
7+
[0, 0, 0, 0, 0],
8+
[0, 0, 0, 0, 0],
9+
[0, 0, 0, 0, 0],
10+
[0, 0, 0, 0, 0],
11+
[0, 0, 0, 0, 0]
12+
])
13+
14+
KT.solve()
15+
expect(KT.board).toEqual([
16+
[19, 4, 15, 10, 25],
17+
[14, 9, 18, 5, 16],
18+
[1, 20, 3, 24, 11],
19+
[8, 13, 22, 17, 6],
20+
[21, 2, 7, 12, 23]
21+
])
22+
})
23+
})

Bit-Manipulation/BinaryCountSetBits.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ function BinaryCountSetBits (a) {
1313
return a.toString(2).split('1').length - 1
1414
}
1515

16-
// Run `binary_and` Function to find the binary and operation
17-
console.log(BinaryCountSetBits(251))
16+
export { BinaryCountSetBits }

CONTRIBUTING.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ should add unique value.
6161
* **Example:**`UserProfile.js` is allowed but `userprofile.js`,`Userprofile.js`,`user-Profile.js`,`userProfile.js` are
6262
not.
6363

64+
#### Module System
65+
66+
We use the [ES Module](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) system, which bring an official, standardized module system to JavaScript.
67+
68+
It roughly means you will need to use `export` and `import` statements instead of `module.exports` and `require()`.
69+
6470
#### Testing
6571

6672
Be confident that your code works. When was the last time you committed a code change, your build failed, and half of
@@ -125,7 +131,7 @@ function sumOfArray (arrayOfNumbers) {
125131
return (sum)
126132
}
127133
```
128-
*
134+
*
129135
* Avoid using global variables and avoid `==`
130136
* Please use `let` over `var`
131137
* Please refrain from using `console.log` or any other console methods

Cache/LFUCache.js

+1-38
Original file line numberDiff line numberDiff line change
@@ -103,41 +103,4 @@ class LFUCache {
103103
}
104104
}
105105

106-
function main () {
107-
// Example 1 (Small Cache)
108-
const cache = new LFUCache(2)
109-
cache.set(1, 1)
110-
cache.set(2, 2)
111-
112-
console.log(cache.get(1))
113-
114-
cache.set(3, 3)
115-
116-
console.log(cache.get(2)) // cache miss
117-
118-
cache.set(4, 4)
119-
120-
console.log(cache.get(1)) // cache miss
121-
console.log(cache.get(3))
122-
console.log(cache.get(4))
123-
124-
console.log('Example Cache: ', cache.cacheInfo(), '\n')
125-
126-
// Example 2 (Computing Fibonacci Series - 100 terms)
127-
function fib (num, cache = null) {
128-
if (cache) {
129-
const value = cache.get(num)
130-
if (value) { return value }
131-
}
132-
if (num === 1 || num === 2) { return 1 }
133-
const result = fib(num - 1, cache) + fib(num - 2, cache)
134-
if (cache) { cache.set(num, result) }
135-
return result
136-
}
137-
138-
const fibCache = new LFUCache(100)
139-
for (let i = 1; i <= 100; i++) { fib(i, fibCache) }
140-
console.log('Fibonacci Series Cache: ', fibCache.cacheInfo(), '\n')
141-
}
142-
143-
main()
106+
export { LFUCache }

Cache/LRUCache.js

+1-38
Original file line numberDiff line numberDiff line change
@@ -86,41 +86,4 @@ class LRUCache {
8686
}
8787
}
8888

89-
function main () {
90-
// Example 1 (Small Cache)
91-
const cache = new LRUCache(2)
92-
cache.set(1, 1)
93-
cache.set(2, 2)
94-
95-
console.log(cache.get(1))
96-
97-
cache.set(3, 3)
98-
99-
console.log(cache.get(2)) // cache miss
100-
101-
cache.set(4, 4)
102-
103-
console.log(cache.get(1)) // cache miss
104-
console.log(cache.get(3))
105-
console.log(cache.get(4))
106-
107-
console.log('Example Cache: ', cache.cacheInfo(), '\n')
108-
109-
// Example 2 (Computing Fibonacci Series - 100 terms)
110-
function fib (num, cache = null) {
111-
if (cache) {
112-
const value = cache.get(num)
113-
if (value) { return value }
114-
}
115-
if (num === 1 || num === 2) { return 1 }
116-
const result = fib(num - 1, cache) + fib(num - 2, cache)
117-
if (cache) { cache.set(num, result) }
118-
return result
119-
}
120-
121-
const fibCache = new LRUCache(100)
122-
for (let i = 1; i <= 100; i++) { fib(i, fibCache) }
123-
console.log('Fibonacci Series Cache: ', fibCache.cacheInfo(), '\n')
124-
}
125-
126-
main()
89+
export { LRUCache }

Cache/test/LFUCache.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { LFUCache } from '../LFUCache'
2+
3+
describe('LFUCache', () => {
4+
it('Example 1 (Small Cache, size=2)', () => {
5+
const cache = new LFUCache(2)
6+
cache.set(1, 1)
7+
cache.set(2, 2)
8+
9+
expect(cache.get(1)).toBe(1)
10+
expect(cache.get(2)).toBe(2)
11+
12+
// Additional entries triggers cache rotate
13+
cache.set(3, 3)
14+
15+
// Then we should have a cache miss for the first entry added
16+
expect(cache.get(1)).toBe(null)
17+
expect(cache.get(2)).toBe(2)
18+
expect(cache.get(3)).toBe(3)
19+
20+
cache.set(4, 4)
21+
expect(cache.get(1)).toBe(null) // cache miss
22+
expect(cache.get(2)).toBe(null) // cache miss
23+
expect(cache.get(3)).toBe(3)
24+
expect(cache.get(4)).toBe(4)
25+
26+
expect(cache.cacheInfo()).toBe('CacheInfo(hits=6, misses=3, capacity=2, current size=2)')
27+
})
28+
29+
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
30+
const cache = new LFUCache(100)
31+
for (let i = 1; i <= 100; i++) {
32+
fib(i, cache)
33+
}
34+
expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)')
35+
})
36+
})
37+
38+
// Helper for building and caching Fibonacci series
39+
function fib (num, cache = null) {
40+
if (cache) {
41+
const value = cache.get(num)
42+
if (value) {
43+
return value
44+
}
45+
}
46+
if (num === 1 || num === 2) {
47+
return 1
48+
}
49+
const result = fib(num - 1, cache) + fib(num - 2, cache)
50+
if (cache) {
51+
cache.set(num, result)
52+
}
53+
return result
54+
}

0 commit comments

Comments
 (0)