Word wheel: Difference between revisions

1,954 bytes added ,  2 years ago
(Added XPL0 example.)
Line 2,089:
claremont with central letter 'a'
spearmint with central letter 'a'
</pre>
 
=={{header|Picat}}==
<lang Picat>main =>
MinLen = 3,
MaxLen = 9,
Chars = "ndeokgelw",
MustContain = 'k',
 
WordList = "unixdict.txt",
Words = read_file_lines(WordList),
Res = word_wheel(Chars,Words,MustContain,MinLen, MaxLen),
println(Res),
println(len=Res.len),
nl.
 
word_wheel(Chars,Words,MustContain,MinLen,MaxLen) = Res.reverse =>
Chars := to_lowercase(Chars),
D = make_hash(Chars),
Res = [],
foreach(W in Words, W.len >= MinLen, W.len <= MaxLen, membchk(MustContain,W))
WD = make_hash(W),
Check = true,
foreach(C in keys(WD), break(Check == false))
if not D.has_key(C) ; WD.get(C,0) > D.get(C,0) then
Check := false
end
end,
if Check == true then
Res := [W|Res]
end
end.
 
% Returns a map of the elements and their occurrences
% in the list L.
make_hash(L) = D =>
D = new_map(),
foreach(E in L)
D.put(E,D.get(E,0)+1)
end.</lang>
 
{{out}}
<pre>[eke,elk,keel,keen,keg,ken,keno,knee,kneel,knew,know,knowledge,kong,leek,week,wok,woke]
len = 17</pre>
 
'''Optimal word(s)''':
<lang Picat>main =>
WordList = "unixdict.txt",
MinLen = 3,
MaxLen = 9,
Words = [Word : Word in read_file_lines(WordList), Word.len >= MinLen, Word.len <= MaxLen],
TargetWords = [Word : Word in Words, Word.len == MaxLen],
MaxResWord = [],
MaxResLen = 0,
foreach(Word in TargetWords)
foreach(MustContain in Word.remove_dups)
Res = word_wheel(Word,Words,MustContain,MinLen, MaxLen),
Len = Res.len,
if Len >= MaxResLen then
if Len == MaxResLen then
MaxResWord := MaxResWord ++ [[word=Word,char=MustContain]]
else
MaxResWord := [[word=Word,char=MustContain]],
MaxResLen := Len
end
end
end
end,
println(maxLResen=MaxResLen),
println(maxWord=MaxResWord).</lang>
 
{{out}}
<pre>
maxReLen = 215
maxWord = [[word = claremont,char = a],[word = spearmint,char = a]]
</pre>
 
495

edits