Word wheel: Difference between revisions
Content added Content deleted
(→{{header|jq}}: shorter solution) |
|||
Line 1,727: | Line 1,727: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq}} |
{{works with|jq}} |
||
'''Also works with gojq, the Go implementation of jq, and with fq''' |
'''Also works with gojq, the Go implementation of jq, and with fq''' provided `keys_unsorted` is replaced `by keys` |
||
<syntaxhighlight lang=jq> |
<syntaxhighlight lang=jq> |
||
# remove words with fewer than 3 or more than 9 letters |
# remove words with fewer than 3 or more than 9 letters |
||
Line 1,735: | Line 1,735: | ||
def solve(puzzle): |
def solve(puzzle): |
||
def chars: explode[] | [.] | implode; |
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 |
(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"]) |
solve( ["d", "e", "e", "g", "k", "l", "n", "o", "w"] ) |
||
| "The following \(length) words are the solutions to the puzzle:", .[] |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
'''Invocation''': < unixdict.txt jq -Rnr -f word-wheel.jq |
'''Invocation''': < unixdict.txt jq -Rnr -f word-wheel.jq |
||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
The |
The solutions to the puzzle are as follows: |
||
eke |
eke |
||
elk |
elk |
||
Line 1,774: | Line 1,767: | ||
woke |
woke |
||
</pre> |
</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |