-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0315c8a
commit d84549a
Showing
7 changed files
with
98 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { | |
|
||
let count = 0 | ||
while (a) { | ||
a &= (a - 1) | ||
a &= a - 1 | ||
count++ | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { | |
n = Math.floor(n / 10) | ||
n_sq = Math.floor(n_sq / 10) | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
// https://projecteuler.net/problem=6 | ||
|
||
export const squareDifference = (num = 100) => { | ||
let sumOfSquares = (num)*(num+1)*(2*num+1)/6 | ||
let sums = (num*(num+1))/2 | ||
let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6 | ||
let sums = (num * (num + 1)) / 2 | ||
|
||
return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { | |
} | ||
|
||
return -1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Implements the Knuth-Morris-Pratt (KMP) algorithm for pattern searching. | ||
* | ||
* The KMP algorithm is a string searching algorithm that utilizes the concept of prefix tables or failure functions | ||
* to efficiently match patterns in strings. It avoids unnecessary comparisons by taking advantage of the information | ||
* gained from previous comparisons. | ||
* | ||
* This implementation constructs a prefix table that stores the longest proper prefix that is also a suffix for each | ||
* prefix of the pattern. This table is then used to guide the pattern matching process, skipping over characters that | ||
* are known to not match. | ||
* | ||
* The algorithm returns the starting index of the first occurrence of the pattern in the text. If the pattern is not | ||
* found, the algorithm returns -1. | ||
* | ||
* Reference: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm | ||
*/ | ||
|
||
function KMPSearch(text, pattern) { | ||
let lps = new Array(pattern.length).fill(0) | ||
computeLPS(pattern, lps) | ||
let i = 0 | ||
let j = 0 | ||
while (i < text.length) { | ||
if (pattern[j] == text[i]) { | ||
j++ | ||
i++ | ||
} | ||
if (j == pattern.length) { | ||
return i - j | ||
} else if (i < text.length && pattern[j] != text[i]) { | ||
if (j != 0) j = lps[j - 1] | ||
else i = i + 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
function computeLPS(pattern, lps) { | ||
let len = 0 | ||
let i = 1 | ||
while (i < pattern.length) { | ||
if (pattern[i] == pattern[len]) { | ||
len++ | ||
lps[i] = len | ||
i++ | ||
} else { | ||
if (len != 0) { | ||
len = lps[len - 1] | ||
} else { | ||
lps[i] = 0 | ||
i++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
export { KMPSearch } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { KMPSearch } from '../KMP' | ||
|
||
describe('Rabin-Karp Search', function () { | ||
test('KMP algorithm test 1', () => { | ||
let text = 'AAAAA' | ||
let pattern = 'AAA' | ||
expect(KMPSearch(text, pattern)).toBe(0) | ||
}) | ||
|
||
test('KMP algorithm test 2', () => { | ||
let text = 'ABABDABACDABABCABAB' | ||
let pattern = 'DABA' | ||
expect(KMPSearch(text, pattern)).toBe(4) | ||
}) | ||
|
||
test('KMP algorithm test 3', () => { | ||
let text = 'ABABDABACDABABCABAB' | ||
let pattern = 'ABABCABAB' | ||
expect(KMPSearch(text, pattern)).toBe(10) | ||
}) | ||
|
||
test('KMP algorithm test 4', () => { | ||
let text = 'ABABDABACDABABCABAB' | ||
let pattern = 'XYZ' | ||
expect(KMPSearch(text, pattern)).toBe(-1) | ||
}) | ||
}) |