Skip to content

Commit 89daef9

Browse files
mailysantositsvinayakgithub-actionssuhailmalik07pomkarnath98
authored
Merge Changes (#1)
* Fixes: TheAlgorithms#155 - Check if a string rearranged can be a palindrome (TheAlgorithms#407) * Check if a string rearranged can be a palindrome * Fixes: TheAlgorithms#155 - palindromeRearranging * Update CheckRearrangePalindrome.js Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * Added Edit Distance Algorithm in Dynamic-Programming (TheAlgorithms#402) * DP Edit Distance Algorithm * Update EditDistance.js Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * 0 1 knapsack (TheAlgorithms#408) * Added Longest Common Subsequence * Renamed the File * Optimized the code * Optimized the code * Changed some styles as per the rule * Again some style fixed * Added Longest Increasing Subsequence program to the list * Style changed * Added 0-1-Knapsack Problem * Style Changed as per guidelines * Update ZeroOneKnapsack.js * Delete LongestCommonSubsequence.js * Delete LongestIncreasingSubsequence.js Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * feat: added Polynomial in Maths (TheAlgorithms#397) * updating DIRECTORY.md * tweak: must be same output string (TheAlgorithms#395) * Added tests for Strings algorithms (TheAlgorithms#390) * test: added tests for check anagram function * updating DIRECTORY.md * Added JSDoc documentation to some sorting functions and added TimoSort (TheAlgorithms#406) * Added TimSort Co-authored-by: Solot Paul <[email protected]> Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * Fix selection sort and add tests; fixes TheAlgorithms#414 (TheAlgorithms#418) * Fix selection sort and add tests; fixes TheAlgorithms#414 Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * Create FibonacciNumber.js (TheAlgorithms#378) * Create FibonacciNumber.js * Update FibonacciNumber.js Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * Added Mean Square Error (TheAlgorithms#417) * Added Mean Square Error * Update MeanSquareError.js Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * Create FibonacciNumberRecursive.js (TheAlgorithms#380) * Create FibonacciNumberRecursive.js * Update FibonacciNumberRecursive.js Co-authored-by: vinayak <[email protected]> * updating DIRECTORY.md * Fix methodname and parameter type in BinaryToDecimal.js (TheAlgorithms#392) * Update BinaryToDecimal.js Fix typo in name * Update BinaryToDecimal.js Co-authored-by: vinayak <[email protected]> * npx fixx (TheAlgorithms#421) Co-authored-by: vinayak <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Suhail Malik <[email protected]> Co-authored-by: Omkarnath Parida <[email protected]> Co-authored-by: Divyajyoti Ukirde <[email protected]> Co-authored-by: balbaal <[email protected]> Co-authored-by: Alexandre Xavier <[email protected]> Co-authored-by: DarkWarrior703 <[email protected]> Co-authored-by: Bogdan Lazar <[email protected]> Co-authored-by: illegalcall <[email protected]> Co-authored-by: Stas <[email protected]> Co-authored-by: Kaustubh Badrike <[email protected]>
1 parent 1671ea6 commit 89daef9

25 files changed

+8736
-41
lines changed

Conversions/BinaryToDecimal.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
function binaryToDeicmal (binaryNumber) {
1+
const binaryToDecimal = (binaryString) => {
22
let decimalNumber = 0
3-
const binaryDigits = binaryNumber.split('').reverse() // Splits the binary number into reversed single digits
3+
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
44
binaryDigits.forEach((binaryDigit, index) => {
55
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
66
})
7-
console.log(`Decimal of ${binaryNumber} is ${decimalNumber}`)
7+
console.log(`Decimal of ${binaryString} is ${decimalNumber}`)
8+
return decimalNumber
89
}
910

10-
binaryToDeicmal('111001')
11-
binaryToDeicmal('101')
11+
(() => {
12+
binaryToDecimal('111001')
13+
binaryToDecimal('101')
14+
})()

DIRECTORY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
## [babel](https://github.com/TheAlgorithms/Javascript/blob/master//babel.config.js)
3+
24
## Backtracking
35
* [KnightTour](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/KnightTour.js)
46
* [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/NQueen.js)
@@ -47,13 +49,16 @@
4749
## Dynamic-Programming
4850
* [ClimbingStairs](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ClimbingStairs.js)
4951
* [CoinChange](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/CoinChange.js)
52+
* [EditDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/EditDistance.js)
53+
* [FibonacciNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/FibonacciNumber.js)
5054
* [KadaneAlgo](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/KadaneAlgo.js)
5155
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LevenshteinDistance.js)
5256
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestCommonSubsequence.js)
5357
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestIncreasingSubsequence.js)
5458
* [MaxNonAdjacentSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/MaxNonAdjacentSum.js)
5559
* [NumberOfSubsetEqualToGivenSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js)
5660
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/SieveOfEratosthenes.js)
61+
* [ZeroOneKnapsack](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ZeroOneKnapsack.js)
5762

