Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Set-consolidation/OCaml/set-consolidation.ocaml
Normal file
40
Task/Set-consolidation/OCaml/set-consolidation.ocaml
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
let join a b =
|
||||
List.fold_left (fun acc v ->
|
||||
if List.mem v acc then acc else v::acc
|
||||
) b a
|
||||
|
||||
let share a b = List.exists (fun x -> List.mem x b) a
|
||||
|
||||
let extract p lst =
|
||||
let rec aux acc = function
|
||||
| x::xs -> if p x then Some (x, List.rev_append acc xs) else aux (x::acc) xs
|
||||
| [] -> None
|
||||
in
|
||||
aux [] lst
|
||||
|
||||
let consolidate sets =
|
||||
let rec aux acc = function
|
||||
| [] -> List.rev acc
|
||||
| x::xs ->
|
||||
match extract (share x) xs with
|
||||
| Some (y, ys) -> aux acc ((join x y) :: ys)
|
||||
| None -> aux (x::acc) xs
|
||||
in
|
||||
aux [] sets
|
||||
|
||||
let print_sets sets =
|
||||
print_string "{ ";
|
||||
List.iter (fun set ->
|
||||
print_string "{";
|
||||
print_string (String.concat " " set);
|
||||
print_string "} "
|
||||
) sets;
|
||||
print_endline "}"
|
||||
|
||||
let () =
|
||||
print_sets (consolidate [["A";"B"]; ["C";"D"]]);
|
||||
print_sets (consolidate [["A";"B"]; ["B";"C"]]);
|
||||
print_sets (consolidate [["A";"B"]; ["C";"D"]; ["D";"B"]]);
|
||||
print_sets (consolidate [["H";"I";"K"]; ["A";"B"]; ["C";"D"]; ["D";"B"];
|
||||
["F";"G";"H"]]);
|
||||
;;
|
||||
Loading…
Add table
Add a link
Reference in a new issue