ocaml: phase 5.1 hamming.ml baseline (Hamming distance, 3+2-1 = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Counts position-wise differences between two strings of equal
length; returns -1 sentinel for length mismatch:
let hamming s t =
if String.length s <> String.length t then -1
else
let d = ref 0 in
for i = 0 to String.length s - 1 do
if s.[i] <> t.[i] then d := !d + 1
done;
!d
Three test cases:
'karolin' vs 'kathrin' 3 (positions 2,3,4)
'1011101' vs '1001001' 2 (positions 2,4)
'abc' vs 'abcd' -1 (length mismatch)
sum 4
91 baseline programs total.
This commit is contained in:
@@ -34,6 +34,7 @@
|
|||||||
"grid_paths.ml": 210,
|
"grid_paths.ml": 210,
|
||||||
"group_consec.ml": 53,
|
"group_consec.ml": 53,
|
||||||
"hailstone.ml": 111,
|
"hailstone.ml": 111,
|
||||||
|
"hamming.ml": 4,
|
||||||
"hanoi.ml": 1023,
|
"hanoi.ml": 1023,
|
||||||
"hist.ml": 75,
|
"hist.ml": 75,
|
||||||
"int_sqrt.ml": 1027,
|
"int_sqrt.ml": 1027,
|
||||||
|
|||||||
13
lib/ocaml/baseline/hamming.ml
Normal file
13
lib/ocaml/baseline/hamming.ml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
let hamming s t =
|
||||||
|
if String.length s <> String.length t then -1
|
||||||
|
else begin
|
||||||
|
let d = ref 0 in
|
||||||
|
for i = 0 to String.length s - 1 do
|
||||||
|
if s.[i] <> t.[i] then d := !d + 1
|
||||||
|
done;
|
||||||
|
!d
|
||||||
|
end
|
||||||
|
|
||||||
|
;;
|
||||||
|
|
||||||
|
hamming "karolin" "kathrin" + hamming "1011101" "1001001" + hamming "abc" "abcd"
|
||||||
@@ -407,6 +407,13 @@ _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-09 Phase 5.1 — hamming.ml baseline (Hamming distance,
|
||||||
|
3 + 2 + (-1) = 4). Counts position-wise differences in equal-length
|
||||||
|
strings; returns -1 sentinel for length mismatch.
|
||||||
|
karolin vs kathrin 3 (positions 2,3,4)
|
||||||
|
1011101 vs 1001001 2 (positions 2,4)
|
||||||
|
abc vs abcd -1 (length mismatch)
|
||||||
|
Sum = 4. 91 baseline programs total.
|
||||||
- 2026-05-09 Phase 5.1 — xor_cipher.ml baseline (XOR roll-key
|
- 2026-05-09 Phase 5.1 — xor_cipher.ml baseline (XOR roll-key
|
||||||
encryption, round-trip → 601). For each character, XOR with the
|
encryption, round-trip → 601). For each character, XOR with the
|
||||||
corresponding key char (key cycled via `i mod kn`). Encrypts
|
corresponding key char (key cycled via `i mod kn`). Encrypts
|
||||||
|
|||||||
Reference in New Issue
Block a user