Skip to content

Commit 86d333e

Browse files
defaudegithub-actionsappgurueu
authored
feat: Test running overhaul, switch to Prettier & reformat everything (TheAlgorithms#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see TheAlgorithms#1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <[email protected]>
1 parent 0ca18c2 commit 86d333e

File tree

392 files changed

+6350
-17123
lines changed

Some content is hidden

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

392 files changed

+6350
-17123
lines changed

.github/workflows/Ci.yml

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@ jobs:
1515

1616
- uses: actions/setup-node@v3
1717
with:
18-
node-version: 16
18+
node-version: 20
1919
cache: npm
2020

2121
- name: 📦 Install dependencies
2222
run: npm ci
2323

2424
- name: 🧪 Run all tests
25-
if: ${{ github.event_name == 'push' }}
2625
run: npm run test
2726

28-
- name: 🧪 Run tests for changed files only
29-
if: ${{ github.event_name == 'pull_request' }}
30-
run: npm run test-changed
31-
3227
- name: 💄 Code style
3328
run: npm run style
3429

.github/workflows/UpdateDirectory.mjs .github/workflows/UpdateDirectory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ globby([
6363
"!**/test/**/*",
6464
'!**/*.test.js',
6565
'!**/*.manual-test.js',
66-
'!babel.config.js'
66+
'!vitest.config.ts'
6767
])
6868
// create markdown content
6969
.then(pathsToMarkdown)

.github/workflows/UpdateDirectory.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v3
14+
1415
- uses: actions/setup-node@v3
1516
with:
16-
node-version: 16
17+
node-version: 20
1718
cache: npm
1819

1920
- name: 📦 Install dependencies
2021
run: npm ci
2122

2223
- name: 🗄️ Create Directory from JS files
23-
run: node .github/workflows/UpdateDirectory.mjs
24+
run: node .github/workflows/UpdateDirectory.js
2425

2526
- name: Configure Github Action
2627
run: |

.husky/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
. "$(dirname "$0")/_/husky.sh"
33

44
npm run style
5-
npm run test-changed
5+
npm run test

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.github
2+
DIRECTORY.md

Backtracking/AllCombinationsOfSizeK.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323

2424
class Combinations {
25-
constructor (n, k) {
25+
constructor(n, k) {
2626
this.n = n
2727
this.k = k
2828
this.current = [] // will be used for storing current combination
2929
this.combinations = []
3030
}
3131

32-
findCombinations (high = this.n, total = this.k, low = 1) {
32+
findCombinations(high = this.n, total = this.k, low = 1) {
3333
if (total === 0) {
3434
this.combinations.push([...this.current])
3535
return this.combinations

Backtracking/GeneratePermutations.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
*/
1111

1212
const swap = (arr, i, j) => {
13-
const newArray = [...arr];
13+
const newArray = [...arr]
1414

15-
[newArray[i], newArray[j]] = [newArray[j], newArray[i]] // Swapping elements ES6 way
15+
;[newArray[i], newArray[j]] = [newArray[j], newArray[i]] // Swapping elements ES6 way
1616

1717
return newArray
1818
}
1919

20-
const permutations = arr => {
20+
const permutations = (arr) => {
2121
const P = []
2222
const permute = (arr, low, high) => {
2323
if (low === high) {

Backtracking/KnightTour.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Wikipedia: https://en.wikipedia.org/wiki/Knight%27s_tour
22

33
class OpenKnightTour {
4-
constructor (size) {
4+
constructor(size) {
55
this.board = new Array(size).fill(0).map(() => new Array(size).fill(0))
66
this.size = size
77
}
88

9-
getMoves ([i, j]) {
9+
getMoves([i, j]) {
1010
// helper function to get the valid moves of the knight from the current position
1111
const moves = [
1212
[i + 2, j - 1],
@@ -19,15 +19,17 @@ class OpenKnightTour {
1919
[i - 1, j + 2]
2020
]
2121

22-
return moves.filter(([y, x]) => y >= 0 && y < this.size && x >= 0 && x < this.size)
22+
return moves.filter(
23+
([y, x]) => y >= 0 && y < this.size && x >= 0 && x < this.size
24+
)
2325
}
2426

25-
isComplete () {
27+
isComplete() {
2628
// helper function to check if the board is complete
27-
return !this.board.map(row => row.includes(0)).includes(true)
29+
return !this.board.map((row) => row.includes(0)).includes(true)
2830
}
2931

30-
solve () {
32+
solve() {
3133
// function to find the solution for the given board
3234
for (let i = 0; i < this.size; i++) {
3335
for (let j = 0; j < this.size; j++) {
@@ -37,7 +39,7 @@ class OpenKnightTour {
3739
return false
3840
}
3941

40-
solveHelper ([i, j], curr) {
42+
solveHelper([i, j], curr) {
4143
// helper function for the main computation
4244
if (this.isComplete()) return true
4345

@@ -52,7 +54,7 @@ class OpenKnightTour {
5254
return false
5355
}
5456

55-
printBoard (output = value => console.log(value)) {
57+
printBoard(output = (value) => console.log(value)) {
5658
// utility function to display the board
5759
for (const row of this.board) {
5860
let string = ''

Backtracking/NQueens.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class NQueens {
2-
constructor (size) {
2+
constructor(size) {
33
if (size < 0) {
44
throw RangeError('Invalid board size')
55
}
@@ -8,7 +8,7 @@ class NQueens {
88
this.solutionCount = 0
99
}
1010

11-
isValid ([row, col]) {
11+
isValid([row, col]) {
1212
// function to check if the placement of the queen in the given location is valid
1313

1414
// checking the left of the current row
@@ -29,15 +29,15 @@ class NQueens {
2929
return true
3030
}
3131

32-
placeQueen (row, col) {
32+
placeQueen(row, col) {
3333
this.board[row][col] = 'Q'
3434
}
3535

36-
removeQueen (row, col) {
36+
removeQueen(row, col) {
3737
this.board[row][col] = '.'
3838
}
3939

40-
solve (col = 0) {
40+
solve(col = 0) {
4141
if (col >= this.size) {
4242
this.solutionCount++
4343
return true
@@ -54,7 +54,7 @@ class NQueens {
5454
return false
5555
}
5656

57-
printBoard (output = value => console.log(value)) {
57+
printBoard(output = (value) => console.log(value)) {
5858
if (!output._isMockFunction) {
5959
output('\n')
6060
}

Backtracking/RatInAMaze.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@
2121
* @param grid The grid to check.
2222
* @throws TypeError When the given grid is invalid.
2323
*/
24-
function validateGrid (grid) {
25-
if (!Array.isArray(grid) || grid.length === 0) throw new TypeError('Grid must be a non-empty array')
24+
function validateGrid(grid) {
25+
if (!Array.isArray(grid) || grid.length === 0)
26+
throw new TypeError('Grid must be a non-empty array')
2627

27-
const allRowsHaveCorrectLength = grid.every(row => row.length === grid.length)
28+
const allRowsHaveCorrectLength = grid.every(
29+
(row) => row.length === grid.length
30+
)
2831
if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')
2932

30-
const allCellsHaveValidValues = grid.every(row => {
31-
return row.every(cell => cell === 0 || cell === 1)
33+
const allCellsHaveValidValues = grid.every((row) => {
34+
return row.every((cell) => cell === 0 || cell === 1)
3235
})
33-
if (!allCellsHaveValidValues) throw new TypeError('Grid must only contain 0s and 1s')
36+
if (!allCellsHaveValidValues)
37+
throw new TypeError('Grid must only contain 0s and 1s')
3438
}
3539

36-
function isSafe (grid, x, y) {
40+
function isSafe(grid, x, y) {
3741
const n = grid.length
3842
return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1
3943
}
@@ -48,7 +52,7 @@ function isSafe (grid, x, y) {
4852
* @param path The path we took to get from the source cell to the current location.
4953
* @returns {string|boolean} Either the path to the target cell or false.
5054
*/
51-
function getPathPart (grid, x, y, solution, path) {
55+
function getPathPart(grid, x, y, solution, path) {
5256
const n = grid.length
5357

5458
// are we there yet?
@@ -89,7 +93,7 @@ function getPathPart (grid, x, y, solution, path) {
8993
return false
9094
}
9195

92-
function getPath (grid) {
96+
function getPath(grid) {
9397
// grid dimensions
9498
const n = grid.length
9599

@@ -108,7 +112,7 @@ function getPath (grid) {
108112
* Creates an instance of the "rat in a maze" based on a given grid (maze).
109113
*/
110114
export class RatInAMaze {
111-
constructor (grid) {
115+
constructor(grid) {
112116
// first, let's do some error checking on the input
113117
validateGrid(grid)
114118

Backtracking/Sudoku.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Sudoku {
22
// Sudoku Class to hold the board and related functions
3-
constructor (board) {
3+
constructor(board) {
44
this.board = board
55
}
66

7-
findEmptyCell () {
7+
findEmptyCell() {
88
// Find a empty cell in the board (returns [-1, -1] if all cells are filled)
99
for (let i = 0; i < 9; i++) {
1010
for (let j = 0; j < 9; j++) {
@@ -14,7 +14,7 @@ class Sudoku {
1414
return [-1, -1]
1515
}
1616

17-
check ([y, x], value) {
17+
check([y, x], value) {
1818
// checks if the value to be added in the board is an acceptable value for the cell
1919

2020
// checking through the row
@@ -29,16 +29,16 @@ class Sudoku {
2929
// checking through the 3x3 block of the cell
3030
const secRow = Math.floor(y / 3)
3131
const secCol = Math.floor(x / 3)
32-
for (let i = (secRow * 3); i < ((secRow * 3) + 3); i++) {
33-
for (let j = (secCol * 3); j < ((secCol * 3) + 3); j++) {
32+
for (let i = secRow * 3; i < secRow * 3 + 3; i++) {
33+
for (let j = secCol * 3; j < secCol * 3 + 3; j++) {
3434
if (y !== i && x !== j && this.board[i][j] === value) return false
3535
}
3636
}
3737

3838
return true
3939
}
4040

41-
solve () {
41+
solve() {
4242
const [y, x] = this.findEmptyCell()
4343

4444
// checking if the board is complete
@@ -56,20 +56,23 @@ class Sudoku {
5656
return false
5757
}
5858

59-
getSection (row, [start, end]) {
59+
getSection(row, [start, end]) {
6060
return this.board[row].slice(start, end)
6161
}
6262

63-
printBoard (output = (...v) => console.log(...v)) {
63+
printBoard(output = (...v) => console.log(...v)) {
6464
// helper function to display board
6565
for (let i = 0; i < 9; i++) {
6666
if (i % 3 === 0 && i !== 0) {
6767
output('- - - - - - - - - - - -')
6868
}
6969
output(
70-
...this.getSection(i, [0, 3]), ' | ',
71-
...this.getSection(i, [3, 6]), ' | ',
72-
...this.getSection(i, [6, 9]))
70+
...this.getSection(i, [0, 3]),
71+
' | ',
72+
...this.getSection(i, [3, 6]),
73+
' | ',
74+
...this.getSection(i, [6, 9])
75+
)
7376
}
7477
}
7578
}

Backtracking/tests/AllCombinationsOfSizeK.test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ 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([
7+
[1, 2],
8+
[1, 3],
9+
[2, 3]
10+
])
711
})
812

913
it('should return 6x2 matrix solution for n = 4 and k = 2', () => {
1014
const test2 = new Combinations(4, 2)
11-
expect(test2.findCombinations()).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
15+
expect(test2.findCombinations()).toEqual([
16+
[1, 2],
17+
[1, 3],
18+
[1, 4],
19+
[2, 3],
20+
[2, 4],
21+
[3, 4]
22+
])
1223
})
1324
})
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { generateParentheses } from '../generateParentheses'
22

33
test('generate all valid parentheses of input 3', () => {
4-
expect(generateParentheses(3)).toStrictEqual(['((()))', '(()())', '(())()', '()(())', '()()()'])
4+
expect(generateParentheses(3)).toStrictEqual([
5+
'((()))',
6+
'(()())',
7+
'(())()',
8+
'()(())',
9+
'()()()'
10+
])
511
})

Backtracking/tests/NQueens.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ describe('NQueens', () => {
1414
})
1515

1616
it('should throw RangeError for negative size board', () => {
17-
expect(() => { return new NQueens(-1) }).toThrow(RangeError)
17+
expect(() => {
18+
return new NQueens(-1)
19+
}).toThrow(RangeError)
1820
})
1921
})

0 commit comments

Comments
 (0)