Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
20
Task/Anagrams/DuckDB/anagrams.duckdb
Normal file
20
Task/Anagrams/DuckDB/anagrams.duckdb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# The MAP giving the frequency counts of characters in the given string
|
||||
create or replace function spectrum(str) as (
|
||||
select histogram(c)
|
||||
from (select unnest(regexp_extract_all(str,'.')) as c)
|
||||
);
|
||||
|
||||
# Find the anagram groups having the most members.
|
||||
# Each group is sorted, and the groups are sorted by first word in the group.
|
||||
with words as (from read_csv('unixdict.txt', header=false) _(word)),
|
||||
histograms as (select word, spectrum(word) as h from words),
|
||||
groups as (select h, count(h) as c from histograms group by h order by c desc),
|
||||
mx as (select max(c) as mx from groups),
|
||||
maximals as (select h from groups, mx where c = mx.mx),
|
||||
results as (select (select array_agg(word).list_sort()
|
||||
from histograms
|
||||
where histograms.h = maximals.h ) as anagrams
|
||||
from maximals)
|
||||
select anagrams
|
||||
from results
|
||||
order by anagrams[1] ;
|
||||
38
Task/Anagrams/TAV/anagrams.tav
Normal file
38
Task/Anagrams/TAV/anagrams.tav
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
\( Find anagrams (words with the same set of letters)
|
||||
Words are the lines of an input file.
|
||||
Sorting the letters of the word gives the key for a map
|
||||
having a list (row) of all words so far.
|
||||
\)
|
||||
\+ stdlib
|
||||
main(parms):+
|
||||
parms[1] =~ 'unixdict.txt' \ default input file
|
||||
wordmap =: get words from parms[1]
|
||||
maxw =: wordmap.maxlen
|
||||
?# idx =: map wordmap give keys ascending
|
||||
words =: wordmap{idx}
|
||||
? maxw = words.count
|
||||
print words::join values by ', '
|
||||
|
||||
\ read the file (infn) and return a map
|
||||
get words from (infn):
|
||||
res =: new map \ create the result map
|
||||
res.maxlen =: 0
|
||||
?# line =: file infn give lines \ or: infn::give lines
|
||||
add line to res
|
||||
:> res
|
||||
|
||||
\ add a word to the map
|
||||
add (word) to (amap):
|
||||
idx =: sort characters of word::case to upper
|
||||
row =: amap{idx} \ get indexed words
|
||||
? row = () \ new entry if void
|
||||
row =: new row
|
||||
amap{idx} =: row
|
||||
row[] =: word \ add new entry
|
||||
amap.maxlen =: maximum of amap.maxlen, row.count
|
||||
|
||||
\ Sort the characters of a string
|
||||
sort characters of (s):
|
||||
sr =: string s as character row
|
||||
row sr sort ascending
|
||||
:> row sr join values as string \ without separator
|
||||
Loading…
Add table
Add a link
Reference in a new issue