Anagrams: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 467: | Line 467: | ||
caret|carte|cater|crate|trace |
caret|carte|cater|crate|trace |
||
</pre> |
</pre> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
Line 1,128: | Line 1,126: | ||
alger glare lager large regal |
alger glare lager large regal |
||
elan lane lean lena neal |
elan lane lean lena neal |
||
evil levi live veil vile |
|||
</pre> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Net; |
|||
using System.Text.RegularExpressions; |
|||
namespace Anagram |
|||
{ |
|||
class Program |
|||
{ |
|||
const string DICO_URL = "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"; |
|||
static void Main( string[] args ) |
|||
{ |
|||
WebRequest request = WebRequest.Create(DICO_URL); |
|||
string[] words; |
|||
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), true)) { |
|||
words = Regex.Split(sr.ReadToEnd(), @"\r?\n"); |
|||
} |
|||
var groups = from string w in words |
|||
group w by string.Concat(w.OrderBy(x => x)) into c |
|||
group c by c.Count() into d |
|||
orderby d.Key descending |
|||
select d; |
|||
foreach (var c in groups.First()) { |
|||
Console.WriteLine(string.Join(" ", c)); |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
abel able bale bela elba |
|||
alger glare lager large regal |
|||
angel angle galen glean lange |
|||
caret carte cater crate trace |
|||
elan lane lean lena neal |
|||
evil levi live veil vile |
evil levi live veil vile |
||
</pre> |
</pre> |
||
Line 1,174: | Line 1,213: | ||
elan, lane, lean, lena, neal, |
elan, lane, lean, lena, neal, |
||
evil, levi, live, veil, vile, |
evil, levi, live, veil, vile, |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Net; |
|||
using System.Text.RegularExpressions; |
|||
namespace Anagram |
|||
{ |
|||
class Program |
|||
{ |
|||
const string DICO_URL = "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"; |
|||
static void Main( string[] args ) |
|||
{ |
|||
WebRequest request = WebRequest.Create(DICO_URL); |
|||
string[] words; |
|||
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), true)) { |
|||
words = Regex.Split(sr.ReadToEnd(), @"\r?\n"); |
|||
} |
|||
var groups = from string w in words |
|||
group w by string.Concat(w.OrderBy(x => x)) into c |
|||
group c by c.Count() into d |
|||
orderby d.Key descending |
|||
select d; |
|||
foreach (var c in groups.First()) { |
|||
Console.WriteLine(string.Join(" ", c)); |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
abel able bale bela elba |
|||
alger glare lager large regal |
|||
angel angle galen glean lange |
|||
caret carte cater crate trace |
|||
elan lane lean lena neal |
|||
evil levi live veil vile |
|||
</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 2,296: | Line 2,294: | ||
seq ["elan"; "lane"; "lean"; "lena"; "neal"]; |
seq ["elan"; "lane"; "lean"; "lena"; "neal"]; |
||
seq ["evil"; "levi"; "live"; "veil"; "vile"]]</lang> |
seq ["evil"; "levi"; "live"; "veil"; "vile"]]</lang> |
||
=={{header|FBSL}}== |
|||
'''A little bit of cheating: literatim re-implementation of C solution in FBSL's Dynamic C layer.''' |
|||
<lang C>#APPTYPE CONSOLE |
|||
DIM gtc = GetTickCount() |
|||
Anagram() |
|||
PRINT "Done in ", (GetTickCount() - gtc) / 1000, " seconds" |
|||
PAUSE |
|||
DYNC Anagram() |
|||
#include <windows.h> |
|||
#include <stdio.h> |
|||
char* sortedWord(const char* word, char* wbuf) |
|||
{ |
|||
char* p1, *p2, *endwrd; |
|||
char t; |
|||
int swaps; |
|||
strcpy(wbuf, word); |
|||
endwrd = wbuf + strlen(wbuf); |
|||
do { |
|||
swaps = 0; |
|||
p1 = wbuf; p2 = endwrd - 1; |
|||
while (p1 < p2) { |
|||
if (*p2 >* p1) { |
|||
t = *p2; *p2 = *p1; *p1 = t; |
|||
swaps = 1; |
|||
} |
|||
p1++; p2--; |
|||
} |
|||
p1 = wbuf; p2 = p1 + 1; |
|||
while (p2 < endwrd) { |
|||
if (*p2 >* p1) { |
|||
t = *p2; *p2 = *p1; *p1 = t; |
|||
swaps = 1; |
|||
} |
|||
p1++; p2++; |
|||
} |
|||
} while (swaps); |
|||
return wbuf; |
|||
} |
|||
static short cxmap[] = { |
|||
0x06, 0x1f, 0x4d, 0x0c, 0x5c, 0x28, 0x5d, 0x0e, 0x09, 0x33, 0x31, 0x56, |
|||
0x52, 0x19, 0x29, 0x53, 0x32, 0x48, 0x35, 0x55, 0x5e, 0x14, 0x27, 0x24, |
|||
0x02, 0x3e, 0x18, 0x4a, 0x3f, 0x4c, 0x45, 0x30, 0x08, 0x2c, 0x1a, 0x03, |
|||
0x0b, 0x0d, 0x4f, 0x07, 0x20, 0x1d, 0x51, 0x3b, 0x11, 0x58, 0x00, 0x49, |
|||
0x15, 0x2d, 0x41, 0x17, 0x5f, 0x39, 0x16, 0x42, 0x37, 0x22, 0x1c, 0x0f, |
|||
0x43, 0x5b, 0x46, 0x4b, 0x0a, 0x26, 0x2e, 0x40, 0x12, 0x21, 0x3c, 0x36, |
|||
0x38, 0x1e, 0x01, 0x1b, 0x05, 0x4e, 0x44, 0x3d, 0x04, 0x10, 0x5a, 0x2a, |
|||
0x23, 0x34, 0x25, 0x2f, 0x2b, 0x50, 0x3a, 0x54, 0x47, 0x59, 0x13, 0x57, |
|||
}; |
|||
#define CXMAP_SIZE (sizeof(cxmap) / sizeof(short)) |
|||
int Str_Hash(const char* key, int ix_max) |
|||
{ |
|||
const char* cp; |
|||
short mash; |
|||
int hash = 33501551; |
|||
for (cp = key; *cp; cp++) { |
|||
mash = cxmap[*cp % CXMAP_SIZE]; |
|||
hash = (hash >>4) ^ 0x5C5CF5C ^ ((hash << 1) + (mash << 5)); |
|||
hash &= 0x3FFFFFFF; |
|||
} |
|||
return hash % ix_max; |
|||
} |
|||
typedef struct sDictWord* DictWord; |
|||
struct sDictWord { |
|||
const char* word; |
|||
DictWord next; |
|||
}; |
|||
typedef struct sHashEntry* HashEntry; |
|||
struct sHashEntry { |
|||
const char* key; |
|||
HashEntry next; |
|||
DictWord words; |
|||
HashEntry link; |
|||
short wordCount; |
|||
}; |
|||
#define HT_SIZE 8192 |
|||
HashEntry hashTable[HT_SIZE]; |
|||
HashEntry mostPerms = NULL; |
|||
int buildAnagrams(FILE* fin) |
|||
{ |
|||
char buffer[40]; |
|||
char bufr2[40]; |
|||
char* hkey; |
|||
int hix; |
|||
HashEntry he, *hep; |
|||
DictWord we; |
|||
int maxPC = 2; |
|||
int numWords = 0; |
|||
while (fgets(buffer, 40, fin)) { |
|||
for (hkey = buffer; *hkey && (*hkey != '\n'); hkey++); |
|||
*hkey = 0; |
|||
hkey = sortedWord(buffer, bufr2); |
|||
hix = Str_Hash(hkey, HT_SIZE); |
|||
he = hashTable[hix]; hep = &hashTable[hix]; |
|||
while (he && strcmp(he->key, hkey)) { |
|||
hep = &he->next; |
|||
he = he->next; |
|||
} |
|||
if (! he) { |
|||
he = (HashEntry)malloc(sizeof(struct sHashEntry)); |
|||
he->next = NULL; |
|||
he->key = strdup(hkey); |
|||
he->wordCount = 0; |
|||
he->words = NULL; |
|||
he->link = NULL; |
|||
*hep = he; |
|||
} |
|||
we = (DictWord)malloc(sizeof(struct sDictWord)); |
|||
we->word = strdup(buffer); |
|||
we->next = he->words; |
|||
he->words = we; |
|||
he->wordCount++; |
|||
if (maxPC < he->wordCount) { |
|||
maxPC = he->wordCount; |
|||
mostPerms = he; |
|||
he->link = NULL; |
|||
} |
|||
else if (maxPC == he->wordCount) { |
|||
he->link = mostPerms; |
|||
mostPerms = he; |
|||
} |
|||
numWords++; |
|||
} |
|||
printf("%d words in dictionary max ana=%d\n", numWords, maxPC); |
|||
return maxPC; |
|||
} |
|||
void main() |
|||
{ |
|||
HashEntry he; |
|||
DictWord we; |
|||
FILE* f1; |
|||
f1 = fopen("unixdict.txt", "r"); |
|||
buildAnagrams(f1); |
|||
fclose(f1); |
|||
f1 = fopen("anaout.txt", "w"); |
|||
for (he = mostPerms; he; he = he->link) { |
|||
fprintf(f1, "%d: ", he->wordCount); |
|||
for (we = he->words; we; we = we->next) { |
|||
fprintf(f1, "%s, ", we->word); |
|||
} |
|||
fprintf(f1, "\n"); |
|||
} |
|||
fclose(f1); |
|||
} |
|||
END DYNC</lang> |
|||
{{out}} (2.2GHz Intel Core2 Duo) |
|||
<pre>25104 words in dictionary max ana=5 |
|||
Done in 0.031 seconds |
|||
Press any key to continue...</pre> |
|||
'''"anaout.txt" listing:''' |
|||
<pre>5: vile, veil, live, levi, evil, |
|||
5: trace, crate, cater, carte, caret, |
|||
5: regal, large, lager, glare, alger, |
|||
5: neal, lena, lean, lane, elan, |
|||
5: lange, glean, galen, angle, angel, |
|||
5: elba, bela, bale, able, abel,</pre> |
|||
== {{header|Factor}} == |
|||
<lang factor> "resource:unixdict.txt" utf8 file-lines |
|||
[ [ natural-sort >string ] keep ] { } map>assoc sort-keys |
|||
[ [ first ] compare +eq+ = ] monotonic-split |
|||
dup 0 [ length max ] reduce '[ length _ = ] filter [ values ] map .</lang> |
|||
<lang factor>{ |
|||
{ "abel" "able" "bale" "bela" "elba" } |
|||
{ "caret" "carte" "cater" "crate" "trace" } |
|||
{ "angel" "angle" "galen" "glean" "lange" } |
|||
{ "alger" "glare" "lager" "large" "regal" } |
|||
{ "elan" "lane" "lean" "lena" "neal" } |
|||
{ "evil" "levi" "live" "veil" "vile" } |
|||
}</lang> |
|||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Line 2,731: | Line 2,540: | ||
[Runtime = 6.897 sec] |
[Runtime = 6.897 sec] |
||
</pre> |
</pre> |
||
=={{header|FBSL}}== |
|||
'''A little bit of cheating: literatim re-implementation of C solution in FBSL's Dynamic C layer.''' |
|||
<lang C>#APPTYPE CONSOLE |
|||
DIM gtc = GetTickCount() |
|||
Anagram() |
|||
PRINT "Done in ", (GetTickCount() - gtc) / 1000, " seconds" |
|||
PAUSE |
|||
DYNC Anagram() |
|||
#include <windows.h> |
|||
#include <stdio.h> |
|||
char* sortedWord(const char* word, char* wbuf) |
|||
{ |
|||
char* p1, *p2, *endwrd; |
|||
char t; |
|||
int swaps; |
|||
strcpy(wbuf, word); |
|||
endwrd = wbuf + strlen(wbuf); |
|||
do { |
|||
swaps = 0; |
|||
p1 = wbuf; p2 = endwrd - 1; |
|||
while (p1 < p2) { |
|||
if (*p2 >* p1) { |
|||
t = *p2; *p2 = *p1; *p1 = t; |
|||
swaps = 1; |
|||
} |
|||
p1++; p2--; |
|||
} |
|||
p1 = wbuf; p2 = p1 + 1; |
|||
while (p2 < endwrd) { |
|||
if (*p2 >* p1) { |
|||
t = *p2; *p2 = *p1; *p1 = t; |
|||
swaps = 1; |
|||
} |
|||
p1++; p2++; |
|||
} |
|||
} while (swaps); |
|||
return wbuf; |
|||
} |
|||
static short cxmap[] = { |
|||
0x06, 0x1f, 0x4d, 0x0c, 0x5c, 0x28, 0x5d, 0x0e, 0x09, 0x33, 0x31, 0x56, |
|||
0x52, 0x19, 0x29, 0x53, 0x32, 0x48, 0x35, 0x55, 0x5e, 0x14, 0x27, 0x24, |
|||
0x02, 0x3e, 0x18, 0x4a, 0x3f, 0x4c, 0x45, 0x30, 0x08, 0x2c, 0x1a, 0x03, |
|||
0x0b, 0x0d, 0x4f, 0x07, 0x20, 0x1d, 0x51, 0x3b, 0x11, 0x58, 0x00, 0x49, |
|||
0x15, 0x2d, 0x41, 0x17, 0x5f, 0x39, 0x16, 0x42, 0x37, 0x22, 0x1c, 0x0f, |
|||
0x43, 0x5b, 0x46, 0x4b, 0x0a, 0x26, 0x2e, 0x40, 0x12, 0x21, 0x3c, 0x36, |
|||
0x38, 0x1e, 0x01, 0x1b, 0x05, 0x4e, 0x44, 0x3d, 0x04, 0x10, 0x5a, 0x2a, |
|||
0x23, 0x34, 0x25, 0x2f, 0x2b, 0x50, 0x3a, 0x54, 0x47, 0x59, 0x13, 0x57, |
|||
}; |
|||
#define CXMAP_SIZE (sizeof(cxmap) / sizeof(short)) |
|||
int Str_Hash(const char* key, int ix_max) |
|||
{ |
|||
const char* cp; |
|||
short mash; |
|||
int hash = 33501551; |
|||
for (cp = key; *cp; cp++) { |
|||
mash = cxmap[*cp % CXMAP_SIZE]; |
|||
hash = (hash >>4) ^ 0x5C5CF5C ^ ((hash << 1) + (mash << 5)); |
|||
hash &= 0x3FFFFFFF; |
|||
} |
|||
return hash % ix_max; |
|||
} |
|||
typedef struct sDictWord* DictWord; |
|||
struct sDictWord { |
|||
const char* word; |
|||
DictWord next; |
|||
}; |
|||
typedef struct sHashEntry* HashEntry; |
|||
struct sHashEntry { |
|||
const char* key; |
|||
HashEntry next; |
|||
DictWord words; |
|||
HashEntry link; |
|||
short wordCount; |
|||
}; |
|||
#define HT_SIZE 8192 |
|||
HashEntry hashTable[HT_SIZE]; |
|||
HashEntry mostPerms = NULL; |
|||
int buildAnagrams(FILE* fin) |
|||
{ |
|||
char buffer[40]; |
|||
char bufr2[40]; |
|||
char* hkey; |
|||
int hix; |
|||
HashEntry he, *hep; |
|||
DictWord we; |
|||
int maxPC = 2; |
|||
int numWords = 0; |
|||
while (fgets(buffer, 40, fin)) { |
|||
for (hkey = buffer; *hkey && (*hkey != '\n'); hkey++); |
|||
*hkey = 0; |
|||
hkey = sortedWord(buffer, bufr2); |
|||
hix = Str_Hash(hkey, HT_SIZE); |
|||
he = hashTable[hix]; hep = &hashTable[hix]; |
|||
while (he && strcmp(he->key, hkey)) { |
|||
hep = &he->next; |
|||
he = he->next; |
|||
} |
|||
if (! he) { |
|||
he = (HashEntry)malloc(sizeof(struct sHashEntry)); |
|||
he->next = NULL; |
|||
he->key = strdup(hkey); |
|||
he->wordCount = 0; |
|||
he->words = NULL; |
|||
he->link = NULL; |
|||
*hep = he; |
|||
} |
|||
we = (DictWord)malloc(sizeof(struct sDictWord)); |
|||
we->word = strdup(buffer); |
|||
we->next = he->words; |
|||
he->words = we; |
|||
he->wordCount++; |
|||
if (maxPC < he->wordCount) { |
|||
maxPC = he->wordCount; |
|||
mostPerms = he; |
|||
he->link = NULL; |
|||
} |
|||
else if (maxPC == he->wordCount) { |
|||
he->link = mostPerms; |
|||
mostPerms = he; |
|||
} |
|||
numWords++; |
|||
} |
|||
printf("%d words in dictionary max ana=%d\n", numWords, maxPC); |
|||
return maxPC; |
|||
} |
|||
void main() |
|||
{ |
|||
HashEntry he; |
|||
DictWord we; |
|||
FILE* f1; |
|||
f1 = fopen("unixdict.txt", "r"); |
|||
buildAnagrams(f1); |
|||
fclose(f1); |
|||
f1 = fopen("anaout.txt", "w"); |
|||
for (he = mostPerms; he; he = he->link) { |
|||
fprintf(f1, "%d: ", he->wordCount); |
|||
for (we = he->words; we; we = we->next) { |
|||
fprintf(f1, "%s, ", we->word); |
|||
} |
|||
fprintf(f1, "\n"); |
|||
} |
|||
fclose(f1); |
|||
} |
|||
END DYNC</lang> |
|||
{{out}} (2.2GHz Intel Core2 Duo) |
|||
<pre>25104 words in dictionary max ana=5 |
|||
Done in 0.031 seconds |
|||
Press any key to continue...</pre> |
|||
'''"anaout.txt" listing:''' |
|||
<pre>5: vile, veil, live, levi, evil, |
|||
5: trace, crate, cater, carte, caret, |
|||
5: regal, large, lager, glare, alger, |
|||
5: neal, lena, lean, lane, elan, |
|||
5: lange, glean, galen, angle, angel, |
|||
5: elba, bela, bale, able, abel,</pre> |
|||
== {{header|Factor}} == |
|||
<lang factor> "resource:unixdict.txt" utf8 file-lines |
|||
[ [ natural-sort >string ] keep ] { } map>assoc sort-keys |
|||
[ [ first ] compare +eq+ = ] monotonic-split |
|||
dup 0 [ length max ] reduce '[ length _ = ] filter [ values ] map .</lang> |
|||
<lang factor>{ |
|||
{ "abel" "able" "bale" "bela" "elba" } |
|||
{ "caret" "carte" "cater" "crate" "trace" } |
|||
{ "angel" "angle" "galen" "glean" "lange" } |
|||
{ "alger" "glare" "lager" "large" "regal" } |
|||
{ "elan" "lane" "lean" "lena" "neal" } |
|||
{ "evil" "levi" "live" "veil" "vile" } |
|||
}</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Line 4,880: | Line 4,878: | ||
elan lane lean lena neal |
elan lane lean lena neal |
||
caret carte cater crate trace |
caret carte cater crate trace |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2016.08}} |
|||
<lang perl6>my @anagrams = 'unixdict.txt'.IO.words.classify(*.comb.sort.join).values; |
|||
my $max = @anagrams».elems.max; |
|||
.put for @anagrams.grep(*.elems == $max);</lang> |
|||
{{out}} |
|||
caret carte cater crate trace |
|||
angel angle galen glean lange |
|||
alger glare lager large regal |
|||
elan lane lean lena neal |
|||
evil levi live veil vile |
|||
abel able bale bela elba |
|||
Just for the fun of it, here's a one-liner that uses no temporaries. Since it would be rather long, we've oriented it vertically: |
|||
{{works with|Rakudo|2016.08}} |
|||
<lang perl6>.put for # print each element of the array made this way: |
|||
'unixdict.txt'.IO.words # load words from file |
|||
.classify(*.comb.sort.join) # group by common anagram |
|||
.classify(*.value.elems) # group by number of anagrams in a group |
|||
.max(*.key).value # get the group with highest number of anagrams |
|||
.map(*.value) # get all groups of anagrams in the group just selected</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 5,176: | Line 5,147: | ||
StringList size=5 [ "alger", "glare", "lager", "large", "regal" ] |
StringList size=5 [ "alger", "glare", "lager", "large", "regal" ] |
||
StringList size=5 [ "caret", "carte", "cater", "crate", "trace" ]</pre> |
StringList size=5 [ "caret", "carte", "cater", "crate", "trace" ]</pre> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 5,888: | Line 5,858: | ||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2016.08}} |
|||
<lang perl6>my @anagrams = 'unixdict.txt'.IO.words.classify(*.comb.sort.join).values; |
|||
my $max = @anagrams».elems.max; |
|||
.put for @anagrams.grep(*.elems == $max);</lang> |
|||
{{out}} |
|||
caret carte cater crate trace |
|||
angel angle galen glean lange |
|||
alger glare lager large regal |
|||
elan lane lean lena neal |
|||
evil levi live veil vile |
|||
abel able bale bela elba |
|||
Just for the fun of it, here's a one-liner that uses no temporaries. Since it would be rather long, we've oriented it vertically: |
|||
{{works with|Rakudo|2016.08}} |
|||
<lang perl6>.put for # print each element of the array made this way: |
|||
'unixdict.txt'.IO.words # load words from file |
|||
.classify(*.comb.sort.join) # group by common anagram |
|||
.classify(*.value.elems) # group by number of anagrams in a group |
|||
.max(*.key).value # get the group with highest number of anagrams |
|||
.map(*.value) # get all groups of anagrams in the group just selected</lang> |
|||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
Line 5,987: | Line 5,984: | ||
{"levi","live","vile","evil","veil"} |
{"levi","live","vile","evil","veil"} |
||
]</lang> |
]</lang> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
<lang Red>Red [] |
<lang Red>Red [] |
||
Line 6,384: | Line 6,382: | ||
evil, levi, live, veil, vile |
evil, levi, live, veil, vile |
||
</pre> |
</pre> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<lang runbasic>sqliteconnect #mem, ":memory:" |
<lang runbasic>sqliteconnect #mem, ":memory:" |
||
Line 6,752: | Line 6,751: | ||
{elan lane lean lena neal} |
{elan lane lean lena neal} |
||
{evil levi live veil vile}</pre> |
{evil levi live veil vile}</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Line 6,772: | Line 6,770: | ||
evil levi live veil vile |
evil levi live veil vile |
||
caret carte cater crate trace</pre> |
caret carte cater crate trace</pre> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
<lang simula>COMMENT COMPILE WITH |
<lang simula>COMMENT COMPILE WITH |
||
Line 7,393: | Line 7,392: | ||
angel angle galen glean lange |
angel angle galen glean lange |
||
abel able bale bela elba</pre> |
abel able bale bela elba</pre> |
||
=={{header|Vedit macro language}}== |
|||
This implementation first sorts characters of each word using Insertion sort in subroutine SORT_LETTERS.<br> |
|||
Then the word list is sorted using built-in Sort function.<br> |
|||
Finally, groups of words are analyzed and largest groups are recorded. |
|||
The word list is expected to be in the same directory as the script. |
|||
<lang vedit>File_Open("|(PATH_ONLY)\unixdict.txt") |
|||
Repeat(ALL) { |
|||
Reg_Copy_Block(10, CP, EOL_Pos) // original word |
|||
Call("SORT_LETTERS") // sort letters of the word |
|||
EOL |
|||
IC(' ') Reg_Ins(10) // add the original word at eol |
|||
Line(1, ERRBREAK) |
|||
} |
|||
Sort(0, File_Size) // sort list according to anagrams |
|||
BOF |
|||
Search("|F") Search(' ') // first word in the list |
|||
Reg_Copy_Block(10, BOL_Pos, CP+1) // reg 10 = sorted anagram word |
|||
Reg_Copy_Block(11, CP, EOL_Pos) // reg 11 = list of words in current group |
|||
Reg_Empty(12) // reg 12 = list of words in largest groups |
|||
Reg_Set(13, " |
|||
") |
|||
#1 = 1 // words in this group |
|||
#2 = 2 // words in largest group found |
|||
Repeat(ALL) { |
|||
Line(1, ERRBREAK) |
|||
if (Match(@10, ADVANCE) == 0) { // same group as previous word? |
|||
Reg_Copy_Block(11, CP-1, EOL_Pos, APPEND) // add word to this group |
|||
#1++ |
|||
} else { // different anagram group |
|||
Search(" ", ERRBREAK) |
|||
if (#1 == #2) { // same size as the largest? |
|||
Reg_Set(12, @13, APPEND) // append newline |
|||
Reg_Set(12, @11, APPEND) // append word list |
|||
} |
|||
if (#1 > #2) { // new larger size of group |
|||
Reg_Set(12, @11) // replace word list |
|||
#2 = #1 |
|||
} |
|||
Reg_Copy_Block(10, BOL_Pos, CP+1) |
|||
Reg_Copy_Block(11, CP, EOL_Pos) // first word of new group |
|||
#1 = 1 |
|||
} |
|||
} |
|||
Buf_Quit(OK) // close word list file |
|||
Buf_Switch(Buf_Free) // output results in a new edit buffer |
|||
Reg_Ins(12) // display all groups of longest anagram words |
|||
Return |
|||
//////////////////////////////////////////////////////////////////// |
|||
// |
|||
// Sort characters in current line using Insertion sort |
|||
// |
|||
:SORT_LETTERS: |
|||
GP(EOL_pos) #9 = Cur_Col-1 |
|||
for (#1 = 2; #1 <= #9; #1++) { |
|||
Goto_Col(#1) #8 = Cur_Char |
|||
#2 = #1 |
|||
while (#2 > 1) { |
|||
#7 = Cur_Char(-1) |
|||
if (#7 <= #8) { break } |
|||
Ins_Char(#7, OVERWRITE) |
|||
#2-- |
|||
Goto_Col(#2) |
|||
} |
|||
Ins_Char(#8, OVERWRITE) |
|||
} |
|||
return</lang> |
|||
{{out}} |
|||
<pre> |
|||
abel able bale bela elba |
|||
caret carte cater crate trace |
|||
angel angle galen glean lange |
|||
alger glare lager large regal |
|||
elan lane lean lena neal |
|||
evil levi live veil vile |
|||
</pre> |
|||
{{omit from|PARI/GP|No real capacity for string manipulation}} |
|||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Line 7,653: | Line 7,569: | ||
Time to go : 2,464844 seconds.</pre> |
Time to go : 2,464844 seconds.</pre> |
||
=={{header|Vedit macro language}}== |
|||
This implementation first sorts characters of each word using Insertion sort in subroutine SORT_LETTERS.<br> |
|||
Then the word list is sorted using built-in Sort function.<br> |
|||
Finally, groups of words are analyzed and largest groups are recorded. |
|||
The word list is expected to be in the same directory as the script. |
|||
<lang vedit>File_Open("|(PATH_ONLY)\unixdict.txt") |
|||
Repeat(ALL) { |
|||
Reg_Copy_Block(10, CP, EOL_Pos) // original word |
|||
Call("SORT_LETTERS") // sort letters of the word |
|||
EOL |
|||
IC(' ') Reg_Ins(10) // add the original word at eol |
|||
Line(1, ERRBREAK) |
|||
} |
|||
Sort(0, File_Size) // sort list according to anagrams |
|||
BOF |
|||
Search("|F") Search(' ') // first word in the list |
|||
Reg_Copy_Block(10, BOL_Pos, CP+1) // reg 10 = sorted anagram word |
|||
Reg_Copy_Block(11, CP, EOL_Pos) // reg 11 = list of words in current group |
|||
Reg_Empty(12) // reg 12 = list of words in largest groups |
|||
Reg_Set(13, " |
|||
") |
|||
#1 = 1 // words in this group |
|||
#2 = 2 // words in largest group found |
|||
Repeat(ALL) { |
|||
Line(1, ERRBREAK) |
|||
if (Match(@10, ADVANCE) == 0) { // same group as previous word? |
|||
Reg_Copy_Block(11, CP-1, EOL_Pos, APPEND) // add word to this group |
|||
#1++ |
|||
} else { // different anagram group |
|||
Search(" ", ERRBREAK) |
|||
if (#1 == #2) { // same size as the largest? |
|||
Reg_Set(12, @13, APPEND) // append newline |
|||
Reg_Set(12, @11, APPEND) // append word list |
|||
} |
|||
if (#1 > #2) { // new larger size of group |
|||
Reg_Set(12, @11) // replace word list |
|||
#2 = #1 |
|||
} |
|||
Reg_Copy_Block(10, BOL_Pos, CP+1) |
|||
Reg_Copy_Block(11, CP, EOL_Pos) // first word of new group |
|||
#1 = 1 |
|||
} |
|||
} |
|||
Buf_Quit(OK) // close word list file |
|||
Buf_Switch(Buf_Free) // output results in a new edit buffer |
|||
Reg_Ins(12) // display all groups of longest anagram words |
|||
Return |
|||
//////////////////////////////////////////////////////////////////// |
|||
// |
|||
// Sort characters in current line using Insertion sort |
|||
// |
|||
:SORT_LETTERS: |
|||
GP(EOL_pos) #9 = Cur_Col-1 |
|||
for (#1 = 2; #1 <= #9; #1++) { |
|||
Goto_Col(#1) #8 = Cur_Char |
|||
#2 = #1 |
|||
while (#2 > 1) { |
|||
#7 = Cur_Char(-1) |
|||
if (#7 <= #8) { break } |
|||
Ins_Char(#7, OVERWRITE) |
|||
#2-- |
|||
Goto_Col(#2) |
|||
} |
|||
Ins_Char(#8, OVERWRITE) |
|||
} |
|||
return</lang> |
|||
{{out}} |
|||
<pre> |
|||
abel able bale bela elba |
|||
caret carte cater crate trace |
|||
angel angle galen glean lange |
|||
alger glare lager large regal |
|||
elan lane lean lena neal |
|||
evil levi live veil vile |
|||
</pre> |
|||
{{omit from|PARI/GP|No real capacity for string manipulation}} |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |