Word ladder: Difference between revisions

Content deleted Content added
Util (talk | contribs)
→‎{{header|Raku}}: Added Raku solution
Wherrera (talk | contribs)
julia example
Line 35: Line 35:
john -> cohn -> conn -> cone -> cane -> jane
john -> cohn -> conn -> cone -> cane -> jane
child into adult can't be done
child into adult can't be done
</pre>

=={{header|Julia}}==
<lang julia>dict = Dict(word => length(word) for word in split(read("unixdict.txt", String), r"\s+"))

function targeted_mutations(s::AbstractString, target::AbstractString)
working, tried = [[s]], Set{String}()
while all(a -> a[end] != target, working)
newworking = Vector{Vector{String}}()
for arr in working
s = arr[end]
for j in 1:length(s), c in 'a':'z'
w = s[1:j-1] * c * s[j+1:end]
if haskey(dict, w) && w != s && !(w in tried)
push!(newworking, [arr; w])
end
push!(tried, w)
end
end
isempty(newworking) && return [["This cannot be done."]]
working = newworking
end
return filter(a -> a[end] == target, working)
end

println("boy to man: ", targeted_mutations("boy", "man"))
println("girl to lady: ", targeted_mutations("girl", "lady"))
println("john to jane: ", targeted_mutations("john", "jane"))
println("child to adult: ", targeted_mutations("child", "adult"))
</lang>{{out}}
<pre>
boy to man: [["boy", "bay", "may", "man"]]
girl to lady: [["girl", "gill", "gall", "gale", "gaze", "laze", "lazy", "lady"]]
john to jane: [["john", "cohn", "conn", "cone", "cane", "jane"]]
child to adult: [["This cannot be done."]]
</pre>
</pre>