ocaml: phase 5.1 island_count.ml baseline (6x7 grid, 5 components)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Count 4-connected components of 1-cells via DFS flood from every
unvisited 1-cell.
Grid (1s shown as #):
# # . . . # #
# . . . # # .
. . # # . . .
. # # # . # #
# . . . . # .
# # . # # # .
Components:
{(0,0),(0,1),(1,0)}
{(0,5),(0,6),(1,4),(1,5)}
{(2,2),(2,3),(3,1),(3,2),(3,3)}
{(3,5),(3,6),(4,5),(5,3),(5,4),(5,5)}
{(4,0),(5,0),(5,1)}
-> 5 islands
Complementary to flood_fill.ml (largest component); this counts
total components.
Tests recursive function returning () at early-exit branches,
ordered double-for entry pass triggering one fill per island root.
180 baseline programs total.
This commit is contained in:
@@ -80,6 +80,7 @@
|
|||||||
"huffman.ml": 224,
|
"huffman.ml": 224,
|
||||||
"int_sqrt.ml": 1027,
|
"int_sqrt.ml": 1027,
|
||||||
"is_prime.ml": 25,
|
"is_prime.ml": 25,
|
||||||
|
"island_count.ml": 5,
|
||||||
"fizz_classifier.ml": 540,
|
"fizz_classifier.ml": 540,
|
||||||
"fizzbuzz.ml": 57,
|
"fizzbuzz.ml": 57,
|
||||||
"flatten_tree.ml": 28,
|
"flatten_tree.ml": 28,
|
||||||
|
|||||||
37
lib/ocaml/baseline/island_count.ml
Normal file
37
lib/ocaml/baseline/island_count.ml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
let h = 6
|
||||||
|
let w = 7
|
||||||
|
let grid = [|
|
||||||
|
[| 1; 1; 0; 0; 0; 1; 1 |];
|
||||||
|
[| 1; 0; 0; 0; 1; 1; 0 |];
|
||||||
|
[| 0; 0; 1; 1; 0; 0; 0 |];
|
||||||
|
[| 0; 1; 1; 1; 0; 1; 1 |];
|
||||||
|
[| 1; 0; 0; 0; 0; 1; 0 |];
|
||||||
|
[| 1; 1; 0; 1; 1; 1; 0 |]
|
||||||
|
|]
|
||||||
|
|
||||||
|
let rec fill visited r c =
|
||||||
|
if r < 0 || r >= h || c < 0 || c >= w then ()
|
||||||
|
else if visited.(r).(c) || grid.(r).(c) = 0 then ()
|
||||||
|
else begin
|
||||||
|
visited.(r).(c) <- true;
|
||||||
|
fill visited (r - 1) c;
|
||||||
|
fill visited (r + 1) c;
|
||||||
|
fill visited r (c - 1);
|
||||||
|
fill visited r (c + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
let count_islands () =
|
||||||
|
let visited = Array.init h (fun _ -> Array.make w false) in
|
||||||
|
let count = ref 0 in
|
||||||
|
for r = 0 to h - 1 do
|
||||||
|
for c = 0 to w - 1 do
|
||||||
|
if grid.(r).(c) = 1 && not visited.(r).(c) then begin
|
||||||
|
count := !count + 1;
|
||||||
|
fill visited r c
|
||||||
|
end
|
||||||
|
done
|
||||||
|
done;
|
||||||
|
!count
|
||||||
|
;;
|
||||||
|
|
||||||
|
count_islands ()
|
||||||
@@ -407,6 +407,18 @@ _Newest first._
|
|||||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||||
recursive match, List.append, List.fold_left.
|
recursive match, List.append, List.fold_left.
|
||||||
|
- 2026-05-11 Phase 5.1 — island_count.ml baseline (count 4-connected
|
||||||
|
components of 1-cells in a 6×7 grid = 5). DFS flood from every
|
||||||
|
unvisited 1-cell. Counted islands:
|
||||||
|
{(0,0),(0,1),(1,0)}
|
||||||
|
{(0,5),(0,6),(1,4),(1,5)}
|
||||||
|
{(2,2),(2,3),(3,1),(3,2),(3,3)}
|
||||||
|
{(3,5),(3,6),(4,5),(5,3),(5,4),(5,5)}
|
||||||
|
{(4,0),(5,0),(5,1)}
|
||||||
|
Complementary to flood_fill.ml which measures largest component;
|
||||||
|
this counts them. Tests recursive returning `()` (unit) at
|
||||||
|
early-exit branches, ordered double-for entry pass that triggers
|
||||||
|
one fill per island root. 180 baseline programs total.
|
||||||
- 2026-05-11 Phase 5.1 — dp_word_break.ml baseline (word-break DP
|
- 2026-05-11 Phase 5.1 — dp_word_break.ml baseline (word-break DP
|
||||||
over 5 strings with 9-word dictionary; 4 strings segmentable).
|
over 5 strings with 9-word dictionary; 4 strings segmentable).
|
||||||
dp[i] = ∃ word w of length wl ≤ i with prefix s[i−wl..i]=w and
|
dp[i] = ∃ word w of length wl ≤ i with prefix s[i−wl..i]=w and
|
||||||
|
|||||||
Reference in New Issue
Block a user