Skip to content

Commit 31329fc

Browse files
Merge branch 'master' into project-euler-5
2 parents 2863fa1 + 718d515 commit 31329fc

File tree

99 files changed

+2829
-2270
lines changed

Some content is hidden

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

99 files changed

+2829
-2270
lines changed

Diff for: .github/ISSUE_TEMPLATE/bug_report.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ body:
3333
label: Steps to reproduce (if applicable)
3434
description: List steps to reproduce the behavior.
3535
placeholder: |
36-
1.
37-
2.
38-
3.
36+
1.
37+
2.
38+
3.
3939
4.
4040
validations:
4141
required: false

Diff for: .github/dependabot.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Keep GitHub Actions up to date with Dependabot...
2+
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
3+
version: 2
4+
updates:
5+
- package-ecosystem: "github-actions"
6+
directory: "/"
7+
schedule:
8+
interval: "daily"

Diff for: .github/stale.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ markComment: >
1616
for your contributions.
1717
# Comment to post when closing a stale issue. Set to `false` to disable
1818
closeComment: >
19-
Please reopen this issue once you commit the changes requested or
19+
Please reopen this issue once you commit the changes requested or
2020
make improvements on the code. Thank you for your contributions.

Diff for: .github/workflows/Ci.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ jobs:
1111
name: Code style and tests
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1515

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

2121
- name: 📦 Install dependencies
@@ -25,13 +25,13 @@ jobs:
2525
run: npm run test
2626

2727
- name: 💄 Code style
28-
run: npm run style
28+
run: npm run check-style
2929

3030
codespell:
3131
name: Check for spelling errors
3232
runs-on: ubuntu-latest
3333
steps:
34-
- uses: actions/checkout@v3
34+
- uses: actions/checkout@v4
3535
- uses: codespell-project/actions-codespell@master
3636
with:
3737
# file types to ignore

Diff for: .github/workflows/UpdateDirectory.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ jobs:
1010
updateDirectory:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414

15-
- uses: actions/setup-node@v3
15+
- uses: actions/setup-node@v4
1616
with:
17-
node-version: 20
17+
node-version: 22
1818
cache: npm
1919

2020
- name: 📦 Install dependencies
@@ -25,8 +25,8 @@ jobs:
2525

2626
- name: Configure Github Action
2727
run: |
28-
git config --global user.name github-actions
29-
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
28+
git config --global user.name "$GITHUB_ACTOR"
29+
git config --global user.email "$[email protected]"
3030
3131
- name: 🤓 Commit & push new Directory (if needed)
3232
run: |

Diff for: .github/workflows/UploadCoverageReport.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
name: UploadCoverageReport
3+
4+
'on':
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- master
9+
pull_request:
10+
11+
env:
12+
REPORT_PATH: "coverage/coverage-final.json"
13+
14+
jobs:
15+
UploadCoverageReport:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- uses: actions/setup-node@v4
21+
with:
22+
node-version: 22
23+
cache: npm
24+
25+
- name: Install dependencies
26+
run: npm ci
27+
28+
- name: Generate coverage report
29+
run: npm test -- --coverage
30+
31+
- name: Upload coverage to codecov (tokenless)
32+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
33+
uses: codecov/codecov-action@v4
34+
with:
35+
files: "${{ env.REPORT_PATH }}"
36+
fail_ci_if_error: true
37+
38+
- name: Upload coverage to codecov (with token)
39+
if: "! github.event.pull_request.head.repo.fork "
40+
uses: codecov/codecov-action@v4
41+
with:
42+
token: ${{ secrets.CODECOV_TOKEN }}
43+
files: "${{ env.REPORT_PATH }}"
44+
fail_ci_if_error: true
45+
...

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ yarn-error.log*
1414

1515
# intelliJ workspace folder
1616
.idea
17+
18+
/coverage

Diff for: .husky/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npm run style
4+
npm run check-style
55
npm run test

Diff for: Backtracking/AllCombinationsOfSizeK.js