5863
## Graphs
5964
* [ConnectedComponents](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/ConnectedComponents.js)
@@ -83,10 +88,12 @@
8388
* [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js)
8489
* [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js)
8590
* [GridGet](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGet.js)
91+
* [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js)
8692
* [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js)
8793
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js)
8894
* [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js)
8995
* [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PiApproximationMonteCarlo.js)
96+
* [Polynomial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Polynomial.js)
9097
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js)
9198
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js)
9299
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js)
@@ -96,6 +103,7 @@
96103

97104
## Recursive
98105
* [EucledianGCD](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/EucledianGCD.js)
106+
* [FibonacciNumberRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/FibonacciNumberRecursive.js)
99107
* [TowerOfHanoi](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/TowerOfHanoi.js)
100108

101109
## Search
@@ -125,16 +133,24 @@
125133
* [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js)
126134
* [RadixSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/RadixSort.js)
127135
* [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.js)
136+
* [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.test.js)
128137
* [ShellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/ShellSort.js)
138+
* [TimSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TimSort.js)
129139
* [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js)
130140
* [WiggleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/WiggleSort.js)
131141

132142
## String
133143
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js)
144+
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.test.js)
134145
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.js)
146+
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.test.js)
147+
* [CheckRearrangePalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckRearrangePalindrome.js)
135148
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.js)
149+
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.test.js)
136150
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
151+
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.test.js)
137152
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js)
153+
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.test.js)
138154

139155
## Timing-Functions
140156
* [IntervalTimer](https://github.com/TheAlgorithms/Javascript/blob/master/Timing-Functions/IntervalTimer.js)

Dynamic-Programming/EditDistance.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Wikipedia -> https://en.wikipedia.org/wiki/Edit_distance
3+
4+
Q. -> Given two strings `word1` and `word2`. You can perform these operations on any of the string to make both strings similar.
5+
- Insert
6+
- Remove
7+
- Replace
8+
Find the minimum operation cost required to make both same. Each operation cost is 1.
9+
10+
Algorithm details ->
11+
time complexity - O(n*m)
12+
space complexity - O(n*m)
13+
*/
14+
15+
const minimumEditDistance = (word1, word2) => {
16+
const n = word1.length
17+
const m = word2.length
18+
const dp = new Array(m + 1).fill(0).map(item => [])
19+
20+
/*
21+
fill dp matrix with default values -
22+
- first row is filled considering no elements in word2.
23+
- first column filled considering no elements in word1.
24+
*/
25+
26+
for (let i = 0; i < n + 1; i++) {
27+
dp[0][i] = i
28+
}
29+
30+
for (let i = 0; i < m + 1; i++) {
31+
dp[i][0] = i
32+
}
33+
34+
/*
35+
indexing is 1 based for dp matrix as we defined some known values at first row and first column/
36+
*/
37+
38+
for (let i = 1; i < m + 1; i++) {
39+
for (let j = 1; j < n + 1; j++) {
40+
const letter1 = word1[j - 1]
41+
const letter2 = word2[i - 1]
42+
43+
if (letter1 === letter2) {
44+
dp[i][j] = dp[i - 1][j - 1]
45+
} else {
46+
dp[i][j] = Math.min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) + 1
47+
}
48+
}
49+
}
50+
51+
return dp[m][n]
52+
}
53+
54+
const main = () => {
55+
console.log(minimumEditDistance('horse', 'ros'))
56+
console.log(minimumEditDistance('cat', 'cut'))
57+
console.log(minimumEditDistance('', 'abc'))
58+
console.log(minimumEditDistance('google', 'glgool'))
59+
}
60+
61+
main()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://en.wikipedia.org/wiki/Fibonacci_number
2+
3+
const fibonacci = (N) => {
4+
// creating array to store values
5+
const memo = new Array(N + 1)
6+
memo[0] = 0
7+
memo[1] = 1
8+
for (let i = 2; i <= N; i++) {
9+
memo[i] = memo[i - 1] + memo[i - 2]
10+
}
11+
return memo[N]
12+
}
13+
14+
// testing
15+
(() => {
16+
const number = 5
17+
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
18+
})()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* A Dynamic Programming based solution for calculating Zero One Knapsack
3+
* https://en.wikipedia.org/wiki/Knapsack_problem
4+
*/
5+
6+
const zeroOneKnapsack = (arr, n, cap, cache) => {
7+
if (cap === 0 || n === 0) {
8+
cache[n][cap] = 0
9+
return cache[n][cap]
10+
}
11+
if (cache[n][cap] !== -1) {
12+
return cache[n][cap]
13+
}
14+
if (arr[n - 1][0] <= cap) {
15+
cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack(arr, n - 1, cap, cache))
16+
return cache[n][cap]
17+
} else {
18+
cache[n][cap] = zeroOneKnapsack(arr, n - 1, cap, cache)
19+
return cache[n][cap]
20+
}
21+
}
22+
23+
const main = () => {
24+
/*
25+
Problem Statement:
26+
You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity.
27+
You have to cherry pick the artifact in order to maximize the total value of the artifacts you stole.
28+
29+
Link for the Problem: https://www.hackerrank.com/contests/srin-aadc03/challenges/classic-01-knapsack
30+
*/
31+
let input = `1
32+
4 5
33+
1 8
34+
2 4
35+
3 0
36+
2 5
37+
2 3`
38+
39+
input = input.trim().split('\n')
40+
input.shift()
41+
const length = input.length
42+
43+
let i = 0
44+
while (i < length) {
45+
const cap = Number(input[i].trim().split(' ')[0])
46+
const currlen = Number(input[i].trim().split(' ')[1])
47+
let j = i + 1
48+
const arr = []
49+
while (j <= i + currlen) {
50+
arr.push(input[j])
51+
j++
52+
}
53+
const newArr = []
54+
arr.map(e => {
55+
newArr.push(e.trim().split(' ').map(Number))
56+
})
57+
const cache = []
58+
for (let i = 0; i <= currlen; i++) {
59+
const temp = []
60+
for (let j = 0; j <= cap; j++) {
61+
temp.push(-1)
62+
}
63+
cache.push(temp)
64+
}
65+
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
66+
console.log(result)
67+
i += currlen + 1
68+
}
69+
}
70+
71+
main()

