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

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:
2026-05-09 18:27:50 +00:00
parent 60e3ce1c96
commit df6efeb68e
3 changed files with 21 additions and 0 deletions

View File

@@ -34,6 +34,7 @@
"grid_paths.ml": 210,
"group_consec.ml": 53,
"hailstone.ml": 111,
"hamming.ml": 4,
"hanoi.ml": 1023,
"hist.ml": 75,
"int_sqrt.ml": 1027,

View 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"