ocaml: phase 5.1 wildcard_match.ml baseline (* / ? matcher, 6/18 match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Recursive wildcard matcher:
let rec is_match s i p j =
if j = String.length p then i = String.length s
else if p.[j] = '*' then
is_match s i p (j + 1) (* * matches empty *)
|| (i < String.length s && is_match s (i + 1) p j) (* * eats char *)
else
i < String.length s
&& (p.[j] = '?' || p.[j] = s.[i])
&& is_match s (i + 1) p (j + 1)
Patterns vs texts:
a*b | aaab abc abxyz xy xyz axby -> 1 match
?b*c | aaab abc abxyz xy xyz axby -> 1 match
*x*y* | aaab abc abxyz xy xyz axby -> 4 matches
total = 6
Tests deeply nested short-circuit && / ||, char equality on
pattern bytes, doubly-nested List.iter cross product.
173 baseline programs total.
This commit is contained in:
@@ -157,6 +157,7 @@
|
||||
"tic_tac_toe.ml": 1,
|
||||
"topo_dfs.ml": 24135,
|
||||
"topo_sort.ml": 6,
|
||||
"wildcard_match.ml": 6,
|
||||
"word_freq.ml": 8,
|
||||
"xor_cipher.ml": 601,
|
||||
"zerosafe.ml": 28,
|
||||
|
||||
24
lib/ocaml/baseline/wildcard_match.ml
Normal file
24
lib/ocaml/baseline/wildcard_match.ml
Normal file
@@ -0,0 +1,24 @@
|
||||
let rec is_match s i p j =
|
||||
if j = String.length p then i = String.length s
|
||||
else if p.[j] = '*' then
|
||||
is_match s i p (j + 1)
|
||||
|| (i < String.length s && is_match s (i + 1) p j)
|
||||
else
|
||||
i < String.length s
|
||||
&& (p.[j] = '?' || p.[j] = s.[i])
|
||||
&& is_match s (i + 1) p (j + 1)
|
||||
|
||||
let count_match patterns texts =
|
||||
let count = ref 0 in
|
||||
List.iter (fun p ->
|
||||
List.iter (fun t ->
|
||||
if is_match t 0 p 0 then count := !count + 1
|
||||
) texts
|
||||
) patterns;
|
||||
!count
|
||||
|
||||
;;
|
||||
|
||||
let patterns = ["a*b"; "?b*c"; "*x*y*"] in
|
||||
let texts = ["aaab"; "abc"; "abxyz"; "xy"; "xyz"; "axby"] in
|
||||
count_match patterns texts
|
||||
Reference in New Issue
Block a user