Anagram generator: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(5 intermediate revisions by 4 users not shown)
Line 1:
{{draft task}}
 
There are already [[Anagrams|other]] [[Anagrams/Deranged_anagrams|tasks]] relating to ''finding'' '''existing''' [[wp:Anagram|anagrams]]. This one is about '''creating''' them.
Line 252:
 
# input: an array to be extended with an additional dictionary word.
# output: a stream of arrays with additional words selected from the characters in the string $letters.
# selected from the characters in the string $letters.
# The input array should be in alphabetical order; if soit is, so will the output array.
# so will the output array.
def extend($letters; $dict):
if $letters == "" then .
Line 303 ⟶ 305:
<pre>
# Invocation:
< unixdict.txt jq -ncR --arg word firefox --argjson anagram false --argjson min 2 -f anagram-generator.jq
"firefox" / 2
["ex","fir","of"]
["ex","for","if"]
Line 329 ⟶ 330:
["offer","xi"]
</pre>
 
 
=={{header|Julia}}==
Line 372:
<pre>
From 'Rosetta code':
cetera stood
coat oersted
coda rosette
code rosetta
coed rosetta
create stood
derate scoot
doctor tease
oersted coat
rosetta code
rosette coda
scoot derate
stood cetera
tease doctor
 
From 'Joe Biden':
done jibe
jibe done
node jibe
 
From 'wherrera':
herr ware
rare wehr
rear wehr
ware herr
wear herr
wehr rare
</pre>
 
=={{header|Nim}}==
{{trans|Julia}}
<syntaxhighlight lang="Nim">import std/[algorithm, sequtils, strutils, tables]
 
proc readWords(filename: string): seq[string] {.compileTime.} =
result = filename.staticRead().splitLines().map(toLowerAscii)
 
const UnixWords = readWords("unixdict.txt")
 
func findPhrases(anaString: string; choices: seq[string];
sizeLong = 4; nShortPermitted = 1): seq[string] =
var anaDict: CountTable[char]
for c in anaString.toLowerAscii:
if c in 'a'..'z':
anadict.inc(c)
var phrases: seq[string]
 
func addWord(remaining: CountTable[char]; phrase: string; numShort: int): string =
for word in UnixWords:
block Search:
if numShort < 1 and word.len < sizeLong:
break Search
if anyIt(word, remaining.getOrDefault(it) < word.count(it)):
break Search
var cdict = remaining
for c in word: cdict.inc(c, -1)
if allIt(cdict.values.toSeq, it == 0):
return strip(phrase & ' ' & word)
let newPhrase = addWord(cdict, phrase & ' ' & word, numshort - ord(word.len < sizeLong))
if newPhrase.len > 0:
phrases.add newPhrase
 
discard addWord(anaDict, "", nShortPermitted)
result = move(phrases)
 
for s in ["Rosetta code", "Joe Biden", "wherrera"]:
echo "From '$#':" % s
for phrase in findPhrases(s, UnixWords, 4, 0).sorted.deduplicate(true):
echo phrase
echo()
</syntaxhighlight>
 
{{out}}
<pre>From 'Rosetta code':
cetera stood
coat oersted
Line 575 ⟶ 648:
 
Alternatives formed by simply changing the order of the two words have been suppressed.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./str" for Str, Char
import "./perm" for Comb
Line 582 ⟶ 655:
 
var wordList = "unixdict.txt" // local copy
var words = File.read("unixdict.txt"wordList).split("\n").map { |w| w.trim() }
var wordMap = {}
for (word in words) {
9,482

edits