Skip to content
Snippets Groups Projects
Commit 3defa09d authored by Frédéric Bapst's avatar Frédéric Bapst
Browse files

add s09

parent 459b163e
No related branches found
No related tags found
No related merge requests found
package s09;
// ------------------------------------------------------------
public class StringSearching {
// ------------------------------------------------------------
static /*final*/ int HASHER = 301; // Maybe also try with 7 and 46237
static /*final*/ int BASE = 256; // Please also try with 257
// ---------------------
static int firstFootprint(String s, int len) {
return -1;
// TODO - A COMPLETER
}
// ---------------------
// must absolutely be O(1)
// coef is (BASE power P.LENGTH-1) mod HASHER
static int nextFootprint(int previousFootprint, char dropChar, char newChar, int coef) {
return -1;
// TODO - A COMPLETER
// h = previousFootprint
// h = h - ... // dropChar (bien réfléchir !)
// h = h * ... // shift
// h = h + ... // newChar
}
// ---------------------
// Rabin-Karp algorithm
public static int indexOf_rk(String t, String p) {
return -1;
// TODO - A COMPLETER
}
}
package s09;
import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
// ------------------------------------------------------------
public class StringSearchingTestJU {
// ------------------------------------------------------------
@Test
public void testRabinKarpHasher301Base256() {
System.out.println(" --- Trying with original HASHER and BASE...");
StringSearching.HASHER = 301;
StringSearching.BASE = 256;
int max = 400;
test(max);
}
@org.junit.jupiter.api.Test
public void testRabinKarpHasher7Base257() {
System.out.println(" --- Trying with another HASHER and BASE...");
StringSearching.HASHER = 77;
StringSearching.BASE = 257;
int max = 400;
test(max);
}
// ------------------------------------------------------------
public static String rndWord(int length, String alphabet, Random r) {
String s="";
int x;
for (int i=0; i<length; i++) {
x = r.nextInt(alphabet.length());
s += alphabet.charAt(x);
}
return s;
}
// ------------------------------------------------------------
private static void test(int n) {
Random r = new Random();
String t, p;
int j;
int occ = 0;
String alphabet = "ab";
assertTrue( 0==StringSearching.indexOf_rk("wxyz", "wxyz"));
assertTrue(10==StringSearching.indexOf_rk("0123456789wxyz", "wxyz"));
assertTrue( 0==StringSearching.indexOf_rk("wxyz0123456789wxya", "wxyz"));
assertTrue(-1==StringSearching.indexOf_rk("wxyz0123456789wxywxyz0123456789wxy", "a"));
t = rndWord(n, alphabet, r);
for (int i=0; i<n*n; i++) {
int l = (int) Math.sqrt(n);
p = rndWord(1+r.nextInt(l), alphabet, r);
j = t.indexOf(p);
if (j!=-1) occ++;
assertTrue(j == StringSearching.indexOf_rk(t, p));
}
System.out.println("Nb of method calls : "+n*n);
System.out.println("Nb of found occurrences : "+occ);
System.out.println("Test passed successfully");
}
// ------------------------------------------------------------
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment