Skip to content

Commit b1a3e19

Browse files
committed
^X
Merge branch 'cycle-detection-in-linkedlist' of https://github.com/pomkarnath98/Javascript into cycle-detection-in-linkedlist Will add modified Cycle Detection problem
2 parents e31390f + 045f68f commit b1a3e19

25 files changed

+8555
-34
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

DIRECTORY.md

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

2-
## back-tracking
3-
* [KnightTour](https://github.com/TheAlgorithms/Javascript/blob/master/back-tracking/KnightTour.js)
4-
* [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/back-tracking/NQueen.js)
5-
* [Sudoku](https://github.com/TheAlgorithms/Javascript/blob/master/back-tracking/Sudoku.js)
2+
## [babel](https://github.com/TheAlgorithms/Javascript/blob/master//babel.config.js)
3+
4+
## Backtracking
5+
* [KnightTour](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/KnightTour.js)
6+
* [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/NQueen.js)
7+
* [Sudoku](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/Sudoku.js)
68

79
## Cache
810
* [LFUCache](https://github.com/TheAlgorithms/Javascript/blob/master/Cache/LFUCache.js)
@@ -45,12 +47,17 @@
4547
* [Trie](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Tree/Trie.js)
4648

4749
## Dynamic-Programming
50+
* [ClimbingStairs](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ClimbingStairs.js)
4851
* [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)
4953
* [KadaneAlgo](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/KadaneAlgo.js)
5054
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LevenshteinDistance.js)
55+
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestCommonSubsequence.js)
56+
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestIncreasingSubsequence.js)
5157
* [MaxNonAdjacentSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/MaxNonAdjacentSum.js)
5258
* [NumberOfSubsetEqualToGivenSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js)
5359
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/SieveOfEratosthenes.js)
60+
* [ZeroOneKnapsack](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ZeroOneKnapsack.js)
5461

5562
## Graphs
5663
* [ConnectedComponents](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/ConnectedComponents.js)
@@ -84,12 +91,13 @@
8491
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js)
8592
* [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js)
8693
* [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PiApproximationMonteCarlo.js)
94+
* [Polynomial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Polynomial.js)
8795
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js)
8896
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js)
8997
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js)
9098

91-
## Project Euler
92-
* [Problem1](https://github.com/TheAlgorithms/Javascript/blob/master/Project%20Euler/Problem1.js)
99+
## Project-Euler
100+
* [Problem1](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem1.js)
93101

94102
## Recursive
95103
* [EucledianGCD](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/EucledianGCD.js)
@@ -98,6 +106,7 @@
98106
## Search
99107
* [BinarySearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/BinarySearch.js)
100108
* [ExponentialSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/ExponentialSearch.js)
109+
* [FibonacciSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/FibonacciSearch.js)
101110
* [InterpolationSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/InterpolationSearch.js)
102111
* [JumpSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/JumpSearch.js)
103112
* [LinearSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/LinearSearch.js)
@@ -127,13 +136,19 @@
127136

128137
## String
129138
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js)
139+
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.test.js)
130140
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.js)
141+
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.test.js)
142+
* [CheckRearrangePalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckRearrangePalindrome.js)
131143
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.js)
144+
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.test.js)
132145
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
146+
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.test.js)
133147
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js)
148+
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.test.js)
134149

