Word wheel: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(42 intermediate revisions by 19 users not shown)
Line 31:
Specifically:
:* Find all words of 3 or more letters using only the letters in the string   '''ndeokgelw'''.
:* All words must contain the central letter &nbsp; <big>'''kK'''.</big>
:* Each letter may be used only as many times as it appears in the string.
:* For this task we'll use lowercase English letters exclusively.
Line 51:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V GRID =
‘N D E
O K G
Line 79:
V chars = GRID.lowercase().split_py().join(‘’)
V found = solve(chars, dictionary' getwords())
print(found.join("\n"))</langsyntaxhighlight>
 
{{out}}
Line 108:
(the given ~206kb <code>unixdict.txt</code> works fine).
 
<langsyntaxhighlight lang="8080asm">puts: equ 9 ; CP/M syscall to print string
fopen: equ 15 ; CP/M syscall to open a file
fread: equ 20 ; CP/M syscall to read from file
Line 218:
wheel: ds 9 ; Room for wheel
wcpy: ds 9 ; Copy of wheel (to mark characters used)
word: equ $ ; Room for current word</langsyntaxhighlight>
 
{{out}}
Line 244:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">wordwheel←{
words←((~∊)∘⎕TC⊆⊢) 80 ¯1⎕MAP ⍵
match←{
Line 255:
words←(⍺∘match¨words)/words
(⍺⍺≤≢¨words)/words
}</langsyntaxhighlight>
{{out}}
<pre> 'ndeokgelw' (3 wordwheel) 'unixdict.txt'
eke elk keel keen keg ken keno knee kneel knew know knowledge kong leek week wok woke </pre>
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 642:
return lst
end tell
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>17 matches:
Line 660:
kong
leek
week
wok
woke</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">letters := ["N", "D", "E", "O", "K", "G", "E", "L", "W"]
 
FileRead, wList, % A_Desktop "\unixdict.txt"
result := ""
for word in Word_wheel(wList, letters, 3)
result .= word "`n"
MsgBox % result
return
 
Word_wheel(wList, letters, minL){
oRes := []
for i, w in StrSplit(wList, "`n", "`r")
{
if (StrLen(w) < minL)
continue
word := w
for i, l in letters
w := StrReplace(w, l,,, 1)
if InStr(word, letters[5]) && !StrLen(w)
oRes[word] := true
}
return oRes
}</syntaxhighlight>
{{out}}
<pre>eke
elk
k
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
ok
week
wok
Line 665 ⟶ 710:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f WORD_WHEEL.AWK letters unixdict.txt
# the required letter must be first
Line 702 ⟶ 747:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 710 ⟶ 755:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT A-Z
20 DATA "ndeokgelw","unixdict.txt"
30 READ WH$, F$
Line 725 ⟶ 770:
140 C=0: FOR I=1 TO LEN(C$): C=C-(MID$(C$,I,1)="@"): NEXT
150 IF C>=3 THEN PRINT W$,
160 GOTO 50</langsyntaxhighlight>
{{out}}
<pre>eke elk keel keen keg
Line 734 ⟶ 779:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
// Read word from selected input
Line 785 ⟶ 830:
writef("%S*N", word)
endread()
$)</langsyntaxhighlight>
{{out}}
<pre>eke
Line 806 ⟶ 851:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 853 ⟶ 898:
fclose(in);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 879 ⟶ 924:
{{libheader|Boost}}
The puzzle parameters can be set with command line options. The default values are as per the task description.
<langsyntaxhighlight lang="cpp">#include <array>
#include <iostream>
#include <fstream>
Line 1,073 ⟶ 1,118:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 1,088 ⟶ 1,133:
claremont with central letter a
spearmint with central letter a
</pre>
 
===Without external libraries===
<syntaxhighlight lang="c++">
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
 
int main() {
const std::string word_wheel_letters = "ndeokgelw";
const std::string middle_letter = word_wheel_letters.substr(4, 1);
 
std::vector<std::string> words;
std::fstream file_stream;
file_stream.open("../unixdict.txt");
std::string word;
while ( file_stream >> word ) {
words.emplace_back(word);
}
 
std::vector<std::string> correct_words;
for ( const std::string& word : words ) {
if ( 3 <= word.length() && word.length() <= 9 &&
word.find(middle_letter) != std::string::npos &&
word.find_first_not_of(word_wheel_letters) == std::string::npos ) {
 
correct_words.emplace_back(word);
}
}
 
for ( const std::string& correct_word : correct_words ) {
std::cout << correct_word << std::endl;
}
 
int32_t max_words_found = 0;
std::vector<std::string> best_words9;
std::vector<char> best_central_letters;
std::vector<std::string> words9;
for ( const std::string& word : words ) {
if ( word.length() == 9 ) {
words9.emplace_back(word);
}
}
 
for ( const std::string& word9 : words9 ) {
std::vector<char> distinct_letters(word9.begin(), word9.end());
std::sort(distinct_letters.begin(), distinct_letters.end());
distinct_letters.erase(std::unique(distinct_letters.begin(), distinct_letters.end()), distinct_letters.end());
 
for ( const char& letter : distinct_letters ) {
int32_t words_found = 0;
for ( const std::string& word : words ) {
if ( word.length() >= 3 && word.find(letter) != std::string::npos ) {
std::vector<char> letters = distinct_letters;
bool valid_word = true;
for ( const char& ch : word ) {
std::vector<char>::iterator iter = std::find(letters.begin(), letters.end(), ch);
int32_t index = ( iter == letters.end() ) ? -1 : std::distance(letters.begin(), iter);
if ( index == -1 ) {
valid_word = false;
break;
}
letters.erase(letters.begin() + index);
}
if ( valid_word ) {
words_found++;
}
}
}
 
if ( words_found > max_words_found ) {
max_words_found = words_found;
best_words9.clear();
best_words9.emplace_back(word9);
best_central_letters.clear();
best_central_letters.emplace_back(letter);
} else if ( words_found == max_words_found ) {
best_words9.emplace_back(word9);
best_central_letters.emplace_back(letter);
}
}
}
 
std::cout << "\n" << "Most words found = " << max_words_found << std::endl;
std::cout << "The nine letter words producing this total are:" << std::endl;
for ( uint64_t i = 0; i < best_words9.size(); ++i ) {
std::cout << best_words9[i] << " with central letter '" << best_central_letters[i] << "'" << std::endl;
}
}
</syntaxhighlight>
<pre>
eke
elk
keel
keen
keg
kellogg
ken
kennel
keno
knee
kneel
knell
knew
knoll
know
knowledge
known
kong
kowloon
leek
look
nook
onlook
week
weekend
wok
woke
 
