From e0cc88e3ffeae28787653b1ca8e97e86d2281a02 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 22 Feb 2024 11:05:32 +0500 Subject: [PATCH] codewars: solve 'consecutive strings' challenge --- .../ocaml/rank-up/consecutive-strings/main.ml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 codewars/ocaml/rank-up/consecutive-strings/main.ml diff --git a/codewars/ocaml/rank-up/consecutive-strings/main.ml b/codewars/ocaml/rank-up/consecutive-strings/main.ml new file mode 100644 index 0000000..f59bede --- /dev/null +++ b/codewars/ocaml/rank-up/consecutive-strings/main.ml @@ -0,0 +1,15 @@ +let rec takeFirstN (xs : string list) (n : int) (acc : string) : string = + match n with + | 0 -> acc + | x -> takeFirstN (List.tl xs) (n-1) (acc ^ (List.nth xs 0));; + +let rec joinK (xs : string list) (acc : string list) (k : int) : string list = + if List.length xs < k then acc + else joinK (List.tl xs) (acc @ [takeFirstN xs k ""]) k;; + +let longestConsec (xs : string list) (k : int) : string = + if (List.length xs) == 0 || (List.length xs) < k || k <= 0 + then "" + else + let pairs = joinK xs [] k in + List.fold_left (fun acc i -> if (String.length i) > (String.length acc) then i else acc) "" pairs;;