Jump to content

Word ladder: Difference between revisions

Added 11l
m (→‎Breadth-first search: fixed a typo)
(Added 11l)
Line 19:
{{Template:Strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<lang 11l>F isOneAway(word1, word2)
V result = 0B
L(i) 0 .< word1.len
I word1[i] != word2[i]
I result
R 0B
E
result = 1B
R result
 
DefaultDict[Int, [String]] words
 
L(word) File(‘unixdict.txt’).read().split("\n")
words[word.len] [+]= word
 
F find_path(start, target)
V lg = start.len
assert(target.len == lg, ‘Source and destination must have same length.’)
assert(start C :words[lg], ‘Source must exist in the dictionary.’)
assert(target C :words[lg], ‘Destination must exist in the dictionary.’)
 
V currPaths = [[start]]
V pool = copy(:words[lg])
 
L
[[String]] newPaths
[String] added
L(candidate) pool
L(path) currPaths
I isOneAway(candidate, path.last)
V newPath = path [+] [candidate]
I candidate == target
R newPath
E
newPaths.append(newPath)
added.append(candidate)
L.break
 
I newPaths.empty
L.break
currPaths = move(newPaths)
L(w) added
pool.remove(w)
 
R [String]()
 
L(start, target) [(‘boy’, ‘man’), (‘girl’, ‘lady’), (‘john’, ‘jane’), (‘child’, ‘adult’), (‘cat’, ‘dog’), (‘lead’, ‘gold’), (‘white’, ‘black’), (‘bubble’, ‘tickle’)]
V path = find_path(start, target)
I path.empty
print(‘No path from "’start‘" to "’target‘".’)
E
print(path.join(‘ -> ’))</lang>
 
{{out}}
<pre>
boy -> bay -> ban -> man
girl -> gill -> gall -> gale -> gaze -> laze -> lazy -> lady
john -> cohn -> conn -> cone -> cane -> jane
No path from "child" to "adult".
cat -> cot -> cog -> dog
lead -> load -> goad -> gold
white -> whine -> chine -> chink -> clink -> blink -> blank -> black
bubble -> babble -> gabble -> garble -> gargle -> gaggle -> giggle -> jiggle -> jingle -> tingle -> tinkle -> tickle
</pre>
 
=={{header|C++}}==
1,480

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.