mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-10 14:22:53 +08:00
Rabin Karp Search Algorithm (#1545)
* Search: Rabin-Karp algorithm * Prettier Style * Search: Rabin-Karp adding reference * Search: Rabin-Karp styling and remove unecessary logging * Search: Rabin-Karp review notes * Simplify return * Updated Documentation in README.md --------- Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
30
Search/test/RabinKarp.test.js
Normal file
30
Search/test/RabinKarp.test.js
Normal file
@ -0,0 +1,30 @@
|
||||
import { rabinKarpSearch } from '../RabinKarp'
|
||||
|
||||
describe('Rabin-Karp Search', function () {
|
||||
it('should find the pattern in the text', function () {
|
||||
const text = 'ABABDABACDABABCABAB'
|
||||
const pattern = 'DAB'
|
||||
const expected = [4, 9]
|
||||
|
||||
const result = rabinKarpSearch(text, pattern)
|
||||
expect(result).to.deep.equal(expected)
|
||||
})
|
||||
|
||||
it('should handle multiple occurrences of the pattern', function () {
|
||||
const text = 'ABABABABABAB'
|
||||
const pattern = 'ABAB'
|
||||
const expected = [2, 4, 6, 8]
|
||||
|
||||
const result = rabinKarpSearch(text, pattern)
|
||||
expect(result).to.deep.equal(expected)
|
||||
})
|
||||
|
||||
it('should handle pattern not found', function () {
|
||||
const text = 'ABCD'
|
||||
const pattern = 'XYZ'
|
||||
const expected = []
|
||||
|
||||
const result = rabinKarpSearch(text, pattern)
|
||||
expect(result).to.deep.equal(expected)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user