ocaml: phase 5.1 anagram_check.ml baseline (char-frequency array, 2/4 anagrams)
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
to_counts builds a 256-slot int array of character frequencies:
let to_counts s =
let counts = Array.make 256 0 in
for i = 0 to String.length s - 1 do
let c = Char.code s.[i] in
counts.(c) <- counts.(c) + 1
done;
counts
same_counts compares two arrays element-by-element via for loop +
bool ref. is_anagram composes them.
Four pairs:
listen ~ silent true
hello !~ world false
anagram ~ nagaram true
abc !~ abcd false (length differs)
sum 2
Exercises Array.make + arr.(i) + arr.(i) <- v + nested for loops +
Char.code + s.[i].
57 baseline programs total.
This commit is contained in:
23
lib/ocaml/baseline/anagram_check.ml
Normal file
23
lib/ocaml/baseline/anagram_check.ml
Normal file
@@ -0,0 +1,23 @@
|
||||
let to_counts s =
|
||||
let counts = Array.make 256 0 in
|
||||
for i = 0 to String.length s - 1 do
|
||||
let c = Char.code s.[i] in
|
||||
counts.(c) <- counts.(c) + 1
|
||||
done;
|
||||
counts
|
||||
|
||||
let same_counts a b =
|
||||
let result = ref true in
|
||||
for i = 0 to 255 do
|
||||
if a.(i) <> b.(i) then result := false
|
||||
done;
|
||||
!result
|
||||
|
||||
let is_anagram s t = same_counts (to_counts s) (to_counts t)
|
||||
|
||||
;;
|
||||
|
||||
(if is_anagram "listen" "silent" then 1 else 0) +
|
||||
(if is_anagram "hello" "world" then 1 else 0) +
|
||||
(if is_anagram "anagram" "nagaram" then 1 else 0) +
|
||||
(if is_anagram "abc" "abcd" then 1 else 0)
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"ackermann.ml": 125,
|
||||
"anagram_check.ml": 2,
|
||||
"anagrams.ml": 3,
|
||||
"bag.ml": 3,
|
||||
"bigint_add.ml": 28,
|
||||
|
||||
@@ -407,6 +407,13 @@ _Newest first._
|
||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||
recursive match, List.append, List.fold_left.
|
||||
- 2026-05-09 Phase 5.1 — anagram_check.ml baseline (char-frequency
|
||||
array, 2/4 pairs are anagrams). to_counts builds a 256-slot int
|
||||
array of character frequencies. same_counts compares two arrays
|
||||
element-by-element. is_anagram = same_counts on both. listen ~
|
||||
silent ✓, hello ≠ world, anagram ~ nagaram ✓, abc ≠ abcd → 2.
|
||||
Exercises Array.make + arr.(i) + arr.(i) <- v + nested for loops
|
||||
+ Char.code + s.[i]. 57 baseline programs total.
|
||||
- 2026-05-09 Phase 5.1 — exception_user.ml baseline (user-defined
|
||||
exception with int payload, 4+5+7+10 = 26). Defines `exception
|
||||
Negative of int`, `safe_sqrt` raises it on negative input, and
|
||||
|
||||
Reference in New Issue
Block a user