Most words found = 215
The nine letter words producing this total are:
claremont with central letter 'a'
spearmint with central letter 'a'
</pre>
 
Line 1,094 ⟶ 1,265:
{{libheader| System.Classes}}
{{Trans|Wren}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Word_wheel;
 
Line 1,147 ⟶ 1,318:
end.
 
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Word Wheel: Nigel Galloway. May 25th., 2021
let fG k n g=g|>Seq.exists(fun(n,_)->n=k) && g|>Seq.forall(fun(k,g)->Map.containsKey k n && g<=n.[k])
let wW n g=let fG=fG(Seq.item 4 g)(g|>Seq.countBy id|>Map.ofSeq) in seq{use n=System.IO.File.OpenText(n) in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(fun n->2<(Seq.length n)&&(Seq.countBy id>>fG)n)
wW "unixdict.txt" "ndeokgelw"|>Seq.iter(printfn "%s")
</syntaxhighlight>
{{out}}
<pre>
eke
elk
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
week
wok
woke
</pre>
=={{header|Factor}}==
{{works with|Factor|0.99 2020-07-03}}
<langsyntaxhighlight lang="factor">USING: assocs io.encodings.ascii io.files kernel math
math.statistics prettyprint sequences sorting ;
 
Line 1,171 ⟶ 1,369:
[ words ] keepd [ can-make? ] curry filter ;
 
"ndeokgelw" "unixdict.txt" solve [ length ] sort-with .</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">
Line 1,194 ⟶ 1,392:
}
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "file.bi"
 
Line 1,266 ⟶ 1,465:
Sub show(g As String,file As String,byref matches as long,minsize as long,mustdo as string)
Redim As String s()
Var L=lcase(loadfile(file))
g=lcase(g)
string_split(L,Chr(10),s())
For m As Long=minsize To len(g)
Line 1,294 ⟶ 1,494:
print matches;" matches"
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,317 ⟶ 1,517:
Overall time taken 0.02187220007181168 seconds
17 matches
</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
 
include "NSLog.incl"
 
local fn CountCharacterInString( string as CFStringRef, character as CFStringRef ) as NSUInteger
end fn = len(string) - len( fn StringByReplacingOccurrencesOfString( string, character, @"" ) )
 
local fn IsLegal( wordStr as CFStringRef ) as BOOL
NSUInteger i, count = len( wordStr )
CFStringRef letters = @"ndeokgelw"
if count < 3 || fn StringContainsString( wordStr, @"k" ) == NO then exit fn = NO
for i = 0 to count - 1
if fn CountCharacterInString( letters, mid( wordStr, i, 1 ) ) < fn CountCharacterInString( wordStr, mid( wordStr, i, 1 ) )
exit fn = NO
end if
next
end fn = YES
 
local fn ArrayOfDictionaryWords as CFArrayRef
CFURLRef url = fn URLWithString( @"http://wiki.puzzlers.org/pub/wordlists/unixdict.txt" )
CFStringRef string = lcase( fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL ) )
CFArrayRef wordArr = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )
end fn = wordArr
 
void local fn FindWheelWords
CFArrayRef wordArr = fn ArrayOfDictionaryWords
CFStringRef wordStr
CFMutableStringRef mutStr = fn MutableStringNew
for wordStr in wordArr
if fn IsLegal( wordStr ) then MutableStringAppendFormat( mutStr, fn StringWithFormat( @"%@\n", wordStr ) )
next
NSLog( @"%@", mutStr )
end fn
 
fn FindWheelWords
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
eke
elk
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
week
wok
woke
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,434 ⟶ 1,698:
fmt.Println(mostWords9[i], "with central letter", string(mostLetters[i]))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,464 ⟶ 1,728:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (toLower)
import Data.List (sort)
import System.IO (readFile)
Line 1,482 ⟶ 1,746:
 