135-
## TimingFunctions
136-
* [IntervalTimer](https://github.com/TheAlgorithms/Javascript/blob/master/TimingFunctions/IntervalTimer.js)
150+
## Timing-Functions
151+
* [IntervalTimer](https://github.com/TheAlgorithms/Javascript/blob/master/Timing-Functions/IntervalTimer.js)
137152

138153
## Trees
139154
* [DepthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Trees/DepthFirstSearch.js)

Dynamic-Programming/ClimbingStairs.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* You are climbing a stair case. It takes n steps to reach to the top.
3+
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
4+
*/
5+
6+
const climbStairs = (n) => {
7+
let prev = 0
8+
let cur = 1
9+
let temp
10+
11+
for (let i = 0; i < n; i++) {
12+
temp = prev
13+
prev = cur
14+
cur += temp
15+
}
16+
return cur
17+
}
18+
19+
const main = () => {
20+
const number = 5
21+
22+
console.log('Number of ways to climb ' + number + ' stairs in ' + climbStairs(5))
23+
}
24+
25+
// testing
26+
main()

Dynamic-Programming/EditDistance.js

+61
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()

Maths/AverageMean.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict'
12
/*
23
author: PatOnTheBack
34
license: GPL-3.0 or later
@@ -11,12 +12,12 @@
1112
https://en.wikipedia.org/wiki/Mean
1213
*/
1314

14-
function mean (nums) {
15-
'use strict'
15+
const mean = (nums) => {
16+
// This is a function returns average/mean of array
1617
var sum = 0
1718
var avg
1819

19-
// This loop sums all values in the 'nums' array.
20+
// This loop sums all values in the 'nums' array using forEach loop
2021
nums.forEach(function (current) {
2122
sum += current
2223
})

Maths/Polynomial.js

+82
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()
File renamed without changes.

Search/FibonacciSearch.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/****************************************************************************
2+
* Fibonacci Search JavaScript Implementation
3+
* Author Alhassan Atama Isiaka
4+
* Version v1.0.0
5+
* Copyright 2020
6+
* https://github.com/komputarist
7+
*
8+
* This implementation is based on Generalizing the Fibonacci search we
9+
* define the Fibonacci search of degree K. Like the Fibonacci search,
10+
* which it reduces to for K = 2, the Fibonacci search of degree K
11+
* involves only addition and subtraction.
12+
* Capocelli R.M. (1991) A Generalization of the Fibonacci Search. In:
13+
* Bergum G.E., Philippou A.N., Horadam A.F. (eds) Applications of Fibonacci
14+
* Numbers. Springer, Dordrecht. https://doi.org/10.1007/978-94-011-3586-3_9
15+
*
16+
* This snippet is free. Feel free to improve on it
17+
*
18+
* We define a function fibonacciSearch() that takes an array of numbers,
19+
* the item (number) to be searched for and the length of the items in the array
20+
****************************************************************************/
21+
22+
const fibonacciSearch = (arr, x, n) => {
23+
let fib2 = 0 // (K-2)'th Fibonacci Number
24+
let fib1 = 1 // (K-1)'th Fibonacci Number.
25+
let fibK = fib2 + fib1 // Kth Fibonacci
26+
27+
/* We want to store the smallest fibonacci number smaller such that
28+
number is greater than or equal to n, we use fibK for this */
29+
while (fibK < n) {
30+
fib2 = fib1
31+
fib1 = fibK
32+
fibK = fib2 + fib1
33+
}
34+
// This marks the eliminated range from front
35+
let offset = -1
36+
37+
/* while there are elements to be checked. We compare arr[fib2] with x.
38+
When fibM becomes 1, fib2 becomes 0 */
39+
40+
while (fibK > 1) {
41+
// Check if fibK is a valid location
42+
const i = Math.min(offset + fib2, n - 1)
43+
44+
/* If x is greater than the value at
45+
index fib2, Partition the subarray array
46+
from offset to i */
47+
if (arr[i] < x) {
48+
fibK = fib1
49+
fib1 = fib2
50+
fib2 = fibK - fib1
51+
offset = i
52+
/* If x is greater than the value at
53+
index fib2, cut the subarray array
54+
from offset to i */
55+
} else if (arr[i] > x) {
56+
fibK = fib2
57+
fib1 = fib1 - fib2
58+
fib2 = fibK - fib1
59+
} else {
60+
// return index for found element
61+
return i
62+
}
63+
}
64+
65+
// comparing the last element with x */
66+
if (fib1 && arr[offset + 1] === x) {
67+
return offset + 1
68+
}
69+
// element not found. return -1
70+
return -1
71+
}
72+
// Example
73+
const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
74+
const n = myArray.length
75+
const x = 90
76+
const fibFinder = fibonacciSearch(myArray, x, n)
77+
console.log('Element found at index:', fibFinder)

String/CheckAnagram.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const checkAnagram = (str1, str2) => {
88

99
// If both strings have not same lengths then they can not be anagram.
1010
if (str1.length !== str2.length) {
11-
return 'Not Anagram'
11+
return 'Not anagrams'
1212
}
1313

1414
// Use hashmap to keep count of characters in str1
@@ -44,7 +44,4 @@ const checkAnagram = (str1, str2) => {
4444
return 'Anagrams'
4545
}
4646

47-
console.log(checkAnagram('abcd', 'bcad')) // should print anagram
48-
console.log(checkAnagram('abcd', 'abef')) // should print not anagram
49-
console.log(checkAnagram(10, 'abcd'))// should print Not String(s).
50-
console.log(checkAnagram('abs', 'abds'))// should print not anagram
47+
export { checkAnagram }

String/CheckAnagram.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { checkAnagram } from './CheckAnagram'
2+
3+
describe('checkAnagram', () => {
4+
it.each`
5+
inputOne | inputTwo
6+
${123456} | ${'abcd'}
7+
${[1, 2, 3, 4, 5, 6]} | ${'abcd'}
8+
${{ test: 'test' }} | ${'abcd'}
9+
${'abcd'} | ${123456}
10+
${'abcd'} | ${[1, 2, 3, 4, 5, 6]}
11+
${'abcd'} | ${{ test: 'test' }}
12+
`(
13+
'expects to return "Not string(s)" given values $inputOne and $inputTwo',
14+
({ inputOne, inputTwo }) => {
15+
const SUT = checkAnagram(inputOne, inputTwo)
16+
expect(SUT).toBe('Not string(s)')
17+
}
18+
)
19+
it('expects to return "Not anagram" if the arguments have different lengths', () => {
20+
const SUT = checkAnagram('abs', 'abds')
21+
expect(SUT).toBe('Not Anagram')
22+
})
23+
it('expects to return "Not anagram" if the arguments are not anagrams', () => {
24+
const SUT = checkAnagram('abcs', 'abds')
25+
expect(SUT).toBe('Not anagrams')
26+
})
27+
it('expects to return "Anagram" if the arguments are anagram', () => {
28+
const SUT = checkAnagram('abcd', 'bcad')
29+
expect(SUT).toBe('Anagrams')
30+
})
31+
})

0 commit comments

Comments
 (0)