Word wheel: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
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 665:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">letters := ["N", "D", "E", "O", "K", "G", "E", "L", "W"]
 
FileRead, wList, % A_Desktop "\unixdict.txt"
Line 687:
}
return oRes
}</langsyntaxhighlight>
{{out}}
<pre>eke
Line 710:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f WORD_WHEEL.AWK letters unixdict.txt
# the required letter must be first
Line 747:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 755:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT A-Z
20 DATA "ndeokgelw","unixdict.txt"
30 READ WH$, F$
Line 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 779:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
// Read word from selected input
Line 830:
writef("%S*N", word)
endread()
$)</langsyntaxhighlight>
{{out}}
<pre>eke
Line 851:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 898:
fclose(in);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 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,118:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 1,139:
{{libheader| System.Classes}}
{{Trans|Wren}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Word_wheel;
 
Line 1,192:
end.
 
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight 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>
</lang>
{{out}}
<pre>
Line 1,223:
=={{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,243:
[ words ] keepd [ can-make? ] curry filter ;
 
"ndeokgelw" "unixdict.txt" solve [ length ] sort-with .</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">
Line 1,268:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "file.bi"
 
Line 1,368:
print matches;" matches"
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,395:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,508:
fmt.Println(mostWords9[i], "with central letter", string(mostLetters[i]))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,538:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (toLower)
import Data.List (sort)
import System.IO (readFile)
Line 1,571:
. gridWords ["NDE", "OKG", "ELW"]
. lines
)</langsyntaxhighlight>
{{Out}}
<pre>eke
Line 1,592:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">require'stats'
wwhe=: {{
ref=. /:~each words=. cutLF tolower fread 'unixdict.txt'
Line 1,603:
;:inv r=. r,words #~ ref e. target
end.
}}</langsyntaxhighlight>
 
Task example:<langsyntaxhighlight Jlang="j"> wwhe'ndeokgelw'
eke elk keg ken wok keel keen keno knee knew know kong leek week woke kneel knowledge</langsyntaxhighlight>
 
=={{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";
 
Line 1,705:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>eke
Line 1,726:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
const tfile = download("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
Line 1,747:
 
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,763:
 
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,791:
print(word)
end
end</langsyntaxhighlight>
{{out}}
<pre>eke
Line 1,812:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[possible]
possible[letters_List][word_String] := Module[{c1, c2, m},
c1 = Counts[Characters@word];
Line 1,826:
words //= Select[StringMatchQ[Repeated[Alternatives @@ chars]]];
words //= Select[possible[chars]];
words</langsyntaxhighlight>
{{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,853:
if count > gridCount[ch]:
break checkWord
echo word</langsyntaxhighlight>
 
{{out}}
Line 1,876:
=={{header|Pascal}}==
{{works with|Free Pascal}}
<syntaxhighlight lang="pascal">
<lang Pascal>
program WordWheel;
 
Line 1,935:
search('NDE' + 'OKG' + 'ELW');
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,960:
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,980:
my @words = $file =~ /$valid/g;
 
print @words . " words for\n$_\n@words\n" =~ s/.{60}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,993:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">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>
Line 2,074:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(Only the first three lines are shown under pwa/p2js)</small>
Line 2,089:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">main =>
MinLen = 3,
MaxLen = 9,
Line 2,125:
foreach(E in L)
D.put(E,D.get(E,0)+1)
end.</langsyntaxhighlight>
 
{{out}}
Line 2,132:
 
'''Optimal word(s)''':
<langsyntaxhighlight Picatlang="picat">main =>
WordList = "unixdict.txt",
MinLen = 3,
Line 2,155:
end,
println(maxLResen=MaxResLen),
println(maxWord=MaxResWord).</langsyntaxhighlight>
 
{{out}}
Line 2,164:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.b check_word(word$)
Shared letters$
If Len(word$)<3 Or FindString(word$,"k")<1
Line 2,196:
Input()
EndIf
End</langsyntaxhighlight>
{{out}}
<pre>eke
Line 2,219:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import urllib.request
from collections import Counter
 
Line 2,245:
chars = ''.join(GRID.strip().lower().split())
found = solve(chars, dictionary=getwords())
print('\n'.join(found))</langsyntaxhighlight>
 
{{out}}
Line 2,268:
 
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,337:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>eke
Line 2,358:
 
=={{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,366:
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,383:
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,403:
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,425:
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,448:
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,498:
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,558:
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,629:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">wheel = "ndeokgelw"
middle, wheel_size = wheel[4], wheel.size
 
Line 2,641:
 
puts res
</syntaxhighlight>
</lang>
{{out}}
<pre>eke
Line 2,663:
 
=={{header|Transd}}==
<langsyntaxhighlight lang="scheme">#lang transd
 
MainModule: {
Line 2,700:
(lout "New max. number: " maxRes ", word: " w ", central letter: " centl)
) ) ) )
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,730:
{{libheader|Wren-sort}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/sort" for Sort, Find
import "/seq" for Lst
Line 2,801:
for (i in 0...mostWords9.count) {
System.print("%(mostWords9[i]) with central letter '%(mostLetters[i])'")
}</langsyntaxhighlight>
 
{{out}}
Line 2,831:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
int I, Set, HasK, HasOther, HasDup, ECnt, Ch;
char Word(25);
Line 2,856:
];
until Ch = EOF;
]</langsyntaxhighlight>
 
{{out}}
10,327

edits