wheelFit :: String -> String -> Bool
wheelFit wheel word = go wheel (. sort word)
where
go _ [] = True
Line 1,497 ⟶ 1,761:
. gridWords ["NDE", "OKG", "ELW"]
. lines
)</langsyntaxhighlight>
{{Out}}
<pre>eke
Line 1,516 ⟶ 1,780:
wok
woke</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">require'stats'
wwhe=: {{
ref=. /:~each words=. cutLF tolower fread 'unixdict.txt'
y=.,y assert. 9=#y
ch0=. 4{y
chn=. (<<<4){y
r=. ''
for_i.2}.i.9 do.
target=. <"1 ~./:~"1 ch0,.(i comb 8){chn
;:inv r=. r,words #~ ref e. target
end.
}}</syntaxhighlight>
 
Task example:<syntaxhighlight lang="j"> wwhe'ndeokgelw'
eke elk keg ken wok keel keen keno knee knew know kong leek week woke kneel knowledge</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
 
public final class WordWheelExtended {
 
public static void main(String[] args) throws IOException {
String wordWheel = "N D E"
+ "O K G"
+ "E L W";
String url = "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt";
InputStream stream = URI.create(url).toURL().openStream();
BufferedReader reader = new BufferedReader( new InputStreamReader(stream) );
List<String> words = reader.lines().toList();
reader.close();
String allLetters = wordWheel.toLowerCase().replace(" ", "");
String middleLetter = allLetters.substring(4, 5);
Predicate<String> firstFilter = word -> word.contains(middleLetter) && 2 < word.length() && word.length() < 10;
Predicate<String> secondFilter = word -> word.chars().allMatch( ch -> allLetters.indexOf(ch) >= 0 );
Predicate<String> correctWords = firstFilter.and(secondFilter);
words.stream().filter(correctWords).forEach(System.out::println);
int maxWordsFound = 0;
List<String> bestWords9 = new ArrayList<String>();
List<Character> bestCentralLetters = new ArrayList<Character>();
List<String> words9 = words.stream().filter( word -> word.length() == 9 ).toList();
 
for ( String word9 : words9 ) {
List<Character> distinctLetters = word9.chars().mapToObj( i -> (char) i ).distinct().toList();
for ( char letter : distinctLetters ) {
int wordsFound = 0;
for ( String word : words ) {
if ( word.length() >= 3 && word.indexOf(letter) >= 0 ) {
List<Character> letters = new ArrayList<Character>(distinctLetters);
boolean validWord = true;
for ( char ch : word.toCharArray() ) {
final int index = letters.indexOf(ch);
if ( index == -1 ) {
validWord = false;
break;
}
letters.remove(index);
}
if ( validWord ) {
wordsFound += 1;
}
}
}
if ( wordsFound > maxWordsFound ) {
maxWordsFound = wordsFound;
bestWords9.clear();
bestWords9.add(word9);
bestCentralLetters.clear();
bestCentralLetters.add(letter);
} else if ( wordsFound == maxWordsFound ) {
bestWords9.add(word9);
bestCentralLetters.add(letter);
}
}
}
System.out.println(System.lineSeparator() + "Most words found = " + maxWordsFound);
System.out.println("The nine letter words producing this total are:");
for ( int i = 0; i < bestWords9.size(); i++ ) {
System.out.println(bestWords9.get(i) + " with central letter '" + bestCentralLetters.get(i) + "'");
}
}
 
}
</syntaxhighlight>
<pre>
eke
elk
keel
keen
keg
kellogg
ken
kennel
keno
knee
kneel
knell
knew
knoll
know
knowledge
known
kong
kowloon
leek
look
nook
onlook
week
weekend
wok
woke
 
Most words found = 215
The nine letter words producing this total are:
claremont with central letter 'a'
spearmint with central letter 'a'
</pre>
 
=={{header|JavaScript}}==
A version using local access to the dictionary, through the macOS JavaScript for Automation API.
{{Works with|JXA}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'"use strict'";
 
// ------------------- WORD WHEEL --------------------
// main :: IO ()
const main = () =>
console.log(unlines(
gridWords(['NDE', 'OKG', 'ELW'])(
lines(readFile('unixdict.txt'))
)
));
 
// gridWords :: [String] -> [String] -> [String]
Line 1,535 ⟶ 1,927:
lexemes => {
const
wheel = sort(toLower(concat(grid.join(""))),
wSet = new Set(wheel),
mid = wheel[4];
 
return lexemes.filter(w => {
const cs = chars([...w)];
 
return 2 < cs.length && cs.every(
c => wSet.has(c)
) && elemcs.some(x => mid)(cs === x) && (
wheelFit(wheel, cs)
);
});
};
 
 
// wheelFit :: [Char] -> [Char] -> Bool
Line 1,558 ⟶ 1,953:
go(ws.slice(1), cs.slice(1))
) : go(ws.slice(1), cs);
 
return go(wheel, sort(word));
};
 
// ----------------- GENERIC FUNCTIONS -----------------
 
// ---------------------- TEST -----------------------
// chars :: String -> [Char]
const// charsmain =:: sIO =>()
const main = s.split(''); =>
gridWords(["NDE", "OKG", "ELW"])(
lines(readFile("unixdict.txt"))
)
.join("\n");
 
// concat :: [[a]] -> [a]
// concat :: [String] -> String
const concat = xs => (
ys => 0 < ys.length ? (
ys.every(Array.isArray) ? (
[]
) : ''
).concat(...ys) : ys
)(list(xs));
 
// ---------------- GENERIC FUNCTIONS ----------------
// elem :: Eq a => a -> [a] -> Bool
const elem = x =>
// True if xs contains an instance of x.
xs => xs.some(y => x === y);
 
// lines :: String -> [String]
const lines = s =>
// A list of strings derived from a single string
// newline-which is delimited stringby \n or by \r\n or \r.
0 < Boolean(s.length) ? (
s.split(/[\r\n]|\n|\r/u)
) : [];
 
// list :: StringOrArrayLike b => b -> [a]
const list = xs =>
// xs itself, if it is an Array,
// or an Array derived from xs.
Array.isArray(xs) ? (
xs
) : Array.from(xs || []);
 
// readFile :: FilePath -> IO String
const readFile = fp => {
// The contents of a text file at the
// pathgiven file fppath.
const
e = $(),
Line 1,610 ⟶ 1,990:
e
);
 
return ObjC.unwrap(
ns.isNil() ? (
Line 1,616 ⟶ 1,997:
);
};
 
 
// sort :: Ord a => [a] -> [a]
const sort = xs => list(xs).slice()
Array.sortfrom((a, bxs) => a < b ? -1 : .sort(a > b ? 1 : 0));
 
 
// toLower :: String -> String
Line 1,626 ⟶ 2,009:
s.toLocaleLowerCase();
 
// unlines :: [String] -> String
const unlines = xs =>
// A single string formed by the intercalation
// of a list of strings with the newline character.
xs.join('\n');
 
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>eke
Line 1,653 ⟶ 2,031:
wok
woke</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq''' provided `keys_unsorted` is replaced `by keys`
<syntaxhighlight lang=jq>
# remove words with fewer than 3 or more than 9 letters
def words: inputs | select(length | . > 2 and . < 10);
 
# The central letter in `puzzle` should be the central letter of the word wheel
def solve(puzzle):
def chars: explode[] | [.] | implode;
def profile(s): reduce s as $c (null; .[$c] += 1);
profile(puzzle[]) as $profile
| def ok($prof): all($prof|keys_unsorted[]; . as $k | $prof[$k] <= $profile[$k]);
(puzzle | .[ (length - 1) / 2]) as $central
| words
| select(index($central) and ok( profile(chars) )) ;
 
"The solutions to the puzzle are as follows:",
solve( ["d", "e", "e", "g", "k", "l", "n", "o", "w"] )
</syntaxhighlight>
'''Invocation''': < unixdict.txt jq -Rnr -f word-wheel.jq
{{output}}
<pre>
The solutions to the puzzle are as follows:
eke
elk
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
week
wok
woke
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
const tfile = download("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
Line 1,676 ⟶ 2,097:
 
println(wordwheel("ndeokgelw", "k"))
</langsyntaxhighlight>{{out}}
<pre>
["ken", "keg", "eke", "elk", "wok", "keno", "knee", "keen", "knew", "kong", "know", "woke", "keel", "leek", "week", "kneel", "knowledge"]
</pre>
===Faster but less general version===
<langsyntaxhighlight lang="julia">const tfile = download("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
const wordarraylist = [[string(c) for c in w] for w in split(read(tfile, String), r"\s+")]
 
Line 1,692 ⟶ 2,113:
 
println(wordwheel2("ndeokgelw", "k"))
</langsyntaxhighlight>{{out}}
<pre>
["eke", "elk", "keel", "keen", "keg", "ken", "keno", "knee", "kneel", "knew", "know", "knowledge", "kong", "leek", "week", "wok", "woke"]
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">LetterCounter = {
new = function(self, word)
local t = { word=word, letters={} }
Line 1,720 ⟶ 2,142:
print(word)
end
end</langsyntaxhighlight>
{{out}}
<pre>eke
Line 1,739 ⟶ 2,161:
wok
woke</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[possible]
possible[letters_List][word_String] := Module[{c1, c2, m},
c1 = Counts[Characters@word];
c2 = Counts[letters];
m = Merge[{c1, c2}, Identity];
Length[Select[Select[m, Length /* GreaterThan[1]], Apply[Greater]]] == 0
]
chars = Characters@"ndeokgelw";
words = Import["http://wiki.puzzlers.org/pub/wordlists/unixdict.txt", "String"];
words = StringSplit[ToLowerCase[words], "\n"];
words //= Select[StringLength /* GreaterEqualThan[3]];
words //= Select[StringContainsQ["k"]];
words //= Select[StringMatchQ[Repeated[Alternatives @@ chars]]];
words //= Select[possible[chars]];
words</syntaxhighlight>
{{out}}
<pre>{eke,elk,keel,keen,keg,ken,keno,knee,kneel,knew,know,knowledge,kong,leek,week,wok,woke}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar, tables
 
const Grid = """N D E
Line 1,763 ⟶ 2,204:
if count > gridCount[ch]:
break checkWord
echo word</langsyntaxhighlight>
 
{{out}}
Line 1,783 ⟶ 2,224:
wok
woke</pre>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
<syntaxhighlight lang="pascal">
program WordWheel;
 
{$mode objfpc}{$H+}
 
uses
SysUtils;
 
const
WheelSize = 9;
MinLength = 3;
WordListFN = 'unixdict.txt';
 
procedure search(Wheel : string);
var
Allowed, Required, Available, w : string;
Len, i, p : integer;
WordFile : TextFile;
Match : boolean;
begin
AssignFile(WordFile, WordListFN);
try
Reset(WordFile);
except
writeln('Could not open dictionary file: ' + WordListFN);
exit;
end;
Allowed := LowerCase(Wheel);
Required := copy(Allowed, 5, 1); { central letter is required }
while not eof(WordFile) do
begin
readln(WordFile, w);
Len := length(w);
if (Len < MinLength) or (Len > WheelSize) then continue;
if pos(Required, w) = 0 then continue;
Available := Allowed;
Match := True;
for i := 1 to Len do
begin
p := pos(w[i], Available);
if p > 0 then
{ prevent re-use of letter }
delete(Available, p, 1)
else
begin
Match := False;
break;
end;
end;
if Match then
writeln(w);
end;
CloseFile(WordFile);
end;
 
{ exercise the procedure }
begin
search('NDE' + 'OKG' + 'ELW');
end.
</syntaxhighlight>
{{out}}
<pre>
eke
elk
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
week
wok
woke
</pre>
 
=={{header|Perl}}==
UPDATED: this version builds a single regex that will select all valid words
straight from the file string.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Word_wheel
Line 1,807 ⟶ 2,331:
my @words = $file =~ /$valid/g;
 
print @words . " words for\n$_\n@words\n" =~ s/.{60}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,820 ⟶ 2,344:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonlinephixonline)-->
<span style="color: #7060A8008080;">requireswith</span><span style="color: #0000FF;">(</span><span style="color: #008000008080;">"0.8.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (fixed some glitches in join_by())javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (fixed another glitch in unique())</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">wheel</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ndeokgelw"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">musthave</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">wheel</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unix_dict</span><span style="color: #0000FF;">{}(),</span>
<span style="color: #000000;">word9</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> <span style="color: #000080;font-style:italic;">-- (for the optional extra part)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fnfound</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8000000;">open</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join_path</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"demo"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"unixdict.txt"</span><span style="color: #0000FF;">}),</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">)0</span>
<span style="color: #008080;">iffor</span> <span style="color: #000000;">fni</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">thento</span> <span style="color: #7060A8;">crashlength</span><span style="color: #0000FF;">(</span><span style="color: #008000000000;">"unixdict.txt not found"words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">ifdo</span>
<span style="color: #008080004080;">whilestring</span> <span style="color: #000000;">1word</span> <span style="color: #0080800000FF;">=</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">do])</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- eof</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">lw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">lw</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span>
Line 1,844 ⟶ 2,367:
<span style="color: #000000;">lw</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">lw</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">found</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">word</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">whilefor</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">jbw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">found</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n "</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">jbw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join_byprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words1</span><span style="color: #0000FF;">,</span> <span style="color: #000000008000;">1</span><span style="color:The #0000FF;">,</span><spanfollowing style="color%d words were found:\n #000000;%s\n">9</span><span style="color: #0000FF;">,{</span><span style="color: #008000000000;">" "found</span><span style="color: #0000FF;">,</span><span style="color: #008000000000;">"\n "jbw</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"The following %d words were found:\n %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jbw</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- optional extra</span>
<span style="color: #004080008080;">integerif</span> <span style="color: #0000007060A8;">mostFoundplatform</span> <span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #000000008080;">0then</span> <span style="color: #000080;font-style:italic;">-- (works but no progress/blank screen for 2min 20s)
-- (the "working" won't show, even w/o the JS check)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">mostWheels</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">mustHavesmostFound</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF000000;">{}0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF004080;">=sequence</span> <span style="color: #000000;">1mostWheels</span> <span style="color: #0080800000FF;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word9</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do{},</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">try_wheel</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">word9mustHaves</span> <span style="color: #0000FF;">[=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]{}</span>
<span style="color: #008080;">iffor</span> <span style="color: #7060A8000000;">lengthi</span><span style="color: #0000FF;">(=</span><span style="color: #000000;">try_wheel1</span> <span style="color: #0000FF008080;">)to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9word9</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">thendo</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">musthavestry_wheel</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8000000;">uniqueword9</span><span style="color: #0000FF;">([</span><span style="color: #000000;">try_wheeli</span><span style="color: #0000FF;">)]</span>
<span style="color: #008080;">forif</span> <span style="color: #0000007060A8;">jlength</span><span style="color: #0000FF;">=(</span><span style="color: #000000;">1try_wheel</span> <span style="color: #0080800000FF;">to</span> <span style)="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">musthaves</span><span style="color: #0000FF;">)9</span> <span style="color: #008080;">dothen</span>
<span style="color: #004080;">integerstring</span> <span style="color: #000000;">foundmusthaves</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0try_wheel</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">kj</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word9musthaves</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">wordfound</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">word9</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]0</span>
<span style="color: #008080;">iffor</span> <span style="color: #7060A8000000;">findk</span><span style="color: #0000FF;">(=</span><span style="color: #000000;">musthaves1</span> <span style="color: #0000FF008080;">[to</span> <span style="color: #0000007060A8;">jlength</span><span style="color: #0000FF;">],(</span><span style="color: #000000;">wordword9</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">thendo</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">restword</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">try_wheelword9</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
<span style="color: #004080008080;">boolif</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">okmusthaves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #004600008080;">truethen</span>
<span style="color: #008080;">for</span> <span style="color: #000000004080;">cstring</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1rest</span> <span style="color: #0080800000FF;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">dotry_wheel</span>
<span style="color: #004080;">integerbool</span> <span style="color: #000000;">ixok</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8004600;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">],</span><span style="color: #000000;">rest</span><span style="color: #0000FF;">)true</span>
<span style="color: #008080;">iffor</span> <span style="color: #000000;">ixc</span><span style="color: #0000FF;">=</span><span style="color: #000000;">01</span> <span style="color: #008080;">thento</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">okix</span> <span style="color: #0000FF;">=</span> <span style="color: #0046007060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">],</span><span style="color: #000000;">rest</span><span style="color: #0000FF;">false)</span>
<span style="color: #008080;">exitif</span> <span style="color: #000000;">ix</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080000000;">endok</span> <span style="color: #0000FF;">=</span> <span style="color: #008080004600;">iffalse</span>
<span style="color: #000000;">rest</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ix</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000008080;">'\0'exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">forif</span>
<span style="color: #000000;">foundrest</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ix</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000008000;">ok'\0'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">ok</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (wouldn't show up anyway)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"working (%s)\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">try_wheel</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">found</span><span style="color: #0000FF;">></span><span style="color: #000000;">mostFound</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">mostFound</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">found</span>
<span style="color: #000000;">mostWheels</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">try_wheel</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">mustHaves</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">musthaves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">found</span><span style="color: #0000FF;">==</span><span style="color: #000000;">mostFound</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">mostWheels</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mostWheels</span><span style="color: #0000FF;">,</span><span style="color: #000000;">try_wheel</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mustHaves</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mustHaves</span><span style="color: #0000FF;">,</span><span style="color: #000000;">musthaves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8008080;">printfend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"working (%s)\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">try_wheel</span><span style="color: #0000FF008080;">})if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">found</span><span style="color: #0000FF;">></span><span style="color: #000000;">mostFoundend</span> <span style="color: #008080;">thenfor</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Most words found = %d\n"</span><span style="color: #0000000000FF;">mostFound,</span> <span style="color: #0000FF000000;">=mostFound</span> <span style="color: #0000000000FF;">found)</span>
<span style="color: #0000007060A8;">mostWheelsprintf</span> <span style="color: #0000FF;">=(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">{,</span><span style="color: #000000008000;">try_wheel"Nine letter words producing this total:\n"</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">mustHavesi</span> <span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #0000FF008080;">{to</span> <span style="color: #0000007060A8;">musthaveslength</span><span style="color: #0000FF;">[(</span><span style="color: #000000;">jmostWheels</span><span style="color: #0000FF;">]})</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #008080000000;">elsif1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s with central letter '%c'\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">foundmostWheels</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">mostFoundmustHaves</span><span style="color: #0000FF;">[</span><span style="color: #008080000000;">i</span><span style="color: #0000FF;">then]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">mostWheels</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mostWheels</span><span style="color: #0000FF;">,</span><span style="color: #000000;">try_wheel</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">mustHaves</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mustHaves</span><span style="color: #0000FF;">,</span><span style="color: #000000;">musthaves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<!--</syntaxhighlight>-->
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Most words found = %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mostFound</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Nine letter words producing this total:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mostWheels</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s with central letter '%c'\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mostWheels</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">mustHaves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
{{out}}
<small>(Only the first three lines are shown under pwa/p2js)</small>
<pre>
The following 17 words were found:
Line 1,906 ⟶ 2,437:
claremont with central letter 'a'
spearmint with central letter 'a'
</pre>
 
=={{header|Picat}}==
<syntaxhighlight 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.</syntaxhighlight>
 
{{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)''':
<syntaxhighlight 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).</syntaxhighlight>
 
{{out}}
<pre>
maxReLen = 215
maxWord = [[word = claremont,char = a],[word = spearmint,char = a]]
</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.b check_word(word$)
Shared letters$
If Len(word$)<3 Or FindString(word$,"k")<1
Line 1,941 ⟶ 2,547:
Input()
EndIf
End</langsyntaxhighlight>
{{out}}
<pre>eke
Line 1,964 ⟶ 2,570:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import urllib.request
from collections import Counter
 
Line 1,990 ⟶ 2,596:
chars = ''.join(GRID.strip().lower().split())
found = solve(chars, dictionary=getwords())
print('\n'.join(found))</langsyntaxhighlight>
 
{{out}}
Line 2,013 ⟶ 2,619:
 
Or, using a local copy of the dictionary, and a recursive test of wheel fit:
<langsyntaxhighlight lang="python">'''Word wheel'''
 
from os.path import expanduser
Line 2,082 ⟶ 2,688:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>eke
Line 2,101 ⟶ 2,707:
wok
woke</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ over find swap found ] is has ( $ c --> b )
 
