Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,35 @@
(require 'struct)
(require 'hash)
(require 'sql)
(require 'words)
(require 'dico.fr.no-accent)
(define mots-français (words-select #:any null 999999))
(string-delimiter "")
(define (string-sort str)
(list->string (list-sort string<? (string->list str))))
(define (ana-sort H words) ;; bump counter for each word
(for ((w words))
#:continue (< (string-length w) 4)
(let [(key (string-sort w))] (hash-set H key (1+ (hash-ref! H key 0))))))
;; input w word
;; output : list of matching words
(define (anagrams w words)
(set! w (string-sort w))
(make-set
(for/list (( ana words))
#:when (string=? w (string-sort ana))
ana)))
(define (task words)
(define H (make-hash))
(ana-sort H words) ;; build counters key= sorted-string, value = count
(hash-get-keys H ;; extract max count values
(for/fold (hmax 0) ((h H) )
#:when (>= (cdr h) hmax)
(cdr h))
))

View file

@ -0,0 +1,8 @@
(length mots-français)
→ 209315
(task mots-français)
→ (aeilns acenr) ;; two winners
(anagrams "acenr" mots-français)
→ { ancre caner caren carne ceran cerna encra nacre nerac rance renac }
(anagrams "aeilns" mots-français)
→ { alisen enlias enlisa ensila islaen islean laines lianes salien saline selina }

View file

@ -0,0 +1,136 @@
' FB 1.05.0 Win64
Type IndexedWord
As String word
As Integer index
End Type
' selection sort, quick enough for sorting small number of letters
Sub sortWord(s As String)
Dim As Integer i, j, m, n = Len(s)
For i = 0 To n - 2
m = i
For j = i + 1 To n - 1
If s[j] < s[m] Then m = j
Next j
If m <> i Then Swap s[i], s[m]
Next i
End Sub
' selection sort, quick enough for sorting small array of IndexedWord instances by index
Sub sortIndexedWord(iw() As IndexedWord)
Dim As Integer i, j, m, n = UBound(iw)
For i = 1 To n - 1
m = i
For j = i + 1 To n
If iw(j).index < iw(m).index Then m = j
Next j
If m <> i Then Swap iw(i), iw(m)
Next i
End Sub
' quicksort for sorting whole dictionary of IndexedWord instances by sorted word
Sub quicksort(a() As IndexedWord, first As Integer, last As Integer)
Dim As Integer length = last - first + 1
If length < 2 Then Return
Dim pivot As String = a(first + length\ 2).word
Dim lft As Integer = first
Dim rgt As Integer = last
While lft <= rgt
While a(lft).word < pivot
lft +=1
Wend
While a(rgt).word > pivot
rgt -= 1
Wend
If lft <= rgt Then
Swap a(lft), a(rgt)
lft += 1
rgt -= 1
End If
Wend
quicksort(a(), first, rgt)
quicksort(a(), lft, last)
End Sub
Dim t As Double = timer
Dim As String w() '' array to hold actual words
Open "undict.txt" For Input As #1
Dim count As Integer = 0
While Not Eof(1)
count +=1
Redim Preserve w(1 To count)
Line Input #1, w(count)
Wend
Close #1
Dim As IndexedWord iw(1 To count) '' array to hold sorted words and their index into w()
Dim word As String
For i As Integer = 1 To count
word = w(i)
sortWord(word)
iw(i).word = word
iw(i).index = i
Next
quickSort iw(), 1, count '' sort the IndexedWord array by sorted word
Dim As Integer startIndex = 1, length = 1, maxLength = 1, ub = 1
Dim As Integer maxIndex(1 To ub)
maxIndex(ub) = 1
word = iw(1).word
For i As Integer = 2 To count
If word = iw(i).word Then
length += 1
Else
If length > maxLength Then
maxLength = length
Erase maxIndex
ub = 1
Redim maxIndex(1 To ub)
maxIndex(ub) = startIndex
ElseIf length = maxLength Then
ub += 1
Redim Preserve maxIndex(1 To ub)
maxIndex(ub) = startIndex
End If
startIndex = i
length = 1
word = iw(i).word
End If
Next
If length > maxLength Then
maxLength = length
Erase maxIndex
Redim maxIndex(1 To 1)
maxIndex(1) = startIndex
ElseIf length = maxLength Then
ub += 1
Redim Preserve maxIndex(1 To ub)
maxIndex(ub) = startIndex
End If
Print Str(count); " words in the dictionary"
Print "The anagram set(s) with the greatest number of words (namely"; maxLength; ") is:"
Print
Dim iws(1 To maxLength) As IndexedWord '' array to hold each anagram set
For i As Integer = 1 To UBound(maxIndex)
For j As Integer = maxIndex(i) To maxIndex(i) + maxLength - 1
iws(j - maxIndex(i) + 1) = iw(j)
Next j
sortIndexedWord iws() '' sort anagram set before displaying it
For j As Integer = 1 To maxLength
Print w(iws(j).index); " ";
Next j
Print
Next i
Print
Print "Took ";
Print Using "#.###"; timer - t;
Print " seconds on i3 @ 2.13 GHz"
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,97 @@
include "ConsoleWindow"
def tab 9
begin globals
dim dynamic gDictionary(_maxLong) as Str255
end globals
local fn IsAnagram( word1 as Str31, word2 as Str31 ) as Boolean
dim as long i, j, h, q
dim as Boolean result
if word1[0] != word2[0] then result = _false : exit fn
for i = 0 to word1[0]
h = 0 : q = 0
for j = 0 to word1[0]
if word1[i] == word1[j] then h++
if word1[i] == word2[j] then q++
next
if h != q then result = _false : exit fn
next
result = _true
end fn = result
local fn LoadDictionaryToArray
'~'1
dim as CFURLRef url
dim as CFArrayRef arr
dim as CFStringRef temp, cfStr
dim as CFIndex elements
dim as Handle h
dim as Str255 s
dim as long fileLen, i
kill dynamic gDictionary
url = fn CFURLCreateWithFileSystemPath( _kCFAllocatorDefault, @"/usr/share/dict/words", _kCFURLPOSIXPathStyle, _false )
open "i", 2, url
fileLen = lof(2, 1)
h = fn NewHandleClear( fileLen )
if ( h )
read file 2, [h], fileLen
cfStr = fn CFStringCreateWithBytes( _kCFAllocatorDefault, #[h], fn GetHandleSize(h), _kCFStringEncodingMacRoman, _false )
if ( cfStr )
arr = fn CFStringCreateArrayBySeparatingStrings( _kCFAllocatorDefault, cfStr, @"\n" )
CFRelease( cfStr )
elements = fn CFArrayGetCount( arr )
for i = 0 to elements - 1
temp = fn CFArrayGetValueAtIndex( arr, i )
fn CFStringGetPascalString( temp, @s, 256, _kCFStringEncodingMacRoman )
gDictionary(i) = s
next
CFRelease( arr )
end if
fn DisposeH( h )
end if
close #2
CFRelease( url )
end fn
local fn FindAnagrams( whichWord as Str31 )
dim as long elements, i
print "Anagrams for "; UCase$(whichWord); ":",
elements = fn DynamicNextElement( dynamic( gDictionary ) )
for i = 0 to elements - 1
if ( len( gDictionary(i) ) == whichWord[0] )
if ( fn IsAnagram( whichWord, gDictionary(i) ) == _true )
print gDictionary(i),
end if
end if
next
print
end fn
fn LoadDictionaryToArray
fn FindAnagrams( "bade" )
fn FindAnagrams( "abet" )
fn FindAnagrams( "beast" )
fn FindAnagrams( "tuba" )
fn FindAnagrams( "mace" )
fn FindAnagrams( "scare" )
fn FindAnagrams( "marine" )
fn FindAnagrams( "antler" )
fn FindAnagrams( "spare" )
fn FindAnagrams( "leading" )
fn FindAnagrams( "alerted" )
fn FindAnagrams( "allergy" )
fn FindAnagrams( "research")
fn FindAnagrams( "hustle" )
fn FindAnagrams( "oriental")
def tab 3
print
fn FindAnagrams( "creationism" )
fn FindAnagrams( "resistance" )
fn FindAnagrams( "mountaineer" )

View file

@ -0,0 +1,27 @@
local(
anagrams = map,
words = include_url('http://www.puzzlers.org/pub/wordlists/unixdict.txt')->split('\n'),
key,
max = 0,
findings = array
)
with word in #words do {
#key = #word -> split('') -> sort& -> join('')
if(not(#anagrams >> #key)) => {
#anagrams -> insert(#key = array)
}
#anagrams -> find(#key) -> insert(#word)
}
with ana in #anagrams
let ana_size = #ana -> size
do {
if(#ana_size > #max) => {
#findings = array(#ana -> join(', '))
#max = #ana_size
else(#ana_size == #max)
#findings -> insert(#ana -> join(', '))
}
}
#findings -> join('<br />\n')

View file

@ -0,0 +1,39 @@
on mouseUp
put mostCommonAnagrams(url "http://www.puzzlers.org/pub/wordlists/unixdict.txt")
end mouseUp
function mostCommonAnagrams X
put 0 into maxCount
repeat for each word W in X
get sortChars(W)
put W & comma after A[it]
add 1 to C[it]
if C[it] >= maxCount then
if C[it] > maxCount then
put C[it] into maxCount
put char 1 to -2 of A[it] into winnerList
else
put cr & char 1 to -2 of A[it] after winnerList
end if
end if
end repeat
return winnerList
end mostCommonAnagrams
function sortChars X
get charsToItems(X)
sort items of it
return itemsToChars(it)
end sortChars
function charsToItems X
repeat for each char C in X
put C & comma after R
end repeat
return char 1 to -2 of R
end charsToItems
function itemsToChars X
replace comma with empty in X
return X
end itemsToChars

View file

@ -0,0 +1,18 @@
import tables, strutils, algorithm
proc main() =
var
count = 0
anagrams = initTable[string, seq[string]]()
for word in "unixdict.txt".lines():
var key = word
key.sort(cmp[char])
anagrams.mgetOrPut(key, newSeq[string]()).add(word)
count = max(count, anagrams[key].len)
for k, v in anagrams:
if v.len == count:
v.join(", ").echo
main()

View file

@ -0,0 +1,5 @@
: anagrams
| m |
File new("unixdict.txt") groupBy(#sort)
dup sortBy(#[ second size]) last second size ->m
filter(#[ second size m == ]) apply(#[ second .cr ]) ;

View file

@ -0,0 +1,40 @@
integer fn = open("unixdict.txt","r")
sequence words = {}, anagrams = {}, last="", letters
object word
integer maxlen = 1
while 1 do
word = trim(gets(fn))
if atom(word) then exit end if
if length(word) then
letters = sort(word)
words = append(words, {letters, word})
end if
end while
close(fn)
words = sort(words)
for i=1 to length(words) do
{letters,word} = words[i]
if letters=last then
anagrams[$] = append(anagrams[$],word)
if length(anagrams[$])>maxlen then
maxlen = length(anagrams[$])
end if
else
last = letters
anagrams = append(anagrams,{word})
end if
end for
puts(1,"\nMost anagrams:\n")
for i=1 to length(anagrams) do
last = anagrams[i]
if length(last)=maxlen then
for j=1 to maxlen do
if j>1 then puts(1,", ") end if
puts(1,last[j])
end for
puts(1,"\n")
end if
end for

View file

@ -0,0 +1,10 @@
func main(file) {
file.open_r(\var fh, \var err) ->
|| die "Can't open file `#{file}' for reading: #{err}\n";
var vls = fh.words.group_by{.sort}.values;
var max = vls.map{.len}.max;
vls.grep{.len == max}.each{.join("\t").say};
}
main(%f'/tmp/unixdict.txt');

View file

@ -0,0 +1,41 @@
import Foundation
let wordsURL = NSURL(string: "http://www.puzzlers.org/pub/wordlists/unixdict.txt")!
let wordsstring = try NSString(contentsOfURL:wordsURL , encoding: NSUTF8StringEncoding)
let allwords = wordsstring.componentsSeparatedByString("\n")
let words = allwords//[0..<100] // used to limit the size while testing
extension String {
var charactersAscending : String {
return String(Array(characters).sort())
}
}
var charsToWords = [String:Set<String>]()
var biggest = 0
var biggestlists = [Set<String>]()
for thisword in words {
let chars = thisword.charactersAscending
var knownwords = charsToWords[chars] ?? Set<String>()
knownwords.insert(thisword)
charsToWords[chars] = knownwords
if knownwords.count > biggest {
biggest = knownwords.count
biggestlists = [knownwords]
}
else if knownwords.count == biggest {
biggestlists.append(knownwords)
}
}
print("Found \(biggestlists.count) sets of anagrams with \(biggest) members each")
for (i, thislist) in biggestlists.enumerate() {
print("set \(i): \(thislist.sort())")
}

View file

@ -0,0 +1,11 @@
def anagrams:
(reduce .[] as $word (
{table: {}, max: 0}; # state
($word | explode | sort | implode) as $hash
| .table = ( .table + { ($hash): ( .table[$hash] + [ $word ]) })
| .max = ([ .max, ( .table[$hash] | length) ] | max ) ))
| .max as $max
| .table | .[] | select(length == $max) ;
# The task:
split("\n") | anagrams

View file

@ -0,0 +1,7 @@
$ jq -M -s -c -R -f anagrams.jq unixdict.txt
["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"]