Jump to content

Word wheel: Difference between revisions

→‎{{header|jq}}: shorter solution
(→‎{{header|jq}}: shorter solution)
Line 1,727:
=={{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
Line 1,735:
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) )) ;
first( foreach (chars, null) as $c (
{ letters: puzzle, ok: null };
if $c == null then .ok = true
else (.letters|index($c)) as $ix
| if $ix == null
then .ok = false
else .letters |= del(.[$ix])
end
end;
select(.ok != null).ok ) ) ) ];
 
"The solutions to the puzzle are as follows:",
solve( ["d", "e", "e", "g", "k", "l", "n", "o", "w"] )
| "The following \(length) words are the solutions to the puzzle:", .[]
</syntaxhighlight>
'''Invocation''': < unixdict.txt jq -Rnr -f word-wheel.jq
{{output}}
<pre>
The followingsolutions 17 words areto the solutionspuzzle toare theas puzzlefollows:
eke
elk
Line 1,774 ⟶ 1,767:
woke
</pre>
 
 
=={{header|Julia}}==
2,458

edits

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