Maths/MeanSquareError.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Wikipedia: https://en.wikipedia.org/wiki/Mean_squared_error
2+
3+
const meanSquaredError = (predicted, expected) => {
4+
if (!Array.isArray(predicted) || !Array.isArray(expected)) {
5+
throw new TypeError('Argument must be an Array')
6+
}
7+
8+
if (predicted.length !== expected.length) {
9+
throw new TypeError('The two lists must be of equal length')
10+
}
11+
12+
let err = 0
13+
14+
for (let i = 0; i < expected.length; i++) {
15+
err += (expected[i] - predicted[i]) ** 2
16+
}
17+
18+
return err / expected.length
19+
}
20+
21+
// testing
22+
(() => {
23+
console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0)
24+
console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5)
25+
console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3)
26+
})()

Maths/Polynomial.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
/**
3+
* Polynomials are algebraic expressions consisting of two or more algebraic terms.
4+
* Terms of a polynomial are:
5+
* 1. Coefficients e.g. 5, 4 in 5x^0, 4x^3 respectively
6+
* 2. Variables e.g. y in 3y^2
7+
* 3. Exponents e.g. 5 in y^5
8+
*
9+
* Class Polynomial constructs the polynomial using Array as an argument.
10+
* The members of array are coefficients and their indexes as exponents.
11+
*/
12+
class Polynomial {
13+
constructor (array) {
14+
this.coefficientArray = array // array of coefficients
15+
this.polynomial = '' // in terms of x e.g. (2x) + (1)
16+
this.construct()
17+
}
18+
19+
/**
20+
* Function to construct the polynomial in terms of x using the coefficientArray
21+
*/
22+
construct () {
23+
this.polynomial = this.coefficientArray.map((coefficient, exponent) => {
24+
if (coefficient === 0) {
25+
return '0'
26+
}
27+
if (exponent === 0) {
28+
return `(${coefficient})`
29+
} else if (exponent === 1) {
30+
return `(${coefficient}x)`
31+
} else {
32+
return `(${coefficient}x^${exponent})`
33+
}
34+
})
35+
.filter((x) => {
36+
if (x !== '0') {
37+
return x
38+
}
39+
})
40+
.reverse()
41+
.join(' + ')
42+
}
43+
44+
/**
45+
* Function to display polynomial in terms of x
46+
* @returns {String} of polynomial representation in terms of x
47+
*/
48+
display () {
49+
return this.polynomial
50+
}
51+
52+
/**
53+
* Function to calculate the value of the polynomial by substituting variable x
54+
* @param {Number} value
55+
*/
56+
evaluate (value) {
57+
return this.coefficientArray.reduce((result, coefficient, exponent) => {
58+
return result + coefficient * (Math.pow(value, exponent))
59+
}, 0)
60+
}
61+
}
62+
63+
/**
64+
* Function to perform tests
65+
*/
66+
const tests = () => {
67+
const polynomialOne = new Polynomial([1, 2, 3, 4])
68+
console.log('Test 1: [1,2,3,4]')
69+
console.log('Display Polynomial ', polynomialOne.display())
70+
// (4x^3) + (3x^2) + (2x) + (1)
71+
console.log('Evaluate Polynomial value=2 ', polynomialOne.evaluate(2))
72+
// 49
73+
74+
const polynomialTwo = new Polynomial([5, 0, 0, -4, 3])
75+
console.log('Test 2: [5,0,0,-4,3]')
76+
console.log('Display Polynomial ', polynomialTwo.display())
77+
// (3x^4) + (-4x^3) + (5)
78+
console.log('Evaluate Polynomial value=1 ', polynomialTwo.evaluate(1))
79+
// 4
80+
}
81+
82+
tests()

Recursive/FibonacciNumberRecursive.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://en.wikipedia.org/wiki/Fibonacci_number
2+
3+
const fibonacci = (N) => {
4+
if (N === 0 || N === 1) return N
5+
6+
return fibonacci(N - 2) + fibonacci(N - 1)
7+
}
8+
9+
// testing
10+
(() => {
11+
const number = 5
12+
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
13+
})()

0 commit comments

Comments
 (0)