First commit of partial RosettaCode contents.
Pushing this for testing purposes. Lots of work still needed.
This commit is contained in:
commit
1e05ecd7ee
781 changed files with 9080 additions and 0 deletions
1
Task/Anagrams/0DESCRIPTION
Normal file
1
Task/Anagrams/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Two or more words can be composed of the same characters, but in a different order. Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them.
|
||||
2
Task/Anagrams/1META.yaml
Normal file
2
Task/Anagrams/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Text processing
|
||||
26
Task/Anagrams/AWK/anagrams.awk
Normal file
26
Task/Anagrams/AWK/anagrams.awk
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# JUMBLEA.AWK - words with the most duplicate spellings
|
||||
# syntax: GAWK -f JUMBLEA.AWK UNIXDICT.TXT
|
||||
{ for (i=1; i<=NF; i++) {
|
||||
w = sortstr(toupper($i))
|
||||
arr[w] = arr[w] $i " "
|
||||
n = gsub(/ /,"&",arr[w])
|
||||
if (max_n < n) { max_n = n }
|
||||
}
|
||||
}
|
||||
END {
|
||||
for (w in arr) {
|
||||
if (gsub(/ /,"&",arr[w]) == max_n) {
|
||||
printf("%s\t%s\n",w,arr[w])
|
||||
}
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
function sortstr(str, i,j,leng) {
|
||||
leng = length(str)
|
||||
for (i=2; i<=leng; i++) {
|
||||
for (j=i; j>1 && substr(str,j-1,1) > substr(str,j,1); j--) {
|
||||
str = substr(str,1,j-2) substr(str,j,1) substr(str,j-1,1) substr(str,j+1)
|
||||
}
|
||||
}
|
||||
return(str)
|
||||
}
|
||||
101
Task/Anagrams/C/anagrams-2.c
Normal file
101
Task/Anagrams/C/anagrams-2.c
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct { const char *key, *word; int cnt; } kw_t;
|
||||
|
||||
int lst_cmp(const void *a, const void *b)
|
||||
{
|
||||
return strcmp(((const kw_t*)a)->key, ((const kw_t*)b)->key);
|
||||
}
|
||||
|
||||
/* Bubble sort. Faster than stock qsort(), believe it or not */
|
||||
void sort_letters(char *s)
|
||||
{
|
||||
int i, j;
|
||||
char t;
|
||||
for (i = 0; s[i] != '\0'; i++) {
|
||||
for (j = i + 1; s[j] != '\0'; j++)
|
||||
if (s[j] < s[i]) {
|
||||
t = s[j]; s[j] = s[i]; s[i] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
struct stat s;
|
||||
char *words, *keys;
|
||||
size_t i, j, k, longest, offset;
|
||||
int n_word = 0;
|
||||
kw_t *list;
|
||||
|
||||
int fd = open("unixdict.txt", O_RDONLY);
|
||||
if (fd == -1) return 1;
|
||||
fstat(fd, &s);
|
||||
words = malloc(s.st_size * 2);
|
||||
keys = words + s.st_size;
|
||||
|
||||
read(fd, words, s.st_size);
|
||||
memcpy(keys, words, s.st_size);
|
||||
|
||||
/* change newline to null for easy use; sort letters in keys */
|
||||
for (i = j = 0; i < s.st_size; i++) {
|
||||
if (words[i] == '\n') {
|
||||
words[i] = keys[i] = '\0';
|
||||
sort_letters(keys + j);
|
||||
j = i + 1;
|
||||
n_word ++;
|
||||
}
|
||||
}
|
||||
|
||||
list = calloc(n_word, sizeof(kw_t));
|
||||
|
||||
/* make key/word pointer pairs for sorting */
|
||||
for (i = j = k = 0; i < s.st_size; i++) {
|
||||
if (words[i] == '\0') {
|
||||
list[j].key = keys + k;
|
||||
list[j].word = words + k;
|
||||
k = i + 1;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
qsort(list, n_word, sizeof(kw_t), lst_cmp);
|
||||
|
||||
/* count each key's repetition */
|
||||
for (i = j = k = offset = longest = 0; i < n_word; i++) {
|
||||
if (!strcmp(list[i].key, list[j].key)) {
|
||||
++k;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* move current longest to begining of array */
|
||||
if (k < longest) {
|
||||
k = 0;
|
||||
j = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (k > longest) offset = 0;
|
||||
|
||||
while (j < i) list[offset++] = list[j++];
|
||||
longest = k;
|
||||
k = 0;
|
||||
}
|
||||
|
||||
/* show the longest */
|
||||
for (i = 0; i < offset; i++) {
|
||||
printf("%s ", list[i].word);
|
||||
if (i < n_word - 1 && strcmp(list[i].key, list[i+1].key))
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* free(list); free(words); */
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
160
Task/Anagrams/C/anagrams.c
Normal file
160
Task/Anagrams/C/anagrams.c
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.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 = malloc(sizeof(struct sHashEntry));
|
||||
he->next = NULL;
|
||||
he->key = strdup(hkey);
|
||||
he->wordCount = 0;
|
||||
he->words = NULL;
|
||||
he->link = NULL;
|
||||
*hep = he;
|
||||
}
|
||||
we = 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;
|
||||
}
|
||||
|
||||
|
||||
int main( )
|
||||
{
|
||||
HashEntry he;
|
||||
DictWord we;
|
||||
FILE *f1;
|
||||
|
||||
f1 = fopen("unixdict.txt","r");
|
||||
buildAnagrams(f1);
|
||||
fclose(f1);
|
||||
|
||||
f1 = fopen("anaout.txt","w");
|
||||
// f1 = stdout;
|
||||
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
10
Task/Anagrams/Clojure/anagrams.clj
Normal file
10
Task/Anagrams/Clojure/anagrams.clj
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(require '[clojure.java.io :as io])
|
||||
|
||||
(def groups
|
||||
(with-open [r (io/reader wordfile)]
|
||||
(group-by sort (line-seq r)))
|
||||
|
||||
(let [wordlists (sort-by (comp - count) (vals groups)
|
||||
maxlength (count (first wordlists))]
|
||||
(doseq [wordlist (take-while #(= (count %) maxlength) wordlists)]
|
||||
(println wordlist))
|
||||
7
Task/Anagrams/CoffeeScript/anagrams-2.coffee
Normal file
7
Task/Anagrams/CoffeeScript/anagrams-2.coffee
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
> coffee anagrams.coffee
|
||||
[ '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' ]
|
||||
31
Task/Anagrams/CoffeeScript/anagrams.coffee
Normal file
31
Task/Anagrams/CoffeeScript/anagrams.coffee
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
http = require 'http'
|
||||
|
||||
show_large_anagram_sets = (word_lst) ->
|
||||
anagrams = {}
|
||||
max_size = 0
|
||||
|
||||
for word in word_lst
|
||||
key = word.split('').sort().join('')
|
||||
anagrams[key] ?= []
|
||||
anagrams[key].push word
|
||||
size = anagrams[key].length
|
||||
max_size = size if size > max_size
|
||||
|
||||
for key, variations of anagrams
|
||||
if variations.length == max_size
|
||||
console.log variations.join ' '
|
||||
|
||||
get_word_list = (process) ->
|
||||
options =
|
||||
host: "www.puzzlers.org"
|
||||
path: "/pub/wordlists/unixdict.txt"
|
||||
|
||||
req = http.request options, (res) ->
|
||||
s = ''
|
||||
res.on 'data', (chunk) ->
|
||||
s += chunk
|
||||
res.on 'end', ->
|
||||
process s.split '\n'
|
||||
req.end()
|
||||
|
||||
get_word_list show_large_anagram_sets
|
||||
29
Task/Anagrams/Erlang/anagrams.erl
Normal file
29
Task/Anagrams/Erlang/anagrams.erl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
-module(anagrams).
|
||||
-compile(export_all).
|
||||
|
||||
play() ->
|
||||
{ok, P} = file:read_file('unixdict.txt'),
|
||||
D = dict:new(),
|
||||
E=fetch(string:tokens(binary_to_list(P), "\n"), D),
|
||||
get_value(dict:fetch_keys(E), E).
|
||||
|
||||
fetch([H|T], D) ->
|
||||
fetch(T, dict:append(lists:sort(H), H, D));
|
||||
fetch([], D) ->
|
||||
D.
|
||||
|
||||
get_value(L, D) -> get_value(L,D,1,[]).
|
||||
get_value([H|T], D, N, L) ->
|
||||
Var = dict:fetch(H,D),
|
||||
Len = length(Var),
|
||||
if
|
||||
Len > N ->
|
||||
get_value(T, D, Len, [Var]);
|
||||
Len == N ->
|
||||
get_value(T, D, Len, [Var | L]);
|
||||
Len < N ->
|
||||
get_value(T, D, N, L)
|
||||
end;
|
||||
|
||||
get_value([], _, _, L) ->
|
||||
L.
|
||||
5
Task/Anagrams/Go/anagrams-2.go
Normal file
5
Task/Anagrams/Go/anagrams-2.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def words = new URL('http://www.puzzlers.org/pub/wordlists/unixdict.txt').text.readLines()
|
||||
def groups = words.groupBy{ it.toList().sort() }
|
||||
def bigGroupSize = groups.collect{ it.value.size() }.max()
|
||||
def isBigAnagram = { it.value.size() == bigGroupSize }
|
||||
println groups.findAll(isBigAnagram).collect{ it.value }.collect{ it.join(' ') }.join('\n')
|
||||
10
Task/Anagrams/Go/anagrams-3.go
Normal file
10
Task/Anagrams/Go/anagrams-3.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import Data.List
|
||||
|
||||
groupon f x y = f x == f y
|
||||
|
||||
main = do
|
||||
f <- readFile "./../Puzzels/Rosetta/unixdict.txt"
|
||||
let words = lines f
|
||||
wix = groupBy (groupon fst) . sort $ zip (map sort words) words
|
||||
mxl = maximum $ map length wix
|
||||
mapM_ (print . map snd) . filter ((==mxl).length) $ wix
|
||||
7
Task/Anagrams/Go/anagrams-4.go
Normal file
7
Task/Anagrams/Go/anagrams-4.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
*Main> main
|
||||
["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"]
|
||||
39
Task/Anagrams/Go/anagrams.go
Normal file
39
Task/Anagrams/Go/anagrams.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b, err := ioutil.ReadFile("unixdict.txt")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
var ma int
|
||||
m := make(map[string][]string)
|
||||
for _, word := range strings.Split(string(b), "\n") {
|
||||
bs := byteSlice(word)
|
||||
sort.Sort(bs)
|
||||
k := string(bs)
|
||||
a := append(m[k], word)
|
||||
if len(a) > ma {
|
||||
ma = len(a)
|
||||
}
|
||||
m[k] = a
|
||||
}
|
||||
for _, a := range m {
|
||||
if len(a) == ma {
|
||||
fmt.Println(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type byteSlice []byte
|
||||
|
||||
func (b byteSlice) Len() int { return len(b) }
|
||||
func (b byteSlice) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
||||
func (b byteSlice) Less(i, j int) bool { return b[i] < b[j] }
|
||||
30
Task/Anagrams/Java/anagrams.java
Normal file
30
Task/Anagrams/Java/anagrams.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class WordsOfEqChars {
|
||||
public static void main(String[] args) throws IOException {
|
||||
URL url = new URL("http://www.puzzlers.org/pub/wordlists/unixdict.txt");
|
||||
InputStreamReader isr = new InputStreamReader(url.openStream());
|
||||
BufferedReader reader = new BufferedReader(isr);
|
||||
|
||||
Map<String, Collection<String>> anagrams = new HashMap<String, Collection<String>>();
|
||||
String word;
|
||||
int count = 0;
|
||||
while ((word = reader.readLine()) != null) {
|
||||
char[] chars = word.toCharArray();
|
||||
Arrays.sort(chars);
|
||||
String key = new String(chars);
|
||||
if (!anagrams.containsKey(key))
|
||||
anagrams.put(key, new ArrayList<String>());
|
||||
anagrams.get(key).add(word);
|
||||
count = Math.max(count, anagrams.get(key).size());
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
for (Collection<String> ana : anagrams.values())
|
||||
if (ana.size() >= count)
|
||||
System.out.println(ana);
|
||||
}
|
||||
}
|
||||
20
Task/Anagrams/JavaScript/anagrams.js
Normal file
20
Task/Anagrams/JavaScript/anagrams.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env js
|
||||
|
||||
var anas = {};
|
||||
var words = read('unixdict.txt').split(/\n/g);
|
||||
|
||||
for (var w in words) {
|
||||
var key = words[w].split("").sort().join('');
|
||||
if (!(key in anas)) {
|
||||
anas[key] = [];
|
||||
}
|
||||
anas[key].push(words[w]);
|
||||
}
|
||||
|
||||
for (var a in anas) {
|
||||
if (anas[a].length >= 2) {
|
||||
print(anas[a]);
|
||||
}
|
||||
}
|
||||
|
||||
quit();
|
||||
73
Task/Anagrams/Lua/anagrams.lua
Normal file
73
Task/Anagrams/Lua/anagrams.lua
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
-- Build the word set
|
||||
local set = {}
|
||||
local file = io.open("unixdict.txt")
|
||||
local str = file:read()
|
||||
while str do
|
||||
table.insert(set,str)
|
||||
str = file:read()
|
||||
end
|
||||
|
||||
-- Build the anagram tree
|
||||
local tree = {}
|
||||
for i,word in next,set do
|
||||
-- Sort a string from lowest char to highest
|
||||
local function sortString(str)
|
||||
if #str <= 1 then
|
||||
return str
|
||||
end
|
||||
local less = ''
|
||||
local greater = ''
|
||||
local pivot = str:byte(1)
|
||||
for i = 2, #str do
|
||||
if str:byte(i) <= pivot then
|
||||
less = less..(str:sub(i,i))
|
||||
else
|
||||
greater = greater..(str:sub(i,i))
|
||||
end
|
||||
end
|
||||
return sortString(less)..str:sub(1,1)..sortString(greater)
|
||||
end
|
||||
local sortchar = sortString(word)
|
||||
if not tree[#word] then tree[#word] = {} end
|
||||
local node = tree[#word]
|
||||
for i = 1,#word do
|
||||
if not node[sortchar:byte(i)] then
|
||||
node[sortchar:byte(i)] = {}
|
||||
end
|
||||
node = node[sortchar:byte(i)]
|
||||
end
|
||||
table.insert(node,word)
|
||||
end
|
||||
|
||||
-- Gather largest groups by gathering all groups of current max size and droping gathered groups and increasing max when a new largest group is found
|
||||
local max = 0
|
||||
local set = {}
|
||||
local function recurse (tree)
|
||||
local num = 0
|
||||
for i,node in next,tree do
|
||||
if type(node) == 'string' then
|
||||
num = num + 1
|
||||
end
|
||||
end
|
||||
if num > max then
|
||||
set = {}
|
||||
max = num
|
||||
end
|
||||
if num == max then
|
||||
local newset = {}
|
||||
for i,node in next,tree do
|
||||
if type(node) == 'string' then
|
||||
table.insert(newset,node)
|
||||
end
|
||||
end
|
||||
table.insert(set,newset)
|
||||
end
|
||||
for i,node in next,tree do
|
||||
if type(node) == 'table' then
|
||||
recurse(node)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
recurse (tree)
|
||||
for i,v in next,set do io.write (i..':\t')for j,u in next,v do io.write (u..' ') end print() end
|
||||
13
Task/Anagrams/PHP/anagrams.php
Normal file
13
Task/Anagrams/PHP/anagrams.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
$words = explode("\n", file_get_contents('http://www.puzzlers.org/pub/wordlists/unixdict.txt'));
|
||||
foreach ($words as $word) {
|
||||
$chars = str_split($word);
|
||||
sort($chars);
|
||||
$anagram[implode($chars)][] = $word;
|
||||
}
|
||||
|
||||
$best = max(array_map('count', $anagram));
|
||||
foreach ($anagram as $ana)
|
||||
if (count($ana) == $best)
|
||||
print_r($ana);
|
||||
?>
|
||||
7
Task/Anagrams/Perl/anagrams-2.pl
Normal file
7
Task/Anagrams/Perl/anagrams-2.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use LWP::Simple;
|
||||
|
||||
for (split ' ' => get 'http://www.puzzlers.org/pub/wordlists/unixdict.txt')
|
||||
{push @{$anagram{ join '' => sort split // }}, $_}
|
||||
|
||||
$max > @$_ or $max = @$_ for values %anagram;
|
||||
@$_ >= $max and print "@$_\n" for values %anagram;
|
||||
15
Task/Anagrams/Perl/anagrams.pl
Normal file
15
Task/Anagrams/Perl/anagrams.pl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use LWP::Simple;
|
||||
use List::Util qw(max);
|
||||
|
||||
my @words = split(' ', get('http://www.puzzlers.org/pub/wordlists/unixdict.txt'));
|
||||
my %anagram;
|
||||
foreach my $word (@words) {
|
||||
push @{ $anagram{join('', sort(split(//, $word)))} }, $word;
|
||||
}
|
||||
|
||||
my $count = max(map {scalar @$_} values %anagram);
|
||||
foreach my $ana (values %anagram) {
|
||||
if (@$ana >= $count) {
|
||||
print "@$ana\n";
|
||||
}
|
||||
}
|
||||
8
Task/Anagrams/PicoLisp/anagrams-2.l
Normal file
8
Task/Anagrams/PicoLisp/anagrams-2.l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(let Words NIL
|
||||
(in "unixdict.txt"
|
||||
(while (line)
|
||||
(let (Word (pack @) Key (pack (sort @)))
|
||||
(if (idx 'Words Key T)
|
||||
(push (car @) Word)
|
||||
(set Key (list Word)) ) ) ) )
|
||||
(flip (by length sort (mapcar val (idx 'Words)))) )
|
||||
4
Task/Anagrams/PicoLisp/anagrams.l
Normal file
4
Task/Anagrams/PicoLisp/anagrams.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(flip
|
||||
(by length sort
|
||||
(by '((L) (sort (copy L))) group
|
||||
(in "unixdict.txt" (make (while (line) (link @)))) ) ) )
|
||||
14
Task/Anagrams/Python/anagrams-2.py
Normal file
14
Task/Anagrams/Python/anagrams-2.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import urllib.request, itertools
|
||||
import time
|
||||
words = urllib.request.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
print('Words ready')
|
||||
t0 = time.clock()
|
||||
anagrams = [list(g) for k,g in itertools.groupby(sorted(words, key=sorted), key=sorted)]
|
||||
anagrams.sort(key=len, reverse=True)
|
||||
count = len(anagrams[0])
|
||||
for ana in anagrams:
|
||||
if len(ana) < count:
|
||||
break
|
||||
print(ana)
|
||||
t0 -= time.clock()
|
||||
print('Finished in %f s' % -t0)
|
||||
25
Task/Anagrams/Python/anagrams-3.py
Normal file
25
Task/Anagrams/Python/anagrams-3.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
>>> import urllib
|
||||
>>> from collections import defaultdict
|
||||
>>> words = urllib.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> len(words)
|
||||
25104
|
||||
>>> anagram = defaultdict(list) # map sorted chars to anagrams
|
||||
>>> for word in words:
|
||||
anagram[tuple(sorted(word))].append( word )
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagram.itervalues())
|
||||
>>> for ana in anagram.itervalues():
|
||||
if len(ana) >= count:
|
||||
print ana
|
||||
|
||||
|
||||
['angel', 'angle', 'galen', 'glean', 'lange']
|
||||
['alger', 'glare', 'lager', 'large', 'regal']
|
||||
['caret', 'carte', 'cater', 'crate', 'trace']
|
||||
['evil', 'levi', 'live', 'veil', 'vile']
|
||||
['elan', 'lane', 'lean', 'lena', 'neal']
|
||||
['abel', 'able', 'bale', 'bela', 'elba']
|
||||
>>> count
|
||||
5
|
||||
>>>
|
||||
22
Task/Anagrams/Python/anagrams-4.py
Normal file
22
Task/Anagrams/Python/anagrams-4.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
>>> import urllib, itertools
|
||||
>>> words = urllib.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> len(words)
|
||||
25104
|
||||
>>> anagrams = [list(g) for k,g in itertools.groupby(sorted(words, key=sorted), key=sorted)]
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagrams)
|
||||
>>> for ana in anagrams:
|
||||
if len(ana) >= count:
|
||||
print ana
|
||||
|
||||
|
||||
['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']
|
||||
>>> count
|
||||
5
|
||||
>>>
|
||||
12
Task/Anagrams/Python/anagrams.py
Normal file
12
Task/Anagrams/Python/anagrams.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
>>> import urllib.request
|
||||
>>> from collections import defaultdict
|
||||
>>> words = urllib.request.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> anagram = defaultdict(list) # map sorted chars to anagrams
|
||||
>>> for word in words:
|
||||
anagram[tuple(sorted(word))].append( word )
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagram.values())
|
||||
>>> for ana in anagram.values():
|
||||
if len(ana) >= count:
|
||||
print ([x.decode() for x in ana])
|
||||
33
Task/Anagrams/REXX/anagrams.rexx
Normal file
33
Task/Anagrams/REXX/anagrams.rexx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*REXX program finds words with the largest set of anagrams (same size).*/
|
||||
ifid='unixdict.txt'; words=0 /*input file identifier, # words.*/
|
||||
wL.=0 /*number of words of length L. */
|
||||
do j=1 while lines(ifid)\==0 /*read each word in file (word=X)*/
|
||||
x=space(linein(ifid),0) /*pick off a word from the input.*/
|
||||
L=length(x); if L<3 then iterate /*onesies and twosies can't win. */
|
||||
words=words+1 /*count of (useable) words. */
|
||||
@.words=x /*save the word in an array. */
|
||||
wL.L=wL.L+1; _=wL.L /*counter of words of length L. */
|
||||
@@.L._=x /*array of words of length L. */
|
||||
/*sort the letters*/ do ja=1 for L; !.ja=substr(x,ja,1); end
|
||||
!.0=L; call esort;z=; do jb=1 for L; z=z || !.jb; end
|
||||
@@s.L._=z /*store the sorted word (letters)*/
|
||||
@s.words=@@s.L._ /*and also, sorted length L vers.*/
|
||||
end /*j*/
|
||||
a.= /*all the anagrams for word X. */
|
||||
say copies('â',30) words 'words in the dictionary file: ' ifid
|
||||
n.=0 /*number of anagrams for word X. */
|
||||
do j=1 for words /*process the usable words found.*/
|
||||
x=@.j; Lx=length(x); xs=@s.j /*get some vital statistics for X*/
|
||||
do k=1 for wL.Lx /*process all the words of len L.*/
|
||||
if xs\==@@s.Lx.k then iterate /*is this a true anagram of X ? */
|
||||
if x==@@.Lx.k then iterate /*skip doing anagram on itself. */
|
||||
n.j=n.j+1; a.j=a.j @@.Lx.k /*bump counter, add ââ⺠anagrams.*/
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
m=n.1 /*assume first (len=1) is largest*/
|
||||
do j=2 to words; m=max(m,n.j); end /*find the maximum anagram count.*/
|
||||
do k=1 for words; if n.k==m then if word(a.k,1)>@.k then say @.k a.k; end
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*ââââââââââââââââââââââââââââââââââESORTâââââââââââââââââââââââââââââââ*/
|
||||
esort:procedure expose !.;h=!.0;do while h>1;h=h%2;do i=1 for !.0-h;j=i;k=h+i
|
||||
do while !.k<!.j;t=!.j;!.j=!.k;!.k=t;if h>=j then leave;j=j-h;k=k-h;end;end;end;return
|
||||
14
Task/Anagrams/Ruby/anagrams-2.rb
Normal file
14
Task/Anagrams/Ruby/anagrams-2.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
require 'open-uri'
|
||||
|
||||
anagram = nil
|
||||
|
||||
open('http://www.puzzlers.org/pub/wordlists/unixdict.txt') do |f|
|
||||
anagram = f.read.split.group_by {|s| s.each_char.sort}
|
||||
end
|
||||
|
||||
count = anagram.each_value.map {|ana| ana.length}.max
|
||||
anagram.each_value do |ana|
|
||||
if ana.length >= count
|
||||
p ana
|
||||
end
|
||||
end
|
||||
17
Task/Anagrams/Ruby/anagrams.rb
Normal file
17
Task/Anagrams/Ruby/anagrams.rb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
require 'open-uri'
|
||||
|
||||
anagram = Hash.new {|hash, key| hash[key] = []} # map sorted chars to anagrams
|
||||
|
||||
open('http://www.puzzlers.org/pub/wordlists/unixdict.txt') do |f|
|
||||
words = f.read.split
|
||||
for word in words
|
||||
anagram[word.split('').sort] << word
|
||||
end
|
||||
end
|
||||
|
||||
count = anagram.values.map {|ana| ana.length}.max
|
||||
anagram.each_value do |ana|
|
||||
if ana.length >= count
|
||||
p ana
|
||||
end
|
||||
end
|
||||
6
Task/Anagrams/Scala/anagrams-2.scala
Normal file
6
Task/Anagrams/Scala/anagrams-2.scala
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Source
|
||||
.fromURL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").getLines.toList
|
||||
.groupBy(_.sorted).values
|
||||
.groupBy(_.size).maxBy(_._1)._2
|
||||
.map(_.mkString("\t"))
|
||||
.foreach(println)
|
||||
4
Task/Anagrams/Scala/anagrams.scala
Normal file
4
Task/Anagrams/Scala/anagrams.scala
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
val src = io.Source fromURL "http://www.puzzlers.org/pub/wordlists/unixdict.txt"
|
||||
val vls = src.getLines.toList.groupBy(_.sorted).values
|
||||
val max = vls.map(_.size).max
|
||||
vls filter (_.size == max) map (_ mkString " ") mkString "\n"
|
||||
24
Task/Anagrams/Tcl/anagrams.tcl
Normal file
24
Task/Anagrams/Tcl/anagrams.tcl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package require Tcl 8.5
|
||||
package require http
|
||||
|
||||
set url http://www.puzzlers.org/pub/wordlists/unixdict.txt
|
||||
set response [http::geturl $url]
|
||||
set data [http::data $response]
|
||||
http::cleanup $response
|
||||
|
||||
set max 0
|
||||
array set anagrams {}
|
||||
|
||||
foreach line [split $data \n] {
|
||||
foreach word [split $line] {
|
||||
set anagram [join [lsort [split $word ""]] ""]
|
||||
lappend anagrams($anagram) $word
|
||||
set max [::tcl::mathfunc::max $max [llength $anagrams($anagram)]]
|
||||
}
|
||||
}
|
||||
|
||||
foreach key [array names anagrams] {
|
||||
if {[llength $anagrams($key)] == $max} {
|
||||
puts $anagrams($key)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue