Word wheel: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(18 intermediate revisions by 6 users not shown)
Line 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,391 ⟶ 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>
 
Line 1,607 ⟶ 1,797:
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}}==
Line 1,724 ⟶ 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}}==
Line 1,767 ⟶ 2,117:
["eke", "elk", "keel", "keen", "keg", "ken", "keno", "knee", "kneel", "knew", "know", "knowledge", "kong", "leek", "week", "wok", "woke"]
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">LetterCounter = {
Line 2,356 ⟶ 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}}==
Line 2,670 ⟶ 3,049:
procGrid: (λ grid String() cent String() subs Bool()
(with cnt 0 (sort grid)
(for w in dict where (and (neq (findindex-of w cent) -1)
(match w "^[[:alpha:]]+$")) do
(if (is-subset grid (sort (cp w))) (+= cnt 1)
Line 2,723 ⟶ 3,102:
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}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./sort" for Sort, Find
import "./seq" for Lst
 
var letters = ["d", "e", "e", "g", "k", "l", "n", "o","w"]
9,476

edits