[ over find
split 1 split
swap drop join ] is remove ( $ c --> $ )
 
$ "rosetta/unixdict.txt" sharefile
drop nest$
[] swap
witheach
[ dup size 3 < iff drop done
dup size 9 > iff drop done
dup char k has not iff drop done
dup $ "ndeokgelw"
witheach remove
$ "" != iff drop done
nested join ]
30 wrap$</syntaxhighlight>
 
{{out}}
 
<pre>eke elk keel keen keg ken keno knee
kneel knew know knowledge kong leek week
wok woke
</pre>
 
=={{header|q}}==
<langsyntaxhighlight lang="q">ce:count each
lc:ce group@ / letter count
dict:"\n"vs .Q.hg "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"
Line 2,111 ⟶ 2,745:
solve:{[grid;dict]
i:where(grid 4)in'dict;
dict i where all each 0<=(lc grid)-/:lc each dict i }[;d39]</langsyntaxhighlight>
<langsyntaxhighlight lang="q">q)`$solve "ndeokglew"
`eke`elk`keel`keen`keg`ken`keno`knee`kneel`knew`know`knowledge`kong`leek`week`wok`woke</langsyntaxhighlight>
A naive solution to the second question is simple
<langsyntaxhighlight lang="q">bust:{[dict]
grids:distinct raze(til 9)rotate\:/:dict where(ce dict)=9;
wc:(count solve@)each grids;
grids where wc=max wc }</langsyntaxhighlight>
but inefficient. Better:
<langsyntaxhighlight lang="q">best:{[dict]
dlc:lc each dict; / letter counts of dictionary words
ig:where(ce dict)=9; / find grids (9-letter words)
Line 2,128 ⟶ 2,762:
ml:4 rotate'dict ig; / mid letters for each grid
wc:ce raze igw inter/:'iaz ml; / word counts for grids
distinct grids where wc=max wc } / grids with most words</langsyntaxhighlight>
<langsyntaxhighlight lang="q">q)show w:best d39
"ntclaremo"
"tspearmin"
 
