Skip to content

Commit d38ebe5

Browse files
authored
chore: Merge pull request TheAlgorithms#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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 16 additions & 13 deletions
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

Lines changed: 3 additions & 14 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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
})
Lines changed: 14 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 7 additions & 1 deletion
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

0 commit comments

Comments
 (0)