DEV Community

loading...

Discussion on: Daily Challenge #60 - Find the Missing Letter

Collapse
citizen428 profile image
Michael Kohl

F#:

module MissingLetter

let private missingLetter' letters =
    letters
    |> List.pairwise
    |> List.tryFind (fun (a, b) -> (int b) - (int a) > 1)
    |> Option.map (fun (a, _) -> int a + 1 |> char)

let missingLetter (letters : char list) : char option =
    if letters.Length < 2 then None
    else missingLetter' letters

Tests:

module MissingLetterTest

open FsUnit.Xunit
open Xunit
open MissingLetter

[<Fact>]
let ``missing lowercase letter`` () = 
   missingLetter [ 'a'; 'b'; 'c'; 'd'; 'f' ] |> should equal (Some 'e')

[<Fact>]
let ``shortest possible `` () = 
   missingLetter [ 'a'; 'c' ] |> should equal (Some 'b')

[<Fact>]
let ``missing uppercase letter`` () = 
   missingLetter [ 'O'; 'Q'; 'R'; 'S' ] |> should equal (Some 'P')

[<Fact>]
let ``no missing letter`` () =
    missingLetter [ 'a'; 'b'; 'c' ] |> should equal None

[<Fact>]
let ``single element list`` () =
    missingLetter [ 'a' ] |> should equal None