DEV Community

loading...

Discussion on: Daily Challenge #124 - Middle Me

Collapse
citizen428 profile image
Michael Kohl

F# solution:

module DailyChallenge

let middleMe x y n =
    if n % 2 = 0 && n > 0 then
        let ys = String.replicate (n / 2) y
        sprintf "%s%s%s" ys x ys
    else
        x

Tests:

module DailyChallengeTests

open FsUnit.Xunit
open Xunit
open DailyChallenge

[<Fact>]
let ``places a character in the middle``() =
    middleMe "A" "*" 10 |> should equal "*****A*****"

[<Fact>]
let ``returns the character if it cannot be placed in the middle``() =
    middleMe "A" "*" 9 |> should equal "A"

[<Fact>]
let ``returns the character if n is <= 0``() =
    middleMe "A" "*" -2 |> should equal "A"