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:
Mahfoudh Arous
2023-10-30 06:30:31 +01:00
committed by GitHub
parent 889d9c361d
commit d74f242ac4
3 changed files with 95 additions and 0 deletions

View 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)
})
})