Skip to content

Commit 3e4176e

Browse files
authored
Merge branch 'TheAlgorithms:master' into remove-Eratosthenes-intarray
2 parents e3ef5c3 + 85a55da commit 3e4176e

24 files changed

+892
-118
lines changed

.github/workflows/UploadCoverageReport.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ name: UploadCoverageReport
88
- master
99
pull_request:
1010

11+
env:
12+
REPORT_PATH: "coverage/coverage-final.json"
13+
1114
jobs:
1215
UploadCoverageReport:
1316
runs-on: ubuntu-latest
@@ -25,9 +28,18 @@ jobs:
2528
- name: Generate coverage report
2629
run: npm test -- --coverage
2730

28-
- name: Upload coverage to codecov
29-
uses: codecov/codecov-action@v3
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
3041
with:
31-
files: "coverage/coverage-final.json"
42+
token: ${{ secrets.CODECOV_TOKEN }}
43+
files: "${{ env.REPORT_PATH }}"
3244
fail_ci_if_error: true
3345
...

Backtracking/tests/RatInAMaze.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe('RatInAMaze', () => {
66

77
for (const value of values) {
88
// we deliberately want to check whether this constructor call fails or not
9-
// eslint-disable-next-line no-new
109
expect(() => {
1110
new RatInAMaze(value)
1211
}).toThrow()
@@ -15,7 +14,6 @@ describe('RatInAMaze', () => {
1514

1615
it('should fail for an empty array', () => {
1716
// we deliberately want to check whether this constructor call fails or not
18-
// eslint-disable-next-line no-new
1917
expect(() => {
2018
new RatInAMaze([])
2119
}).toThrow()
@@ -28,7 +26,6 @@ describe('RatInAMaze', () => {
2826
]
2927

3028
// we deliberately want to check whether this constructor call fails or not
31-
// eslint-disable-next-line no-new
3229
expect(() => {
3330
new RatInAMaze(array)
3431
}).toThrow()
@@ -39,7 +36,6 @@ describe('RatInAMaze', () => {
3936

4037
for (const value of values) {
4138
// we deliberately want to check whether this constructor call fails or not
42-
// eslint-disable-next-line no-new
4339
expect(() => {
4440
new RatInAMaze(value)
4541
}).toThrow()

Backtracking/tests/Sudoku.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const solved = [
2727
describe('Sudoku', () => {
2828
it('should create a valid board successfully', () => {
2929
// we deliberately want to check whether this constructor call fails or not
30-
// eslint-disable-next-line no-new
3130
expect(() => {
3231
new Sudoku(data)
3332
}).not.toThrow()

Compression/RLE.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* RLE (Run Length Encoding) is a simple form of data compression.
3+
* The basic idea is to represent repeated successive characters as a single count and character.
4+
* For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
5+
*
6+
* @author - [ddaniel27](https://github.com/ddaniel27)
7+
*/
8+
9+
function Compress(str) {
10+
let compressed = ''
11+
let count = 1
12+
13+
for (let i = 0; i < str.length; i++) {
14+
if (str[i] !== str[i + 1]) {
15+
compressed += count + str[i]
16+
count = 1
17+
continue
18+
}
19+
20+
count++
21+
}
22+
23+
return compressed
24+
}
25+
26+
function Decompress(str) {
27+
let decompressed = ''
28+
let match = [...str.matchAll(/(\d+)(\D)/g)] // match all groups of digits followed by a non-digit character
29+
30+
match.forEach((item) => {
31+
let [count, char] = [item[1], item[2]]
32+
decompressed += char.repeat(count)
33+
})
34+
35+
return decompressed
36+
}
37+
38+
export { Compress, Decompress }

Compression/test/RLE.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Compress, Decompress } from '../RLE'
2+
3+
describe('Test RLE Compressor/Decompressor', () => {
4+
it('Test - 1, Pass long repetitive strings', () => {
5+
expect(Compress('AAAAAAAAAAAAAA')).toBe('14A')
6+
expect(Compress('AAABBQQQQQFG')).toBe('3A2B5Q1F1G')
7+
})
8+
9+
it('Test - 2, Pass compressed strings', () => {
10+
expect(Decompress('14A')).toBe('AAAAAAAAAAAAAA')
11+
expect(Decompress('3A2B5Q1F1G')).toBe('AAABBQQQQQFG')
12+
})
13+
})

Conversions/HexToDecimal.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function hexToInt(hexNum) {
2+
if (!/^[0-9A-F]+$/.test(hexNum)) {
3+
throw new Error('Invalid hex string.')
4+
}
25
const numArr = hexNum.split('') // converts number to array
36
return numArr.map((item, index) => {
47
switch (item) {
@@ -29,4 +32,4 @@ function hexToDecimal(hexNum) {
2932
}, 0)
3033
}
3134

32-
export { hexToInt, hexToDecimal }
35+
export { hexToDecimal }

Conversions/test/HexToDecimal.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { hexToDecimal } from '../HexToDecimal'
2+
3+
describe('Testing HexToDecimal', () => {
4+
it.each([
5+
['0', 0],
6+
['1', 1],
7+
['A', 10],
8+
['B', 11],
9+
['C', 12],
10+
['D', 13],
11+
['E', 14],
12+
['F', 15],
13+
['10', 16],
14+
['859', 2137],
15+
['4D2', 1234],
16+
['81323ABD92', 554893491602]
17+
])('check with %s', (hexStr, expected) => {
18+
expect(hexToDecimal(hexStr)).toBe(expected)
19+
})
20+
21+
it.each(['a', '-1', 'G', ''])('throws for %s', (hexStr) => {
22+
expect(() => hexToDecimal(hexStr)).toThrowError()
23+
})
24+
})

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* [ROT13](Ciphers/ROT13.js)
3636
* [VigenereCipher](Ciphers/VigenereCipher.js)
3737
* [XORCipher](Ciphers/XORCipher.js)
38+
* **Compression**
39+
* [RLE](Compression/RLE.js)
3840
* **Conversions**
3941
* [ArbitraryBase](Conversions/ArbitraryBase.js)
4042
* [ArrayBufferToBase64](Conversions/ArrayBufferToBase64.js)
@@ -285,6 +287,7 @@
285287
* [Problem016](Project-Euler/Problem016.js)
286288
* [Problem017](Project-Euler/Problem017.js)
287289
* [Problem018](Project-Euler/Problem018.js)
290+
* [Problem019](Project-Euler/Problem019.js)
288291
* [Problem020](Project-Euler/Problem020.js)
289292
* [Problem021](Project-Euler/Problem021.js)
290293
* [Problem023](Project-Euler/Problem023.js)

Data-Structures/Array/QuickSelect.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313

1414
function QuickSelect(items, kth) {
15-
// eslint-disable-line no-unused-vars
1615
if (kth < 1 || kth > items.length) {
1716
throw new RangeError('Index Out of Bound')
1817
}

Data-Structures/Queue/QueueUsing2Stacks.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,6 @@ class Queue {
2929
return top
3030
}
3131
}
32-
33-
// display elements of the inputstack
34-
listIn(output = (value) => console.log(value)) {
35-
let i = 0
36-
while (i < this.inputStack.length) {
37-
output(this.inputStack[i])
38-
i++
39-
}
40-
}
41-
42-
// display element of the outputstack
43-
listOut(output = (value) => console.log(value)) {
44-
let i = 0
45-
while (i < this.outputStack.length) {
46-
output(this.outputStack[i])
47-
i++
48-
}
49-
}
5032
}
5133

5234
export { Queue }

0 commit comments

Comments
 (0)