DEV Community

Discussion on: Daily Coding Puzzles - Nov 4th - Nov 9th

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

F#

let lastWord s =
    let update (first, word) letter =
        if letter >= first then (letter, string letter + word)
        else                    (first, word + string letter)
    s |> Seq.fold update ('A', "") |> snd

Testing it (console)

    [
        "CAB", "CAB"
        "JAM", "MJA"
        "CODE", "OCDE"
        "ABAAB", "BBAAA"
        "CABCBBABC", "CCCABBBAB"
        "ABCABCABC", "CCCBAABAB"
        "ZXCASDQWE", "ZXCASDQWE"
    ]
    |> List.iter (fun (input, expected) ->
        let actual = lastWord input
        let e = if expected = actual then "√" else "X"
        printfn "%s %s ==> %s, %s" e input expected actual
    )
    // √ CAB ==> CAB, CAB
    // √ JAM ==> MJA, MJA
    // √ CODE ==> OCDE, OCDE
    // √ ABAAB ==> BBAAA, BBAAA
    // √ CABCBBABC ==> CCCABBBAB, CCCABBBAB
    // √ ABCABCABC ==> CCCBAABAB, CCCBAABAB
    // √ ZXCASDQWE ==> ZXCASDQWE, ZXCASDQWE

Collapse
 
aspittel profile image
Ali Spittel

Awesome -- this one (for me) was a lot easier than they made it sound!

Thread Thread
 
kspeakman profile image
Kasey Speakman • Edited

Same here. The hard part was understanding the problem. (It felt very much like "A train leaving SF at 50kph ..." word problems.) But after that the code wasn't so bad.

Thread Thread
 
thejessleigh profile image
jess unrein

I think the possible "gotcha" here is that they do not want a reverse alphabetically sorted string, which, if you're not careful about the requirements, could be what you try to build and have it trip you up.