codewars: solve 'consecutive strings' challenge

This commit is contained in:
Ivan R. 2024-02-22 11:05:32 +05:00
parent 82091b0abd
commit e0cc88e3ff
Signed by: lumin
GPG key ID: 927D3132247BDE4E

View file

@ -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;;