q)ce solve each w
215 215</langsyntaxhighlight>
Full discussion at [https://code.kx.com/q/learn/pb/word-wheel/ code.kx.com]
 
Line 2,145 ⟶ 2,779:
Using [https://modules.raku.org/search/?q=Terminal%3A%3ABoxer Terminal::Boxer] from the Raku ecosystem.
 
<syntaxhighlight lang="raku" perl6line>use Terminal::Boxer;
 
my %*SUB-MAIN-OPTS = :named-anywhere;
Line 2,167 ⟶ 2,801:
say "{sum %words.values».elems} words found";
 
printf "%d letters: %s\n", .key, .value.sort.join(', ') for %words.sort;</langsyntaxhighlight>
 
{{out}}
;Using defaults
 
<syntaxhighlight lang="text">raku word-wheel.raku</langsyntaxhighlight>
<pre>Using ./unixdict.txt, minimum 3 letters.
╭───┬───┬───╮
Line 2,190 ⟶ 2,824:
Using the much larger dictionary '''words.txt''' file from '''https://github.com/dwyl/english-words'''
 
<syntaxhighlight lang="text">raku word-wheel.raku --dict=./words.txt</langsyntaxhighlight>
 
<pre>Using ./words.txt, minimum 3 letters.
Line 2,240 ⟶ 2,874:
Additional information is also provided concerning how many words have been skipped due to
the various filters.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds (dictionary) words which can be found in a specified word wheel (grid).*/
parse arg grid minL iFID . /*obtain optional arguments from the CL*/
if grid==''|grid=="," then grid= 'ndeokgelw' /*Not specified? Then use the default.*/
Line 2,300 ⟶ 2,934:
do n=1 for length(aa); p= pos( substr(aa,n,1), gg); if p==0 then return 1
gg= overlay(., gg, p) /*"rub out" the found character in grid*/
end /*n*/; return 0 /*signify that the AA passed the test*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,371 ⟶ 3,005:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">wheel = "ndeokgelw"
middle, wheel_size = wheel[4], wheel.size
 
Line 2,383 ⟶ 3,017:
 
puts res
</syntaxhighlight>
</lang>
{{out}}
<pre>eke
Line 2,403 ⟶ 3,037:
woke
</pre>
 
=={{header|Transd}}==
<syntaxhighlight lang="scheme">#lang transd
 
MainModule: {
maxwLen: 9,
minwLen: 3,
dict: Vector<String>(),
subWords: Vector<String>(),
 
procGrid: (λ grid String() cent String() subs Bool()
(with cnt 0 (sort grid)
(for w in dict where (and (neq (index-of w cent) -1)
(match w "^[[:alpha:]]+$")) do
(if (is-subset grid (sort (cp w))) (+= cnt 1)
(if subs (append subWords w))
)
)
(ret cnt)
)),
 
_start: (λ locals: res 0 maxRes 0
(with fs FileStream()
(open-r fs "/mnt/proj/res/unixdict.txt")
(for w in (read-lines fs)
where (within (size w) minwLen maxwLen) do
(append dict w))
)
 
(lout "Main part of task:\n")
(procGrid "ndeokgelw" "k" true)
(lout "Number of words: " (size subWords) ";\nword list: " subWords)
 
(lout "\n\nOptional part of task:\n")
(for w in dict where (eq (size w) maxwLen) do
(for centl in (split (unique (sort (cp w))) "") do
(if (>= (= res (procGrid (cp w) centl false)) maxRes) (= maxRes res)
(lout "New max. number: " maxRes ", word: " w ", central letter: " centl)
) ) ) )
}</syntaxhighlight>
{{out}}
<pre>
Main part of task:
 
Number of words: 17;
word list: ["eke", "elk", "keel", "keen", "keg", "ken", "keno", "knee", "kneel", "knew", "know", "knowledge", "kong", "leek", "week", "wok", "woke"]
 
 
Optional part of task:
 
New max. number: 100, word: abdominal, central letter: a
New max. number: 117, word: abernathy, central letter: a
New max. number: 119, word: abhorrent, central letter: r
New max. number: 121, word: absorbent, central letter: e
New max. number: 123, word: adsorbate, central letter: a
New max. number: 125, word: adventure, central letter: e
New max. number: 155, word: advertise, central letter: e
New max. number: 161, word: alongside, central letter: a
New max. number: 170, word: alongside, central letter: l
New max. number: 182, word: ancestral, central letter: a
New max. number: 182, word: arclength, central letter: a
New max. number: 185, word: beplaster, central letter: e
New max. number: 215, word: claremont, central letter: a
New max. number: 215, word: spearmint, central letter: a
</pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Const wheel="ndeokgelw"
 
Sub print(s):
On Error Resume Next
WScript.stdout.WriteLine (s)
If err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit
End Sub
 
Dim oDic
Set oDic = WScript.CreateObject("scripting.dictionary")
Dim cnt(127)
Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set ff=fso.OpenTextFile("unixdict.txt")
i=0
print "reading words of 3 or more letters"
While Not ff.AtEndOfStream
x=LCase(ff.ReadLine)
If Len(x)>=3 Then
If Not odic.exists(x) Then oDic.Add x,0
End If
Wend
print "remaining words: "& oDic.Count & vbcrlf
ff.Close
Set ff=Nothing
Set fso=Nothing
 
Set re=New RegExp
print "removing words with chars not in the wheel"
re.pattern="[^"& wheel &"]"
For Each w In oDic.Keys
If re.test(w) Then oDic.remove(w)
Next
print "remaining words: "& oDic.Count & vbcrlf
 
print "ensuring the mandatory letter "& Mid(wheel,5,1) & " is present"
re.Pattern=Mid(wheel,5,1)
For Each w In oDic.Keys
If Not re.test(w) Then oDic.remove(w)
Next
print "remaining words: "& oDic.Count & vbcrlf
 
print "checking number of chars"
 
Dim nDic
Set nDic = WScript.CreateObject("scripting.dictionary")
For i=1 To Len(wheel)
x=Mid(wheel,i,1)
If nDic.Exists(x) Then
a=nDic(x)
nDic(x)=Array(a(0)+1,0)
Else
nDic.add x,Array(1,0)
End If
Next
 
For Each w In oDic.Keys
For Each c In nDic.Keys
ndic(c)=Array(nDic(c)(0),0)
Next
For ii = 1 To len(w)
c=Mid(w,ii,1)
a=nDic(c)
If (a(0)=a(1)) Then
oDic.Remove(w):Exit For
End If
nDic(c)=Array(a(0),a(1)+1)
Next
Next
 
print "Remaining words "& oDic.count
For Each w In oDic.Keys
print w
Next
</syntaxhighlight>
{{out}}
<small>
<pre>
reading words of 3 or more letters
remaining words: 24945
 
removing words with chars not in the wheel
remaining words: 163
 
ensuring the mandatory letter k is present
remaining words: 27
 
checking number of chars
Remaining words 17
eke
elk
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
week
wok
woke
</pre>
</small>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
{{libheader|Wren-seq}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./sort" for Sort, Find
import "./seq" for Lst
 
var letters = ["d", "e", "e", "g", "k", "l", "n", "o","w"]
Line 2,478 ⟶ 3,288:
for (i in 0...mostWords9.count) {
System.print("%(mostWords9[i]) with central letter '%(mostLetters[i])'")
}</langsyntaxhighlight>
 
{{out}}
Line 2,505 ⟶ 3,315:
claremont with central letter 'a'
spearmint with central letter 'a'
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">string 0; \use zero-terminated strings
int I, Set, HasK, HasOther, HasDup, ECnt, Ch;
char Word(25);
def LF=$0A, CR=$0D, EOF=$1A;
[FSet(FOpen("unixdict.txt", 0), ^I);
OpenI(3);
repeat I:= 0; HasK:= false; HasOther:= false;
ECnt:= 0; Set:= 0; HasDup:= false;
loop [repeat Ch:= ChIn(3) until Ch # CR; \remove possible CR
if Ch=LF or Ch=EOF then quit;
Word(I):= Ch;
I:= I+1;
if Ch = ^k then HasK:= true;
case Ch of ^k,^n,^d,^e,^o,^g,^l,^w: [] \assume all lowercase
other HasOther:= true;
if Ch = ^e then ECnt:= ECnt+1
else [if Set & 1<<(Ch-^a) then HasDup:= true;
Set:= Set ! 1<<(Ch-^a);
];
];
Word(I):= 0; \terminate string
if I>=3 & HasK & ~HasOther & ~HasDup & ECnt<=2 then
[Text(0, Word); CrLf(0);
];
until Ch = EOF;
]</syntaxhighlight>
 
{{out}}
<pre>
eke
elk
keel
keen
keg
ken
keno
knee
kneel
knew
know
knowledge
kong
leek
week
wok
woke
</pre>
9,476

edits