+23-41
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
1-
/*
2-
Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order
3-
4-
What is combinations?
5-
- Combinations is selecting items from a collections without considering the order of selection
6-
7-
Example:
8-
- We have an apple, a banana, and a jackfruit
9-
- We have three objects, and need to choose two items, then combinations will be
10-
11-
1. Apple & Banana
12-
2. Apple & Jackfruit
13-
3. Banana & Jackfruit
14-
15-
To read more about combinations, you can visit the following link:
16-
- https://betterexplained.com/articles/easy-permutations-and-combinations/
17-
18-
Solution:
19-
- We will be using backtracking to solve this questions
20-
- Take one element, and make all them combinations for k-1 elements
21-
- Once we get all combinations of that element, pop it and do same for next element
22-
*/
23-
24-
class Combinations {
25-
constructor(n, k) {
26-
this.n = n
27-
this.k = k
28-
this.current = [] // will be used for storing current combination
29-
this.combinations = []
30-
}
31-
32-
findCombinations(high = this.n, total = this.k, low = 1) {
33-
if (total === 0) {
34-
this.combinations.push([...this.current])
35-
return this.combinations
1+
function generateCombinations(n, k) {
2+
let currentCombination = []
3+
let allCombinations = [] // will be used for storing all combinations
4+
let currentValue = 1
5+
6+
function findCombinations() {
7+
if (currentCombination.length === k) {
8+
// Add the array of size k to the allCombinations array
9+
allCombinations.push([...currentCombination])
10+
return
3611
}
37-
for (let i = low; i <= high; i++) {
38-
this.current.push(i)
39-
this.findCombinations(high, total - 1, i + 1)
40-
this.current.pop()
12+
if (currentValue > n) {
13+
// Check for exceeding the range
14+
return
4115
}
42-
return this.combinations
16+
currentCombination.push(currentValue++)
17+
findCombinations()
18+
currentCombination.pop()
19+
findCombinations()
20+
currentValue--
4321
}
22+
23+
findCombinations()
24+
25+
return allCombinations
4426
}
4527

46-
export { Combinations }
28+
export { generateCombinations }

Diff for: Backtracking/KnightTour.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
class OpenKnightTour {
44
constructor(size) {
5+
// Constructor to initialize the chessboard and size
56
this.board = new Array(size).fill(0).map(() => new Array(size).fill(0))
67
this.size = size
78
}
89

910
getMoves([i, j]) {
10-
// helper function to get the valid moves of the knight from the current position
11+
// Helper function to get the valid moves of the knight from the current position
1112
const moves = [
1213
[i + 2, j - 1],
1314
[i + 2, j + 1],
@@ -19,18 +20,19 @@ class OpenKnightTour {
1920
[i - 1, j + 2]
2021
]
2122

23+
// Filter out moves that are within the board boundaries
2224
return moves.filter(
2325
([y, x]) => y >= 0 && y < this.size && x >= 0 && x < this.size
2426
)
2527
}
2628

2729
isComplete() {
28-
// helper function to check if the board is complete
30+
// Helper function to check if the board is complete
2931
return !this.board.map((row) => row.includes(0)).includes(true)
3032
}
3133

3234
solve() {
33-
// function to find the solution for the given board
35+
// Function to find the solution for the given board
3436
for (let i = 0; i < this.size; i++) {
3537
for (let j = 0; j < this.size; j++) {
3638
if (this.solveHelper([i, j], 0)) return true
@@ -40,22 +42,23 @@ class OpenKnightTour {
4042
}
4143

4244
solveHelper([i, j], curr) {
43-
// helper function for the main computation
45+
// Helper function for the main computation
4446
if (this.isComplete()) return true
4547

48+
// Iterate through possible moves and attempt to fill the board
4649
for (const [y, x] of this.getMoves([i, j])) {
4750
if (this.board[y][x] === 0) {
4851
this.board[y][x] = curr + 1
4952
if (this.solveHelper([y, x], curr + 1)) return true
50-
// backtracking
53+
// Backtracking: If the solution is not found, reset the cell to 0
5154
this.board[y][x] = 0
5255
}
5356
}
5457
return false
5558
}
5659

5760
printBoard(output = (value) => console.log(value)) {
58-
// utility function to display the board
61+
// Utility function to display the board
5962
for (const row of this.board) {
6063
let string = ''
6164
for (const elem of row) {

Diff for: Backtracking/MColoringProblem.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Colors a graph using up to m colors such that no two adjacent vertices share the same color.
3+
* @param {number[][]} graph - Adjacency matrix of the graph, using 0 for no edge.
4+
* @param {number} m - The number of colors to use.
5+
* @returns {?Array.<number>} A valid M-coloring of the graph using colors 1 to m, or null if none exists.
6+
* @see https://en.wikipedia.org/wiki/Graph_coloring
7+
*/
8+
function mColoring(graph, m) {
9+
const colors = new Array(graph.length).fill(0)
10+
11+
// Check if it's safe to color a vertex with a given color.
12+
function isSafe(vertex, color) {
13+
for (let i = 0; i < graph.length; i++) {
14+
if (graph[vertex][i] && colors[i] === color) {
15+
return false
16+
}
17+
}
18+
return true
19+
}
20+
21+
// Use backtracking to try and color the graph.
22+
function solveColoring(vertex = 0) {
23+
if (vertex === graph.length) {
24+
return true
25+
}
26+
27+
for (let color = 1; color <= m; color++) {
28+
if (isSafe(vertex, color)) {
29+
colors[vertex] = color
30+
31+
if (solveColoring(vertex + 1)) {
32+
return true
33+
}
34+
35+
// If no solution, backtrack.
36+
colors[vertex] = 0
37+
}
38+
}
39+
return false
40+
}
41+
42+
// If coloring is possible, return the colors.
43+
if (solveColoring()) {
44+
return colors
45+
}
46+
return null
47+
}
48+
49+
export { mColoring }

Diff for: Backtracking/tests/AllCombinationsOfSizeK.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { Combinations } from '../AllCombinationsOfSizeK'
1+
import { generateCombinations } from '../AllCombinationsOfSizeK'
22

33
describe('AllCombinationsOfSizeK', () => {
44
it('should return 3x2 matrix solution for n = 3 and k = 2', () => {
5-
const test1 = new Combinations(3, 2)
6-
expect(test1.findCombinations()).toEqual([
5+
const res = generateCombinations(3, 2)
6+
expect(res).toEqual([
77
[1, 2],
88
[1, 3],
99
[2, 3]
1010
])
1111
})
1212

1313
it('should return 6x2 matrix solution for n = 4 and k = 2', () => {
14-
const test2 = new Combinations(4, 2)
15-
expect(test2.findCombinations()).toEqual([
14+
const res = generateCombinations(4, 2)
15+
expect(res).toEqual([
1616
[1, 2],
1717
[1, 3],
1818
[1, 4],

Diff for: Backtracking/tests/KnightTour.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('OpenKnightTour', () => {
1111
[0, 0, 0, 0, 0]
1212
])
1313

14-
KT.solve()
14+
expect(KT.solve()).toBe(true)
1515
expect(KT.board).toEqual([
1616
[19, 4, 15, 10, 25],
1717
[14, 9, 18, 5, 16],
@@ -20,4 +20,18 @@ describe('OpenKnightTour', () => {
2020
[21, 2, 7, 12, 23]
2121
])
2222
})
23+
24+
it('OpenKnightTour(2)', () => {
25+
const KT = new OpenKnightTour(2)
26+
expect(KT.board).toEqual([
27+
[0, 0],
28+
[0, 0]
29+
])
30+
31+
expect(KT.solve()).toBe(false)
32+
expect(KT.board).toEqual([
33+
[0, 0],
34+
[0, 0]
35+
])
36+
})
2337
})

0 commit comments

Comments
 (0)