ocaml: phase 5.1 frequency.ml baseline + Format module alias (+2 tests, 498 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
frequency.ml exercises the recently-added Hashtbl.iter / fold + Hashtbl.find_opt + s.[i] indexing + for-loop together: build a char-count table for 'abracadabra' then take the max via Hashtbl.fold. Expected = 5 (a x 5). Total 25 baseline programs. Format module added as a thin alias of Printf — sprintf, printf, and asprintf all delegate to Printf.sprintf. The dynamic runtime doesn't distinguish boxes/breaks, so format strings work the same as in Printf and most Format-using OCaml programs now compile.
This commit is contained in:
18
lib/ocaml/baseline/frequency.ml
Normal file
18
lib/ocaml/baseline/frequency.ml
Normal file
@@ -0,0 +1,18 @@
|
||||
let count_chars s =
|
||||
let t = Hashtbl.create 8 in
|
||||
for i = 0 to String.length s - 1 do
|
||||
let c = s.[i] in
|
||||
let n = match Hashtbl.find_opt t c with
|
||||
| Some v -> v + 1
|
||||
| None -> 1
|
||||
in
|
||||
Hashtbl.replace t c n
|
||||
done;
|
||||
t
|
||||
|
||||
let max_count t =
|
||||
Hashtbl.fold (fun _ v acc -> if v > acc then v else acc) t 0
|
||||
|
||||
;;
|
||||
|
||||
max_count (count_chars "abracadabra")
|
||||
Reference in New Issue
Block a user