Anagram generator: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed literal string to predefined variable.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(2 intermediate revisions by 2 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 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
9,476

edits