Skip to content

Commit 4d7149c

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 0830570 + 60443c7 commit 4d7149c

File tree

488 files changed

+10933
-17728
lines changed

Some content is hidden

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

488 files changed

+10933
-17728
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Bug report
2+
description: 'Create a report to help us improve'
3+
title: '[BUG]: '
4+
labels: ['bug']
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: '### Before you open an issue, please verify if a similar one already exists or has been closed before. More details about the process of contributing can be found in [CONTRIBUTING.md](https://github.com/TheAlgorithms/JavaScript/blob/master/CONTRIBUTING.md).'
9+
- type: textarea
10+
id: description
11+
attributes:
12+
label: Description
13+
description: Explain what the problem is.
14+
validations:
15+
required: true
16+
- type: textarea
17+
id: expectedbhv
18+
attributes:
19+
label: Expected Behavior
20+
description: Describe what was the expected behavior.
21+
validations:
22+
required: true
23+
- type: textarea
24+
id: actualbhv
25+
attributes:
26+
label: Actual Behavior
27+
description: Describe what actually happens.
28+
validations:
29+
required: true
30+
- type: textarea
31+
id: steps
32+
attributes:
33+
label: Steps to reproduce (if applicable)
34+
description: List steps to reproduce the behavior.
35+
placeholder: |
36+
1.
37+
2.
38+
3.
39+
4.
40+
validations:
41+
required: false
42+
- type: textarea
43+
id: extra_information
44+
attributes:
45+
label: Additional information
46+
description: Is there anything else we should know about this bug report?
47+
validations:
48+
required: false

.github/ISSUE_TEMPLATE/config.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
blank_issues_enabled: false
2+
contact_links:
3+
- name: Discord community
4+
url: https://the-algorithms.com/discord/
5+
about: Have any questions or found any bugs? Contact us via our Discord community.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Feature request
2+
description: 'Suggest features, propose improvements, discuss new ideas'
3+
title: '[FEATURE]: '
4+
labels: ['enhancement']
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
## This issue template is not for requesting new algorithms. For new algorithms, PRs should be opened directly.
10+
## Make sure your issue isn't a duplicate and you follow our [contributing guidelines](https://github.com/TheAlgorithms/JavaScript/blob/master/CONTRIBUTING.md)
11+
- type: textarea
12+
id: description
13+
attributes:
14+
label: Motivation
15+
description: Describe what is the motivation behind this feature.
16+
validations:
17+
required: true
18+
- type: textarea
19+
id: examples
20+
attributes:
21+
label: Examples
22+
description: If possible, provide examples of how this feature can be used.
23+
validations:
24+
required: false
25+
- type: textarea
26+
id: workarounds
27+
attributes:
28+
label: Possible workarounds
29+
description: If possible, describes possible workarounds to this feature.
30+
validations:
31+
required: false
32+
- type: textarea
33+
id: extra_information
34+
attributes:
35+
label: Additional information
36+
description: Is there anything else we should know about this feature request?
37+
validations:
38+
required: false

.github/ISSUE_TEMPLATE/other.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Other issue
2+
description: Use this for any other issues. Do NOT create blank issues
3+
title: "[OTHER]: "
4+
labels: ["awaiting triage"]
5+
body:
6+
- type: textarea
7+
id: description
8+
attributes:
9+
label: What would you like to share?
10+
description: Provide a clear and concise explanation of your issue.
11+
validations:
12+
required: true
13+
- type: textarea
14+
id: extrainfo
15+
attributes:
16+
label: Additional information
17+
description: Is there anything else we should know about this issue?
18+
validations:
19+
required: false

.github/pull_request_template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
- [ ] All new JavaScript files are placed inside an existing directory.
1616
- [ ] All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
1717
**Example:**`UserProfile.js` is allowed but `userprofile.js`,`Userprofile.js`,`user-Profile.js`,`userProfile.js` are not
18-
- [ ] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
18+
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
1919
- [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.

.github/workflows/Ci.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v3
15+
1516
- uses: actions/setup-node@v3
1617
with:
17-
node-version: 16
18+
node-version: 20
1819
cache: npm
1920

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

23-
- name: 🧪 Run tests
24-
run: npm test
24+
- name: 🧪 Run all tests
25+
run: npm run test
2526

2627
- name: 💄 Code style
2728
run: npm run style

.github/workflows/UpdateDirectory.mjs renamed to .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: |

.prettierignore

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

Backtracking/AllCombinationsOfSizeK.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order
33
44
What is combinations?
5-
- Combinations is selecting items from a collections without considering order of selection
5+
- Combinations is selecting items from a collections without considering the order of selection
66
77
Example:
88
- We have an apple, a banana, and a jackfruit
@@ -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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Problem Statement: Generate all distinct permutations of a an array (all permutations should be in sorted order);
2+
* Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order);
33
*
44
* What is permutations?
55
* - Permutation means possible arrangements in a set (here it is an array);
@@ -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

0 commit comments

Comments
 (0)