2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,2 +1,12 @@
|
|||
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.
|
||||
When two or more words are composed of the same characters, but in a different order, they are called [[wp:Anagram|anagrams]].
|
||||
|
||||
{{task heading}}
|
||||
|
||||
Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt,
|
||||
<br>find the sets of words that share the same characters that contain the most words in them.
|
||||
|
||||
{{task heading|Related tasks}}
|
||||
|
||||
{{Related tasks/Word plays}}
|
||||
|
||||
<hr>
|
||||
|
|
|
|||
93
Task/Anagrams/ALGOL-68/anagrams.alg
Normal file
93
Task/Anagrams/ALGOL-68/anagrams.alg
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
# find longest list(s) of words that are anagrams in a list of words #
|
||||
# use the associative array in the Associate array/iteration task #
|
||||
PR read "aArray.a68" PR
|
||||
|
||||
# returns the number of occurances of ch in text #
|
||||
PROC count = ( STRING text, CHAR ch )INT:
|
||||
BEGIN
|
||||
INT result := 0;
|
||||
FOR c FROM LWB text TO UPB text DO IF text[ c ] = ch THEN result +:= 1 FI OD;
|
||||
result
|
||||
END # count # ;
|
||||
|
||||
# returns text with the characters sorted into ascending order #
|
||||
PROC char sort = ( STRING text )STRING:
|
||||
BEGIN
|
||||
STRING sorted := text;
|
||||
FOR end pos FROM UPB sorted - 1 BY -1 TO LWB sorted
|
||||
WHILE
|
||||
BOOL swapped := FALSE;
|
||||
FOR pos FROM LWB sorted TO end pos DO
|
||||
IF sorted[ pos ] > sorted[ pos + 1 ]
|
||||
THEN
|
||||
CHAR t := sorted[ pos ];
|
||||
sorted[ pos ] := sorted[ pos + 1 ];
|
||||
sorted[ pos + 1 ] := t;
|
||||
swapped := TRUE
|
||||
FI
|
||||
OD;
|
||||
swapped
|
||||
DO SKIP OD;
|
||||
sorted
|
||||
END # char sort # ;
|
||||
|
||||
# read the list of words and store in an associative array #
|
||||
|
||||
CHAR separator = "|"; # character that will separate the anagrams #
|
||||
|
||||
IF FILE input file;
|
||||
STRING file name = "unixdict.txt";
|
||||
open( input file, file name, stand in channel ) /= 0
|
||||
THEN
|
||||
# failed to open the file #
|
||||
print( ( "Unable to open """ + file name + """", newline ) )
|
||||
ELSE
|
||||
# file opened OK #
|
||||
BOOL at eof := FALSE;
|
||||
# set the EOF handler for the file #
|
||||
on logical file end( input file, ( REF FILE f )BOOL:
|
||||
BEGIN
|
||||
# note that we reached EOF on the #
|
||||
# latest read #
|
||||
at eof := TRUE;
|
||||
# return TRUE so processing can continue #
|
||||
TRUE
|
||||
END
|
||||
);
|
||||
REF AARRAY words := INIT LOC AARRAY;
|
||||
STRING word;
|
||||
WHILE NOT at eof
|
||||
DO
|
||||
STRING word;
|
||||
get( input file, ( word, newline ) );
|
||||
words // char sort( word ) +:= separator + word
|
||||
OD;
|
||||
# close the file #
|
||||
close( input file );
|
||||
|
||||
# find the maximum number of anagrams #
|
||||
|
||||
INT max anagrams := 0;
|
||||
|
||||
REF AAELEMENT e := FIRST words;
|
||||
WHILE e ISNT nil element DO
|
||||
IF INT anagrams := count( value OF e, separator );
|
||||
anagrams > max anagrams
|
||||
THEN
|
||||
max anagrams := anagrams
|
||||
FI;
|
||||
e := NEXT words
|
||||
OD;
|
||||
|
||||
print( ( "Maximum number of anagrams: ", whole( max anagrams, -4 ), newline ) );
|
||||
# show the anagrams with the maximum number #
|
||||
e := FIRST words;
|
||||
WHILE e ISNT nil element DO
|
||||
IF INT anagrams := count( value OF e, separator );
|
||||
anagrams = max anagrams
|
||||
THEN
|
||||
print( ( ( value OF e )[ ( LWB value OF e ) + 1: ], newline ) )
|
||||
FI;
|
||||
e := NEXT words
|
||||
OD
|
||||
FI
|
||||
243
Task/Anagrams/COBOL/anagrams.cobol
Normal file
243
Task/Anagrams/COBOL/anagrams.cobol
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
*> TECTONICS
|
||||
*> wget http://www.puzzlers.org/pub/wordlists/unixdict.txt
|
||||
*> or visit https://sourceforge.net/projects/souptonuts/files
|
||||
*> or snag ftp://ftp.openwall.com/pub/wordlists/all.gz
|
||||
*> for a 5 million all language word file (a few phrases)
|
||||
*> cobc -xj anagrams.cob [-DMOSTWORDS -DMOREWORDS -DALLWORDS]
|
||||
*> ***************************************************************
|
||||
identification division.
|
||||
program-id. anagrams.
|
||||
|
||||
environment division.
|
||||
configuration section.
|
||||
repository.
|
||||
function all intrinsic.
|
||||
|
||||
input-output section.
|
||||
file-control.
|
||||
select words-in
|
||||
assign to wordfile
|
||||
organization is line sequential
|
||||
status is words-status
|
||||
.
|
||||
|
||||
REPLACE ==:LETTERS:== BY ==42==.
|
||||
|
||||
data division.
|
||||
file section.
|
||||
fd words-in record is varying from 1 to :LETTERS: characters
|
||||
depending on word-length.
|
||||
01 word-record.
|
||||
05 word-data pic x occurs 0 to :LETTERS: times
|
||||
depending on word-length.
|
||||
|
||||
working-storage section.
|
||||
>>IF ALLWORDS DEFINED
|
||||
01 wordfile constant as "/usr/local/share/dict/all.words".
|
||||
01 max-words constant as 4802100.
|
||||
|
||||
>>ELSE-IF MOSTWORDS DEFINED
|
||||
01 wordfile constant as "/usr/local/share/dict/linux.words".
|
||||
01 max-words constant as 628000.
|
||||
|
||||
>>ELSE-IF MOREWORDS DEFINED
|
||||
01 wordfile constant as "/usr/share/dict/words".
|
||||
01 max-words constant as 100000.
|
||||
|
||||
>>ELSE
|
||||
01 wordfile constant as "unixdict.txt".
|
||||
01 max-words constant as 26000.
|
||||
>>END-IF
|
||||
|
||||
*> The 5 million word file needs to restrict the word length
|
||||
>>IF ALLWORDS DEFINED
|
||||
01 max-letters constant as 26.
|
||||
>>ELSE
|
||||
01 max-letters constant as :LETTERS:.
|
||||
>>END-IF
|
||||
|
||||
01 word-length pic 99 comp-5.
|
||||
01 words-status pic xx.
|
||||
88 ok-status values '00' thru '09'.
|
||||
88 eof-status value '10'.
|
||||
|
||||
*> sortable word by letter table
|
||||
01 letter-index usage index.
|
||||
01 letter-table.
|
||||
05 letters occurs 1 to max-letters times
|
||||
depending on word-length
|
||||
ascending key letter
|
||||
indexed by letter-index.
|
||||
10 letter pic x.
|
||||
|
||||
*> table of words
|
||||
01 sorted-index usage index.
|
||||
01 word-table.
|
||||
05 word-list occurs 0 to max-words times
|
||||
depending on word-tally
|
||||
ascending key sorted-word
|
||||
indexed by sorted-index.
|
||||
10 match-count pic 999 comp-5.
|
||||
10 this-word pic x(max-letters).
|
||||
10 sorted-word pic x(max-letters).
|
||||
01 sorted-display pic x(10).
|
||||
|
||||
01 interest-table.
|
||||
05 interest-list pic 9(8) comp-5
|
||||
occurs 0 to max-words times
|
||||
depending on interest-tally.
|
||||
|
||||
01 outer pic 9(8) comp-5.
|
||||
01 inner pic 9(8) comp-5.
|
||||
01 starter pic 9(8) comp-5.
|
||||
01 ender pic 9(8) comp-5.
|
||||
01 word-tally pic 9(8) comp-5.
|
||||
01 interest-tally pic 9(8) comp-5.
|
||||
01 tally-display pic zz,zzz,zz9.
|
||||
|
||||
01 most-matches pic 99 comp-5.
|
||||
01 matches pic 99 comp-5.
|
||||
01 match-display pic z9.
|
||||
|
||||
*> timing display
|
||||
01 time-stamp.
|
||||
05 filler pic x(11).
|
||||
05 timer-hours pic 99.
|
||||
05 filler pic x.
|
||||
05 timer-minutes pic 99.
|
||||
05 filler pic x.
|
||||
05 timer-seconds pic 99.
|
||||
05 filler pic x.
|
||||
05 timer-subsec pic v9(6).
|
||||
01 timer-elapsed pic 9(6)v9(6).
|
||||
01 timer-value pic 9(6)v9(6).
|
||||
01 timer-display pic zzz,zz9.9(6).
|
||||
|
||||
*> ***************************************************************
|
||||
procedure division.
|
||||
main-routine.
|
||||
|
||||
>>IF ALLWORDS DEFINED
|
||||
display "** Words limited to " max-letters " letters **"
|
||||
>>END-IF
|
||||
|
||||
perform show-time
|
||||
|
||||
perform load-words
|
||||
perform find-most
|
||||
perform display-result
|
||||
|
||||
perform show-time
|
||||
goback
|
||||
.
|
||||
|
||||
*> ***************************************************************
|
||||
load-words.
|
||||
open input words-in
|
||||
if not ok-status then
|
||||
display "error opening " wordfile upon syserr
|
||||
move 1 to return-code
|
||||
goback
|
||||
end-if
|
||||
|
||||
perform until exit
|
||||
read words-in
|
||||
if eof-status then exit perform end-if
|
||||
if not ok-status then
|
||||
display wordfile " read error: " words-status upon syserr
|
||||
end-if
|
||||
|
||||
if word-length equal zero then exit perform cycle end-if
|
||||
|
||||
>>IF ALLWORDS DEFINED
|
||||
move min(word-length, max-letters) to word-length
|
||||
>>END-IF
|
||||
|
||||
add 1 to word-tally
|
||||
move word-record to this-word(word-tally) letter-table
|
||||
sort letters ascending key letter
|
||||
move letter-table to sorted-word(word-tally)
|
||||
end-perform
|
||||
|
||||
move word-tally to tally-display
|
||||
display trim(tally-display) " words" with no advancing
|
||||
|
||||
close words-in
|
||||
if not ok-status then
|
||||
display "error closing " wordfile upon syserr
|
||||
move 1 to return-code
|
||||
end-if
|
||||
|
||||
*> sort word list by anagram check field
|
||||
sort word-list ascending key sorted-word
|
||||
.
|
||||
|
||||
*> first entry in a list will end up with highest match count
|
||||
find-most.
|
||||
perform varying outer from 1 by 1 until outer > word-tally
|
||||
move 1 to matches
|
||||
add 1 to outer giving starter
|
||||
perform varying inner from starter by 1
|
||||
until sorted-word(inner) not equal sorted-word(outer)
|
||||
add 1 to matches
|
||||
end-perform
|
||||
if matches > most-matches then
|
||||
move matches to most-matches
|
||||
initialize interest-table all to value
|
||||
move 0 to interest-tally
|
||||
end-if
|
||||
move matches to match-count(outer)
|
||||
if matches = most-matches then
|
||||
add 1 to interest-tally
|
||||
move outer to interest-list(interest-tally)
|
||||
end-if
|
||||
end-perform
|
||||
.
|
||||
|
||||
*> only display the words with the most anagrams
|
||||
display-result.
|
||||
move interest-tally to tally-display
|
||||
move most-matches to match-display
|
||||
display ", most anagrams: " trim(match-display)
|
||||
", with " trim(tally-display) " set" with no advancing
|
||||
if interest-tally not equal 1 then
|
||||
display "s" with no advancing
|
||||
end-if
|
||||
display " of interest"
|
||||
|
||||
perform varying outer from 1 by 1 until outer > interest-tally
|
||||
move sorted-word(interest-list(outer)) to sorted-display
|
||||
display sorted-display
|
||||
" [" trim(this-word(interest-list(outer)))
|
||||
with no advancing
|
||||
add 1 to interest-list(outer) giving starter
|
||||
add most-matches to interest-list(outer) giving ender
|
||||
perform varying inner from starter by 1
|
||||
until inner = ender
|
||||
display ", " trim(this-word(inner))
|
||||
with no advancing
|
||||
end-perform
|
||||
display "]"
|
||||
end-perform
|
||||
.
|
||||
|
||||
*> elapsed time
|
||||
show-time.
|
||||
move formatted-current-date("YYYY-MM-DDThh:mm:ss.ssssss")
|
||||
to time-stamp
|
||||
compute timer-value = timer-hours * 3600 + timer-minutes * 60
|
||||
+ timer-seconds + timer-subsec
|
||||
if timer-elapsed = 0 then
|
||||
display time-stamp
|
||||
move timer-value to timer-elapsed
|
||||
else
|
||||
if timer-value < timer-elapsed then
|
||||
add 86400 to timer-value
|
||||
end-if
|
||||
subtract timer-elapsed from timer-value
|
||||
move timer-value to timer-display
|
||||
display time-stamp ", " trim(timer-display) " seconds"
|
||||
end-if
|
||||
.
|
||||
|
||||
end program anagrams.
|
||||
14
Task/Anagrams/Ela/anagrams.ela
Normal file
14
Task/Anagrams/Ela/anagrams.ela
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
open monad io list string
|
||||
|
||||
groupon f x y = f x == f y
|
||||
|
||||
lines = split "\n" << replace "\n\n" "\n" << replace "\r" "\n"
|
||||
|
||||
main = do
|
||||
fh <- readFile "c:\\test\\unixdict.txt" OpenMode
|
||||
f <- readLines fh
|
||||
closeFile fh
|
||||
let words = lines f
|
||||
let wix = groupBy (groupon fst) << sort $ zip (map sort words) words
|
||||
let mxl = maximum $ map length wix
|
||||
mapM_ (putLn << map snd) << filter ((==mxl) << length) $ wix
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
#define system.
|
||||
#define system'routines.
|
||||
#define system'io.
|
||||
#define system'collections.
|
||||
#define extensions.
|
||||
#define extensions'routines.
|
||||
#import system.
|
||||
#import system'routines.
|
||||
#import system'io.
|
||||
#import system'collections.
|
||||
#import extensions.
|
||||
#import extensions'routines.
|
||||
|
||||
#class(extension) op
|
||||
{
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
[
|
||||
#var aDictionary := Dictionary new.
|
||||
|
||||
File new &path:"unixdict.txt" run &eachLine: aWord
|
||||
"unixdict.txt" file_path run &eachLine: aWord
|
||||
[
|
||||
#var aKey := aWord normalized.
|
||||
#var anItem := aDictionary@aKey.
|
||||
|
|
|
|||
|
|
@ -2,24 +2,15 @@ defmodule Anagrams do
|
|||
def find(file) do
|
||||
File.read!(file)
|
||||
|> String.split
|
||||
|> Enum.map(&String.codepoints &1)
|
||||
|> sort(%{})
|
||||
|> Enum.group_by(fn word -> String.codepoints(word) |> Enum.sort end)
|
||||
|> Enum.group_by(fn {_,v} -> length(v) end)
|
||||
|> Enum.max
|
||||
|> print
|
||||
end
|
||||
|
||||
defp sort([],m), do: m
|
||||
defp sort([word|words],m) do
|
||||
s = Enum.sort(word)
|
||||
m = Dict.update(m, s, [word], fn val -> [word|val] end)
|
||||
sort(words,m)
|
||||
end
|
||||
|
||||
defp print({_,y}) do
|
||||
Enum.each(y, fn {_,e} ->
|
||||
Enum.map(e, &Enum.join(&1)) |> Enum.sort |> Enum.join(" ") |> IO.puts
|
||||
end)
|
||||
Enum.each(y, fn {_,e} -> Enum.sort(e) |> Enum.join(" ") |> IO.puts end)
|
||||
end
|
||||
end
|
||||
|
||||
Anagrams.find("unixdict.txt")
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
File.stream!("unixdict.txt")
|
||||
|> Stream.map(&String.strip &1)
|
||||
|> Stream.map(&{&1, &1 |> String.codepoints |> Enum.sort |> Enum.join})
|
||||
|> Enum.group_by(fn {_,y} -> y end)
|
||||
|> Dict.values
|
||||
|> Enum.group_by(&length(&1))
|
||||
|> Enum.group_by(&String.codepoints(&1) |> Enum.sort)
|
||||
|> Map.values
|
||||
|> Enum.group_by(&length &1)
|
||||
|> Enum.max
|
||||
|> elem(1)
|
||||
|> Enum.each(fn n -> Enum.map(n, fn {y,_} -> y end)
|
||||
|> Enum.sort
|
||||
|> Enum.join(" ")
|
||||
|> IO.puts
|
||||
end)
|
||||
|> Enum.each(fn n -> Enum.sort(n) |> Enum.join(" ") |> IO.puts end)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
url = "http://www.puzzlers.org/pub/wordlists/unixdict.txt"
|
||||
|
||||
wordlist = map!(chomp,(open(readlines, download(url)))) ;
|
||||
wordlist = map!(chomp,(open(readlines, download(url))))
|
||||
|
||||
wsort(word) = join(sort(collect(word)))
|
||||
|
||||
function anagram(wordlist)
|
||||
hash = Dict() ; ananum = 0
|
||||
for word in wordlist
|
||||
sorted = CharString(sort(collect(word.data)))
|
||||
hash[sorted] = [ get(hash, sorted, []), word ]
|
||||
sorted = wsort(word)
|
||||
hash[sorted] = [ get(hash, sorted, []); word ]
|
||||
ananum = max(length(hash[sorted]), ananum)
|
||||
end
|
||||
collect(values(filter((x,y)-> length(y) == ananum, hash)))
|
||||
|
|
|
|||
2
Task/Anagrams/Mathematica/anagrams-5.math
Normal file
2
Task/Anagrams/Mathematica/anagrams-5.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
list=Import["http://www.puzzlers.org/pub/wordlists/unixdict.txt","Lines"];
|
||||
MaximalBy[GatherBy[list, Sort@*Characters], Length]
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
my %anagram = slurp('unixdict.txt').words.classify( { .comb.sort.join } );
|
||||
my @anagrams = 'unixdict.txt'.IO.words.classify(*.comb.sort.join).values;
|
||||
|
||||
my $max = [max] map { +@($_) }, %anagram.values;
|
||||
my $max = @anagrams».elems.max;
|
||||
|
||||
%anagram.values.grep( { +@($_) >= $max } )».join(' ')».say;
|
||||
.put for @anagrams.grep(*.elems == $max);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
.say for # print each element of the array made this way:
|
||||
slurp('unixdict.txt')\ # load file in memory
|
||||
.words\ # extract words
|
||||
.classify( *.comb.sort.join )\ # group by common anagram
|
||||
.classify( *.value.elems )\ # group by number of anagrams in a group
|
||||
.max( :by(*.key) ).value\ # get the group with highest number of anagrams
|
||||
».value # get all groups of anagrams in the group just selected
|
||||
.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
|
||||
|
|
|
|||
|
|
@ -1,30 +1,30 @@
|
|||
/*REXX program finds words with the largest set of anagrams (of the same size)*/
|
||||
iFID='unixdict.txt' /*the dictionary input File IDentifier.*/
|
||||
$=; !.=; #.=0; w=0; uw=0; most=0 /*initialize a bunch of REXX variables.*/
|
||||
/* [↓] read the entire file (by lines)*/
|
||||
do while lines(iFID)\==0 /*Got any data? Then read a record. */
|
||||
@=space(linein(iFID),0) /*pick off a word from the input line. */
|
||||
L=length(@); if L<3 then iterate /*onesies and twosies words can't win. */
|
||||
if \datatype(@,'M') then iterate /*ignore any non─anagramable words. */
|
||||
uw=uw+1 /*count of the (useable) words in file.*/
|
||||
z=sortA(@) /*sort the letters in the word. */
|
||||
!.z=!.z @; #.z=#.z+1 /*append it to !.z; bump the counter. */
|
||||
/*REXX program finds words with the largest set of anagrams (of the same size). */
|
||||
iFID= 'unixdict.txt' /*the dictionary input File IDentifier.*/
|
||||
$=; !.=; #.=0; w=0; uw=0; most=0 /*initialize a bunch of REXX variables.*/
|
||||
/* [↓] read the entire file (by lines)*/
|
||||
do while lines(iFID)\==0 /*Got any data? Then read a record. */
|
||||
@=space( linein(iFID), 0) /*pick off a word from the input line. */
|
||||
L=length(@); if L<3 then iterate /*onesies and twosies words can't win. */
|
||||
if \datatype(@, 'M') then iterate /*ignore any non─anagramable words. */
|
||||
uw=uw+1 /*count of the (useable) words in file.*/
|
||||
z=sortA(@) /*sort the letters in the word. */
|
||||
!.z=!.z @; #.z=#.z+1 /*append it to !.z; bump the counter. */
|
||||
if #.z>most then do; $=z; most=#.z; if L>w then w=L; iterate; end
|
||||
if #.z==most then $=$ z /*append the sorted word──◄ max anagram*/
|
||||
end /*while*/ /*$ ►── list of high count anagrams. */
|
||||
say '─────────────────────────' uw 'useable words in the dictionary file: ' iFID
|
||||
if #.z==most then $=$ z /*append the sorted word──► max anagram*/
|
||||
end /*while*/ /*$ ◄── list of high count anagrams. */
|
||||
say '─────────────────────────' uw "useable words in the dictionary file: " iFID
|
||||
say
|
||||
do m=1 for words($); z=subword($,m,1) /*high count of anagrams.*/
|
||||
say ' ' left(subword(!.z,1,1),w) ' [anagrams: ' subword(!.z,2)"]"
|
||||
end /*m*/ /*W is the maximum width of any word.*/
|
||||
do m=1 for words($); z=subword($, m, 1) /*the high count of the anagrams. */
|
||||
say ' ' left(subword(!.z, 1, 1), w) ' [anagrams: ' subword(!.z, 2)"]"
|
||||
end /*m*/ /*W is the maximum width of any word.*/
|
||||
say
|
||||
say '───── Found' words($) "words (each of which have" #.z-1 'anagrams).'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────SORTA subroutine──────────────────────────*/
|
||||
sortA: procedure; arg char +1 xx,@. /*get the first letter of arg; @.=null*/
|
||||
@.char=char /*no need to concatenate the first char*/
|
||||
/*[↓] sort/put letters alphabetically.*/
|
||||
do length(xx); parse var xx char +1 xx; @.char=@.char || char; end
|
||||
/*reassemble word with sorted letters. */
|
||||
return @.a||@.b||@.c||@.d||@.e||@.f||@.g||@.h||@.i||@.j||@.k||@.l||@.m||,
|
||||
@.n||@.o||@.p||@.q||@.r||@.s||@.t||@.u||@.v||@.w||@.x||@.y||@.z
|
||||
say '───── Found' words($) "words (each of which have" #.z-1 'anagrams).'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sortA: procedure; arg char +1 xx,@. /*get the first letter of arg; @.=null*/
|
||||
@.char=char /*no need to concatenate the first char*/
|
||||
/*[↓] sort/put letters alphabetically.*/
|
||||
do length(xx); parse var xx char +1 xx; @.char=@.char || char; end
|
||||
/*reassemble word with sorted letters. */
|
||||
return @.a||@.b||@.c||@.d||@.e||@.f||@.g||@.h||@.i||@.j||@.k||@.l||@.m||,
|
||||
@.n||@.o||@.p||@.q||@.r||@.s||@.t||@.u||@.v||@.w||@.x||@.y||@.z
|
||||
|
|
|
|||
|
|
@ -1,28 +1,27 @@
|
|||
/*REXX program finds words with the largest set of anagrams (of the same size)*/
|
||||
iFID='unixdict.txt' /*the dictionary input File IDentifier.*/
|
||||
$=; !.=; #.=0; ww=0; uw=0; most=0 /*initialize a bunch of REXX variables.*/
|
||||
/* [↓] read the entire file (by lines)*/
|
||||
do while lines(iFID)\==0 /*Got any data? Then read a record. */
|
||||
@=space(linein(iFID),0) /*pick off a word from the input line. */
|
||||
LL=length(@); if LL<3 then iterate /*onesies and twosies (words) can't win*/
|
||||
if \datatype(@,'M') then iterate /*ignore any non─anagramable words. */
|
||||
uw=uw+1 /*count of the (useable) words in file.*/
|
||||
parse upper var @ _ +1 xx @. /*get uppercase @ and nullify @. */
|
||||
@._=_ /*get the first letter (special case). */
|
||||
/*[↓] sort/put letters alphabetically.*/
|
||||
do LL-1; parse var xx _ +1 xx; @._=@._||_; end /*get rest of word.*/
|
||||
/*reassemble word with sorted letters. */
|
||||
/*REXX program finds words with the largest set of anagrams (of the same size). */
|
||||
iFID= 'unixdict.txt' /*the dictionary input File IDentifier.*/
|
||||
$=; !.=; #.=0; ww=0; uw=0; most=0 /*initialize a bunch of REXX variables.*/
|
||||
/* [↓] read the entire file (by lines)*/
|
||||
do while lines(iFID)\==0 /*Got any data? Then read a record. */
|
||||
@=space( linein(iFID), 0) /*pick off a word from the input line. */
|
||||
LL=length(@); if LL<3 then iterate /*onesies and twosies (words) can't win*/
|
||||
if \datatype(@, 'M') then iterate /*ignore any non─anagramable words. */
|
||||
uw=uw+1 /*count of the (useable) words in file.*/
|
||||
parse upper var @ _ +1 xx @. /*get uppercase @ and nullify @. */
|
||||
@._=_ /*get the first letter (special case). */
|
||||
/*[↓] sort/put letters alphabetically.*/
|
||||
do LL-1; parse var xx _ +1 xx; @._=@._||_; end /*get the rest of the word.*/
|
||||
/*reassemble word with sorted letters. */
|
||||
zz=@.a||@.b||@.c||@.d||@.e||@.f||@.g||@.h||@.i||@.j||@.k||@.l||@.m||,
|
||||
@.n||@.o||@.p||@.q||@.r||@.s||@.t||@.u||@.v||@.w||@.x||@.y||@.z
|
||||
!.zz=!.zz @; #.zz=#.zz+1 /*append it to !.zz; bump the counter.*/
|
||||
if #.zz>most then do; $=zz; most=#.zz; if LL>ww then ww=LL; iterate; end
|
||||
if #.zz==most then $=$ zz /*append the sorted word──◄ $ anagrams.*/
|
||||
!.zz=!.zz @; #.zz=#.zz+1 /*append it to !.zz; bump the counter.*/
|
||||
if #.zz>most then do; $=zz; most=#.zz; if LL>ww then ww=LL; iterate; end
|
||||
if #.zz==most then $=$ zz /*append the sorted word──► $ anagrams.*/
|
||||
end /*while*/
|
||||
say '─────────────────────────' uw 'useable words in the dictionary file: ' iFID
|
||||
say '─────────────────────────' uw "useable words in the dictionary file: " iFID
|
||||
say
|
||||
do m=1 for words($); z=subword($,m,1) /*high count of anagrams.*/
|
||||
say ' ' left(subword(!.z,1,1),ww) ' [anagrams: ' subword(!.z,2)"]"
|
||||
end /*m*/ /*WW is the maximum width of any word.*/
|
||||
say
|
||||
say '───── Found' words($) "words (each of which have" #.z-1 'anagrams).'
|
||||
/*stick a fork in it, we're all done. */
|
||||
do m=1 for words($); z=subword($,m,1) /*the high count of the anagrams. */
|
||||
say ' ' left(subword(!.z, 1, 1), ww) " [anagrams: " subword(!.z,2)"]"
|
||||
end /*m*/ /*WW is the maximum width of any word.*/
|
||||
say /*stick a fork in it, we're all done. */
|
||||
say '───── Found' words($) "words (each of which have" #.z-1 'anagrams).'
|
||||
|
|
|
|||
|
|
@ -1,28 +1,27 @@
|
|||
/*REXX program finds words with the largest set of anagrams (of the same size)*/
|
||||
iFID='unixdict.txt' /*the dictionary input File IDentifier.*/
|
||||
$=; !.=; #.=0; ww=0; uw=0; most=0 /*initialize a bunch of REXX variables.*/
|
||||
/* [↓] read the entire file (by lines)*/
|
||||
do while lines(iFID)\==0 /*Got any data? Then read a record. */
|
||||
@=space(linein(iFID),0) /*pick off a word from the input line. */
|
||||
LL=length(@); if LL<3 then iterate /*onesies and twosies (words) can't win*/
|
||||
if \datatype(@,'M') then iterate /*ignore any non─anagramable words. */
|
||||
uw=uw+1 /*count of the (useable) words in file.*/
|
||||
parse upper var @ _ +1 xx '' @. /*get uppercase @ and nullify @. */
|
||||
@._=_ /*get the first letter (special case). */
|
||||
/*[↓] sort/put letters alphabetically.*/
|
||||
do LL-1; parse var xx _ +1 xx; @._=@._||_; end /*get rest of word.*/
|
||||
/*reassemble word with sorted letters. */
|
||||
zz=@.a||@.b||@.c||@.d||@.e||@.f||@.g||@.h||@.i||@.j||@.k||@.l||@.m||,
|
||||
@.n||@.o||@.p||@.q||@.r||@.s||@.t||@.u||@.v||@.w||@.x||@.y||@.z
|
||||
!.zz=!.zz @; #.zz=#.zz+1 /*append it to !.zz; bump the counter.*/
|
||||
if #.zz>most then do; $=zz; most=#.zz; if LL>ww then ww=LL; iterate; end
|
||||
if #.zz==most then $=$ zz /*append the sorted word──► $ anagrams.*/
|
||||
/*REXX program finds words with the largest set of anagrams (of the same size). */
|
||||
iFID= 'unixdict.txt' /*the dictionary input File IDentifier.*/
|
||||
$=; !.=; #.=0; ww=0; uw=0; most=0 /*initialize a bunch of REXX variables.*/
|
||||
/* [↓] read the entire file (by lines)*/
|
||||
do while lines(iFID)\==0 /*Got any data? Then read a record. */
|
||||
@=space( linein(iFID), 0) /*pick off a word from the input line. */
|
||||
LL=length(@); if LL<3 then iterate /*onesies and twosies (words) can't win*/
|
||||
if \datatype(@, 'M') then iterate /*ignore any non─anagramable words. */
|
||||
uw=uw+1 /*count of the (useable) words in file.*/
|
||||
parse upper var @ _ +1 xx . @. /*get uppercase @ and nullify @. */
|
||||
@._=_ /*get the first letter (special case). */
|
||||
/*[↓] sort/put letters alphabetically.*/
|
||||
do LL-1; parse var xx _ +1 xx; @._=@._ || _; end /*get rest of the word*/
|
||||
/*reassemble word with sorted letters. */
|
||||
zz=@.a|| @.b|| @.c|| @.d|| @.e|| @.f|| @.g|| @.h|| @.i|| @.j|| @.k|| @.l|| @.m ||,
|
||||
@.n|| @.o|| @.p|| @.q|| @.r|| @.s|| @.t|| @.u|| @.v|| @.w|| @.x|| @.y|| @.z
|
||||
!.zz=!.zz @; #.zz=#.zz+1 /*append it to !.zz; bump the counter.*/
|
||||
if #.zz>most then do; $=zz; most=#.zz; if LL>ww then ww=LL; iterate; end
|
||||
if #.zz==most then $=$ zz /*append the sorted word──► $ anagrams.*/
|
||||
end /*while*/
|
||||
say '─────────────────────────' uw 'useable words in the dictionary file: ' iFID
|
||||
say '─────────────────────────' uw 'useable words in the dictionary file: ' iFID
|
||||
say
|
||||
do m=1 for words($); z=subword($,m,1) /*high count of anagrams.*/
|
||||
say ' ' left(subword(!.z,1,1),ww) ' [anagrams: ' subword(!.z,2)"]"
|
||||
end /*m*/ /*WW is the maximum width of any word.*/
|
||||
say
|
||||
say '───── Found' words($) "words (each of which have" #.z-1 'anagrams).'
|
||||
/*stick a fork in it, we're all done. */
|
||||
do m=1 for words($); z=subword($, m, 1) /*the high count of the anagrams. */
|
||||
say ' ' left(subword(!.z, 1, 1), ww) " [anagrams: " subword(!.z, 2)']'
|
||||
end /*m*/ /*WW is the maximum width of any word.*/
|
||||
say /*stick a fork in it, we're all done. */
|
||||
say '───── Found' words($) "words (each of which have" #.z-1 'anagrams).'
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
u='Halloween' /*the word to be sorted by letter*/
|
||||
upper u /*fast method to uppercase a var.*/
|
||||
/*another: u = translate(u) */
|
||||
/*another: parse upper var u u */
|
||||
/*another: u = upper(u) */
|
||||
/*not always available [↑] */
|
||||
u= 'Halloween' /*word to be sorted by (Latin) letter.*/
|
||||
upper u /*fast method to uppercase a variable. */
|
||||
/*another: u = translate(u) */
|
||||
/*another: parse upper var u u */
|
||||
/*another: u = upper(u) */
|
||||
/*not always available [↑] */
|
||||
say 'u=' u
|
||||
_.=
|
||||
do until u=='' /*keep truckin' until U is null.*/
|
||||
parse var u y +1 u /*get the next (first) char in U.*/
|
||||
xx = '?'y /*assign a prefixed char to XX. */
|
||||
_.xx = _.xx || y /*append it to all the Y chars.*/
|
||||
end /*until*/ /*U now has the first char gone.*/
|
||||
/*Note: the var U is destroyed.*/
|
||||
do until u=='' /*keep truckin' until U is null. */
|
||||
parse var u y +1 u /*get the next (first) character in U.*/
|
||||
xx='?'y /*assign a prefixed character to XX. */
|
||||
_.xx=_.xx || y /*append it to all the Y characters. */
|
||||
end /*until*/ /*U now has the first character elided.*/
|
||||
/*Note: the variable U is destroyed.*/
|
||||
|
||||
/* [↓] build sorted letter word. */
|
||||
/* [↓] constructs a sorted letter word*/
|
||||
|
||||
z=_.?a||_.?b||_.?c||_.?d||_.?e||_.?f||_.?g||_.?h||_.?i||_.?j||_.?k||_.?l||_.?m||,
|
||||
_.?n||_.?o||_.?p||_.?q||_.?r||_.?s||_.?t||_.?u||_.?v||_.?w||_.?x||_.?y||_.?z
|
||||
|
||||
/*Note: the ? is prefixed to the letter to avoid */
|
||||
/*collisions with other REXX one-character variables.*/
|
||||
/*Note: the ? is prefixed to the letter to avoid */
|
||||
/*collisions with other REXX one-character variables.*/
|
||||
say 'z=' z
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
u='Halloween' /*the word to be sorted by letter*/
|
||||
upper u /*fast method to uppercase a var.*/
|
||||
L=length(u) /*get the length of the word. */
|
||||
u= 'Halloween' /*word to be sorted by (Latin) letter.*/
|
||||
upper u /*fast method to uppercase a variable. */
|
||||
L=length(u) /*get the length of the word (in bytes)*/
|
||||
say 'u=' u
|
||||
say 'L=' L
|
||||
_.=
|
||||
do k=1 for L /*keep truckin' for L chars. */
|
||||
parse var u =(k) y +1 /*get Kth character in U string. */
|
||||
xx = '?'y /*assign a prefixed char to XX. */
|
||||
_.xx = _.xx || y /*append it to all the Y chars.*/
|
||||
end /*do k*/ /*U now has the first char gone.*/
|
||||
do k=1 for L /*keep truckin' for L characters. */
|
||||
parse var u =(k) y +1 /*get the Kth character in U string.*/
|
||||
xx='?'y /*assign a prefixed character to XX. */
|
||||
_.xx=_.xx || y /*append it to all the Y characters. */
|
||||
end /*do k*/ /*U now has the first character elided.*/
|
||||
|
||||
/* [↓] build sorted letter word. */
|
||||
/* [↓] construct a sorted letter word.*/
|
||||
|
||||
z=_.?a||_.?b||_.?c||_.?d||_.?e||_.?f||_.?g||_.?h||_.?i||_.?j||_.?k||_.?l||_.?m||,
|
||||
_.?n||_.?o||_.?p||_.?q||_.?r||_.?s||_.?t||_.?u||_.?v||_.?w||_.?x||_.?y||_.?z
|
||||
|
|
|
|||
|
|
@ -1,67 +1,64 @@
|
|||
a$ = httpGet$("http://www.puzzlers.org/pub/wordlists/unixdict.txt") ' get the words from this web
|
||||
|
||||
sqliteconnect #mem, ":memory:" ' create in memory DB
|
||||
#mem execute("CREATE TABLE words(theWord,sortWord)")
|
||||
|
||||
ii = 1
|
||||
while ii
|
||||
jj = instr(a$,chr$(10),ii + 1)
|
||||
if jj > 0 then
|
||||
theWord$ = mid$(a$,ii, jj - ii) ' get each word
|
||||
|
||||
if instr(theWord$,"'") <> 0 then theWord$ = dblQuote$(theWord$) ' eclipse the single quote
|
||||
sortWord$ = theWord$
|
||||
' ------------------------------------
|
||||
' Sort word using the ol bubble sort
|
||||
' ------------------------------------
|
||||
j = 1
|
||||
while j
|
||||
j = 0
|
||||
for i = 1 to len(sortWord$) - 1
|
||||
if mid$(sortWord$,i,1) > mid$(sortWord$,i + 1,1) then
|
||||
sortWord$ = left$(sortWord$,i - 1) + mid$(sortWord$,i + 1,1) + mid$(sortWord$,i,1) + mid$(sortWord$,i + 2)
|
||||
j = 1
|
||||
end if
|
||||
next i
|
||||
wend
|
||||
' ----------------------------
|
||||
' place in memory sql table
|
||||
' ----------------------------
|
||||
#mem execute("INSERT INTO words VALUES('";theWord$;"','";sortWord$;"')")
|
||||
end if
|
||||
ii = jj + 1
|
||||
wend
|
||||
|
||||
' -----------------------------------------------------------
|
||||
' Select matched words in word order and print in html table
|
||||
' -----------------------------------------------------------
|
||||
html "<table border=1>"
|
||||
mem$ = "SELECT words.theWord,
|
||||
matchWords.theWord as mWord
|
||||
FROM words
|
||||
JOIN words as matchWords
|
||||
ON matchWords.sortWord = words.sortWord
|
||||
AND matchWOrds.theWord <> words.theWord
|
||||
ORDER BY words.theWord"
|
||||
sqliteconnect #mem, ":memory:"
|
||||
mem$ = "CREATE TABLE anti(gram,ordr);
|
||||
CREATE INDEX ord ON anti(ordr)"
|
||||
#mem execute(mem$)
|
||||
WHILE #mem hasanswer()
|
||||
#row = #mem #nextrow()
|
||||
theWord$ = #row theWord$()
|
||||
mWord$ = #row mWord$()
|
||||
html "<tr><td>";theWord$;"</td><td>";mWord$;"</td></tr>"
|
||||
WEND
|
||||
html "</table>"
|
||||
end
|
||||
' read the file
|
||||
a$ = httpGet$("http://www.puzzlers.org/pub/wordlists/unixdict.txt")
|
||||
|
||||
' -----------------------------------------
|
||||
' Convert single quotes to double quotes
|
||||
' -----------------------------------------
|
||||
FUNCTION dblQuote$(str$)
|
||||
i = 1
|
||||
qq$ = ""
|
||||
while (word$(str$,i,"'")) <> ""
|
||||
dblQuote$ = dblQuote$;qq$;word$(str$,i,"'")
|
||||
qq$ = "''"
|
||||
i = i + 1
|
||||
WEND
|
||||
END FUNCTION
|
||||
' break the file words apart
|
||||
i = 1
|
||||
while i <> 0
|
||||
j = instr(a$,chr$(10),i+1)
|
||||
if j = 0 then exit while
|
||||
a1$ = mid$(a$,i,j-i)
|
||||
q = instr(a1$,"'")
|
||||
if q > 0 then a1$ = left$(a1$,q) + mid$(a1$,q)
|
||||
ln = len(a1$)
|
||||
s$ = a1$
|
||||
|
||||
' Split the characters of the word and sort them
|
||||
s = 1
|
||||
while s = 1
|
||||
s = 0
|
||||
for k = 1 to ln -1
|
||||
if mid$(s$,k,1) > mid$(s$,k+1,1) then
|
||||
h$ = mid$(s$,k,1)
|
||||
h1$ = mid$(s$,k+1,1)
|
||||
s$ = left$(s$,k-1) + h1$ + h$ + mid$(s$,k+2)
|
||||
s = 1
|
||||
end if
|
||||
next k
|
||||
wend
|
||||
|
||||
mem$ = "INSERT INTO anti VALUES('";a1$;"','";ord$;"')"
|
||||
#mem execute(mem$)
|
||||
i = j +1
|
||||
wend
|
||||
' find all antigrams
|
||||
mem$ = "SELECT count(*) as cnt,anti.ordr FROM anti GROUP BY ordr ORDER BY cnt desc"
|
||||
#mem execute(mem$)
|
||||
numDups = #mem ROWCOUNT() 'Get the number of rows
|
||||
dim dups$(numDups)
|
||||
for i = 1 to numDups
|
||||
#row = #mem #nextrow()
|
||||
cnt = #row cnt()
|
||||
if i = 1 then maxCnt = cnt
|
||||
if cnt < maxCnt then exit for
|
||||
dups$(i) = #row ordr$()
|
||||
next i
|
||||
|
||||
for i = 1 to i -1
|
||||
mem$ = "SELECT anti.gram FROM anti
|
||||
WHERE anti.ordr = '";dups$(i);"'
|
||||
ORDER BY anti.gram"
|
||||
#mem execute(mem$)
|
||||
rows = #mem ROWCOUNT() 'Get the number of rows
|
||||
|
||||
for ii = 1 to rows
|
||||
#row = #mem #nextrow()
|
||||
gram$ = #row gram$()
|
||||
print gram$;chr$(9);
|
||||
next ii
|
||||
print
|
||||
next i
|
||||
end
|
||||
|
|
|
|||
20
Task/Anagrams/SuperCollider/anagrams-1.supercollider
Normal file
20
Task/Anagrams/SuperCollider/anagrams-1.supercollider
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(
|
||||
var text, words, sorted, dict = IdentityDictionary.new, findMax;
|
||||
File.use("unixdict.txt".resolveRelative, "r", { |f| text = f.readAllString });
|
||||
words = text.split(Char.nl);
|
||||
sorted = words.collect { |each|
|
||||
var key = each.copy.sort.asSymbol;
|
||||
dict[key] ?? { dict[key] = [] };
|
||||
dict[key] = dict[key].add(each)
|
||||
};
|
||||
findMax = { |dict|
|
||||
var size = 0, max = [];
|
||||
dict.keysValuesDo { |key, val|
|
||||
if(val.size == size) { max = max.add(val) } {
|
||||
if(val.size > size) { max = []; size = val.size }
|
||||
}
|
||||
};
|
||||
max
|
||||
};
|
||||
findMax.(dict)
|
||||
)
|
||||
1
Task/Anagrams/SuperCollider/anagrams-2.supercollider
Normal file
1
Task/Anagrams/SuperCollider/anagrams-2.supercollider
Normal file
|
|
@ -0,0 +1 @@
|
|||
[ [ angel, angle, galen, glean, lange ], [ caret, carte, cater, crate, trace ], [ elan, lane, lean, lena, neal ], [ evil, levi, live, veil, vile ], [ alger, glare, lager, large, regal ] ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue