Word ladder: Difference between revisions

Content added Content deleted
m (added related tasks.)
(Promote to full task)
Line 1: Line 1:
{{Draft task}}
{{Task}}
Yet another shortest path problem. Given two words of equal length the task is to transpose the first into the second.
Yet another shortest path problem. Given two words of equal length the task is to transpose the first into the second.


Line 140: Line 140:
<lang fsharp>
<lang fsharp>
// Word ladder: Nigel Galloway. June 5th., 2021
// Word ladder: Nigel Galloway. June 5th., 2021
let fG n g=n|>List.partition(fun n->2>Seq.fold2(fun z n g->z+if n=g then 0 else 1) 0 n g)
open System.Text.RegularExpressions
let fN g=List.init(String.length g)(fun n->let g=g.ToCharArray() in g.[n]<-'.'; g|>System.String)|>String.concat "|"
let fG n g=let g=fN g in n|>List.partition(fun n->Regex.IsMatch(n,g))
let wL n g=let dict=seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(Seq.length>>(=)(Seq.length n))|>List.ofSeq|>List.except [n]
let wL n g=let dict=seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(Seq.length>>(=)(Seq.length n))|>List.ofSeq|>List.except [n]
let (|Done|_|) n=n|>List.tryFind((=)g)
let (|Done|_|) n=n|>List.tryFind((=)g)