Skip to content

Commit 96d1d68

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 4010fb3 + 1554ba5 commit 96d1d68

6 files changed

+88
-8
lines changed

Conversions/HexToDecimal.js

+4-1
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

+24
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+
})

Dynamic-Programming/LongestIncreasingSubsequence.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Return the length of the Longest Increasing Subsequence, given array x
77
function longestIncreasingSubsequence(x) {
88
const length = x.length
9+
if (length == 0) {
10+
return 0
11+
}
912
const dp = Array(length).fill(1)
1013

1114
let res = 1

Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
/*
2-
Given an array of non-negative integers and a value sum,
2+
Given an array of positive integers and a value sum,
33
determine the total number of the subset with sum
44
equal to the given sum.
55
*/
66
/*
77
Given solution is O(n*sum) Time complexity and O(sum) Space complexity
88
*/
99
function NumberOfSubsetSum(array, sum) {
10+
if (sum < 0) {
11+
throw new Error('The sum must be non-negative.')
12+
}
13+
14+
if (!array.every((num) => num > 0)) {
15+
throw new Error('All of the inputs of the array must be positive.')
16+
}
1017
const dp = [] // create an dp array where dp[i] denote number of subset with sum equal to i
1118
for (let i = 1; i <= sum; i++) {
1219
dp[i] = 0
@@ -23,10 +30,4 @@ function NumberOfSubsetSum(array, sum) {
2330
return dp[sum]
2431
}
2532

26-
// example
27-
28-
// const array = [1, 1, 2, 2, 3, 1, 1]
29-
// const sum = 4
30-
// const result = NumberOfSubsetSum(array, sum)
31-
3233
export { NumberOfSubsetSum }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { longestIncreasingSubsequence } from '../LongestIncreasingSubsequence'
2+
3+
describe('Testing longestIncreasingSubsequence', () => {
4+
it.each([
5+
[[], 0],
6+
[[1], 1],
7+
[[2, 2], 1],
8+
[[3, 3, 3], 1],
9+
[[4, 4, 4, 4], 1],
10+
[[1, 2], 2],
11+
[[1, 2, 2, 2, 2], 2],
12+
[[1, 0, 2], 2],
13+
[[1, 10, 2, 30], 3],
14+
[[5, 8, 3, 7, 9, 1], 3],
15+
[[10, 9, 2, 5, 3, 7, 101, 18], 4],
16+
[[10, 10, 9, 9, 2, 2, 5, 5, 3, 3, 7, 7, 101, 101, 18, 18], 4],
17+
[[0, 1, 0, 3, 2, 3], 4],
18+
[[1, 1, 2, 2, 2], 2],
19+
[[1, 1, 2, 2, 2, 3, 3, 3, 3], 3],
20+
[[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], 6]
21+
])('check with %j', (input, expected) => {
22+
expect(longestIncreasingSubsequence(input)).toBe(expected)
23+
})
24+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NumberOfSubsetSum } from '../NumberOfSubsetEqualToGivenSum'
2+
3+
describe('Testing NumberOfSubsetSum', () => {
4+
it.each([
5+
[[], 0, 1],
6+
[[], 1, 0],
7+
[[1], 2, 0],
8+
[[1, 2, 3, 4, 5], 0, 1],
9+
[[1, 1, 1, 1, 1], 5, 1],
10+
[[1, 1, 1, 1, 1], 4, 5],
11+
[[1, 2, 3, 3], 6, 3],
12+
[[10, 20, 30, 1], 31, 2],
13+
[[1, 1, 2, 2, 3, 1, 1], 4, 18]
14+
])('check with %j and %i', (arr, sum, expected) => {
15+
expect(NumberOfSubsetSum(arr, sum)).toBe(expected)
16+
})
17+
18+
it.each([
19+
[[1, 2], -1],
20+
[[0, 2], 2],
21+
[[1, -1], 0]
22+
])('throws for %j and %i', (arr, sum) => {
23+
expect(() => NumberOfSubsetSum(arr, sum)).toThrowError()
24+
})
25+
})

0 commit comments

Comments
 (0)