codewars: solve 'consecutive strings' challenge
This commit is contained in:
parent
82091b0abd
commit
e0cc88e3ff
1 changed files with 15 additions and 0 deletions
15
codewars/ocaml/rank-up/consecutive-strings/main.ml
Normal file
15
codewars/ocaml/rank-up/consecutive-strings/main.ml
Normal 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;;
|
Loading…
Reference in a new issue