new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
102
Task/Anagrams/ABAP/anagrams.abap
Normal file
102
Task/Anagrams/ABAP/anagrams.abap
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
report zz_anagrams no standard page heading.
|
||||
define update_progress.
|
||||
call function 'SAPGUI_PROGRESS_INDICATOR'
|
||||
exporting
|
||||
text = &1.
|
||||
end-of-definition.
|
||||
|
||||
" Selection screen segment allowing the person to choose which file will act as input.
|
||||
selection-screen begin of block file_choice.
|
||||
parameters p_file type string lower case.
|
||||
selection-screen end of block file_choice.
|
||||
|
||||
" When the user requests help with input, run the routine to allow them to navigate the presentation server.
|
||||
at selection-screen on value-request for p_file.
|
||||
perform getfile using p_file.
|
||||
|
||||
at selection-screen output.
|
||||
%_p_file_%_app_%-text = 'Input File: '.
|
||||
|
||||
start-of-selection.
|
||||
data: gt_data type table of string.
|
||||
|
||||
" Read the specified file from the presentation server into memory.
|
||||
perform readfile using p_file changing gt_data.
|
||||
" After the file has been read into memory, loop through it line-by-line and make anagrams.
|
||||
perform anagrams using gt_data.
|
||||
|
||||
" Subroutine for generating a list of anagrams.
|
||||
" The supplied input is a table, with each entry corresponding to a word.
|
||||
form anagrams using it_data like gt_data.
|
||||
types begin of ty_map.
|
||||
types key type string.
|
||||
types value type string.
|
||||
types end of ty_map.
|
||||
|
||||
data: lv_char type c,
|
||||
lv_len type i,
|
||||
lv_string type string,
|
||||
ls_entry type ty_map,
|
||||
lt_anagrams type standard table of ty_map,
|
||||
lt_c_tab type table of string.
|
||||
|
||||
field-symbols: <fs_raw> type string.
|
||||
" Loop through each word in the table, and make an associative array.
|
||||
loop at gt_data assigning <fs_raw>.
|
||||
" First, we need to re-order the word alphabetically. This generated a key. All anagrams will use this same key.
|
||||
" Add each character to a table, which we will then sort alphabetically.
|
||||
lv_len = strlen( <fs_raw> ).
|
||||
refresh lt_c_tab.
|
||||
do lv_len times.
|
||||
lv_len = sy-index - 1.
|
||||
append <fs_raw>+lv_len(1) to lt_c_tab.
|
||||
enddo.
|
||||
sort lt_c_tab as text.
|
||||
" Now append the characters to a string and add it as a key into the map.
|
||||
clear lv_string.
|
||||
loop at lt_c_tab into lv_char.
|
||||
concatenate lv_char lv_string into lv_string respecting blanks.
|
||||
endloop.
|
||||
ls_entry-key = lv_string.
|
||||
ls_entry-value = <fs_raw>.
|
||||
append ls_entry to lt_anagrams.
|
||||
endloop.
|
||||
" After we're done processing, output a list of the anagrams.
|
||||
clear lv_string.
|
||||
loop at lt_anagrams into ls_entry.
|
||||
" Is it part of the same key --> Output in the same line, else a new entry.
|
||||
if lv_string = ls_entry-key.
|
||||
write: ', ', ls_entry-value.
|
||||
else.
|
||||
if sy-tabix <> 1.
|
||||
write: ']'.
|
||||
endif.
|
||||
write: / '[', ls_entry-value.
|
||||
endif.
|
||||
lv_string = ls_entry-key.
|
||||
endloop.
|
||||
" Close last entry.
|
||||
write ']'.
|
||||
endform.
|
||||
|
||||
" Read a specified file from the presentation server.
|
||||
form readfile using i_file type string changing it_raw like gt_data.
|
||||
data: l_datat type string,
|
||||
l_msg(2048),
|
||||
l_lines(10).
|
||||
|
||||
" Read the file into memory.
|
||||
update_progress 'Reading file...'.
|
||||
call method cl_gui_frontend_services=>gui_upload
|
||||
exporting
|
||||
filename = i_file
|
||||
changing
|
||||
data_tab = it_raw
|
||||
exceptions
|
||||
others = 1.
|
||||
" Output error if the file could not be uploaded.
|
||||
if sy-subrc <> 0.
|
||||
write : / 'Error reading the supplied file!'.
|
||||
return.
|
||||
endif.
|
||||
endform.
|
||||
69
Task/Anagrams/Ada/anagrams.ada
Normal file
69
Task/Anagrams/Ada/anagrams.ada
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
with Ada.Containers.Indefinite_Ordered_Maps;
|
||||
with Ada.Containers.Indefinite_Ordered_Sets;
|
||||
|
||||
procedure Words_Of_Equal_Characters is
|
||||
package Set_Of_Words is new Ada.Containers.Indefinite_Ordered_Sets (String);
|
||||
use Ada.Containers, Set_Of_Words;
|
||||
package Anagrams is new Ada.Containers.Indefinite_Ordered_Maps (String, Set);
|
||||
use Anagrams;
|
||||
|
||||
File : File_Type;
|
||||
Result : Map;
|
||||
Max : Count_Type := 1;
|
||||
|
||||
procedure Put (Position : Anagrams.Cursor) is
|
||||
First : Boolean := True;
|
||||
List : Set renames Element (Position);
|
||||
procedure Put (Position : Set_Of_Words.Cursor) is
|
||||
begin
|
||||
if First then
|
||||
First := False;
|
||||
else
|
||||
Put (',');
|
||||
end if;
|
||||
Put (Element (Position));
|
||||
end Put;
|
||||
begin
|
||||
if List.Length = Max then
|
||||
Iterate (List, Put'Access);
|
||||
New_Line;
|
||||
end if;
|
||||
end Put;
|
||||
|
||||
begin
|
||||
Open (File, In_File, "unixdict.txt");
|
||||
loop
|
||||
declare
|
||||
Word : constant String := Get_Line (File);
|
||||
Key : String (Word'Range) := (others => Character'Last);
|
||||
List : Set;
|
||||
Position : Anagrams.Cursor;
|
||||
begin
|
||||
for I in Word'Range loop
|
||||
for J in Word'Range loop
|
||||
if Key (J) > Word (I) then
|
||||
Key (J + 1..I) := Key (J..I - 1);
|
||||
Key (J) := Word (I);
|
||||
exit;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
Position := Find (Result, Key);
|
||||
if Has_Element (Position) then
|
||||
List := Element (Position);
|
||||
Insert (List, Word);
|
||||
Replace_Element (Result, Position, List);
|
||||
else
|
||||
Insert (List, Word);
|
||||
Include (Result, Key, List);
|
||||
end if;
|
||||
Max := Count_Type'Max (Max, Length (List));
|
||||
end;
|
||||
end loop;
|
||||
exception
|
||||
when End_Error =>
|
||||
Iterate (Result, Put'Access);
|
||||
Close (File);
|
||||
end Words_Of_Equal_Characters;
|
||||
160
Task/Anagrams/C/anagrams-1.c
Normal file
160
Task/Anagrams/C/anagrams-1.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;
|
||||
}
|
||||
31
Task/Anagrams/CoffeeScript/anagrams-1.coffee
Normal file
31
Task/Anagrams/CoffeeScript/anagrams-1.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
|
||||
39
Task/Anagrams/Go/anagrams-1.go
Normal file
39
Task/Anagrams/Go/anagrams-1.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] }
|
||||
15
Task/Anagrams/Perl/anagrams-1.pl
Normal file
15
Task/Anagrams/Perl/anagrams-1.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";
|
||||
}
|
||||
}
|
||||
4
Task/Anagrams/PicoLisp/anagrams-1.l
Normal file
4
Task/Anagrams/PicoLisp/anagrams-1.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(flip
|
||||
(by length sort
|
||||
(by '((L) (sort (copy L))) group
|
||||
(in "unixdict.txt" (make (while (line) (link @)))) ) ) )
|
||||
12
Task/Anagrams/Python/anagrams-1.py
Normal file
12
Task/Anagrams/Python/anagrams-1.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])
|
||||
21
Task/Anagrams/Racket/anagrams-1.rkt
Normal file
21
Task/Anagrams/Racket/anagrams-1.rkt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#lang racket
|
||||
|
||||
(require net/url)
|
||||
|
||||
(define (get-lines url-string)
|
||||
(define port (get-pure-port (string->url url-string)))
|
||||
(for/list ([l (in-lines port)]) l))
|
||||
|
||||
(define (hash-words words)
|
||||
(for/fold ([ws-hash (hash)]) ([w words])
|
||||
(hash-update ws-hash
|
||||
(list->string (sort (string->list w) < #:key (λ (c) (char->integer c))))
|
||||
(λ (ws) (cons w ws))
|
||||
(λ () '()))))
|
||||
|
||||
(define (get-maxes h)
|
||||
(define max-ws (apply max (map length (hash-values h))))
|
||||
(define max-keys (filter (λ (k) (= (length (hash-ref h k)) max-ws)) (hash-keys h)))
|
||||
(map (λ (k) (hash-ref h k)) max-keys))
|
||||
|
||||
(get-maxes (hash-words (get-lines "http://www.puzzlers.org/pub/wordlists/unixdict.txt")))
|
||||
17
Task/Anagrams/Ruby/anagrams-1.rb
Normal file
17
Task/Anagrams/Ruby/anagrams-1.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
|
||||
4
Task/Anagrams/Scala/anagrams-1.scala
Normal file
4
Task/Anagrams/Scala/anagrams-1.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"
|
||||
7
Task/Anagrams/Smalltalk/anagrams-1.st
Normal file
7
Task/Anagrams/Smalltalk/anagrams-1.st
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
list:= (FillInTheBlank request: 'myMessageBoxTitle') subStrings: String crlf.
|
||||
dict:= Dictionary new.
|
||||
list do: [:val|
|
||||
(dict at: val copy sort ifAbsent: [dict at: val copy sort put: OrderedCollection new])
|
||||
add: val.
|
||||
].
|
||||
sorted:=dict asSortedCollection: [:a :b| a size > b size].
|
||||
Loading…
Add table
Add a link
Reference in a new issue