update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
58
Task/Semordnilap/Aime/semordnilap.aime
Normal file
58
Task/Semordnilap/Aime/semordnilap.aime
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
text
|
||||
reverse(text s)
|
||||
{
|
||||
data b;
|
||||
integer i;
|
||||
|
||||
i = length(s);
|
||||
while (i) {
|
||||
i -= 1;
|
||||
b_insert(b, -1, character(s, i));
|
||||
}
|
||||
|
||||
return b_string(b);
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
integer c, p;
|
||||
record r;
|
||||
file f;
|
||||
text s;
|
||||
|
||||
f_affix(f, "unixdict.txt");
|
||||
|
||||
while (f_line(f, s) != -1) {
|
||||
r_p_integer(r, s, 0);
|
||||
}
|
||||
|
||||
c = 0;
|
||||
p = 0;
|
||||
|
||||
if (r_first(r, s)) {
|
||||
do {
|
||||
text t;
|
||||
|
||||
t = reverse(s);
|
||||
if (compare(s, t) > 0) {
|
||||
if (r_key(r, t)) {
|
||||
p += 1;
|
||||
if (c < 5) {
|
||||
c += 1;
|
||||
o_text(s);
|
||||
o_byte(' ');
|
||||
o_text(t);
|
||||
o_byte('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (r_greater(r, s, s));
|
||||
}
|
||||
|
||||
o_text("Semordnilap pairs: ");
|
||||
o_integer(p);
|
||||
o_text("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
24
Task/Semordnilap/Bracmat/semordnilap.bracmat
Normal file
24
Task/Semordnilap/Bracmat/semordnilap.bracmat
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
( get'("unixdict.txt",STR):?dict
|
||||
& new$hash:?H
|
||||
& 0:?p
|
||||
& ( @( !dict
|
||||
: ?
|
||||
( [!p ?w \n [?p ?
|
||||
& (H..insert)$(!w.rev$!w)
|
||||
& ~
|
||||
)
|
||||
)
|
||||
| 0:?N
|
||||
& (H..forall)
|
||||
$ (
|
||||
=
|
||||
. !arg:(?a.?b)
|
||||
& !a:<!b
|
||||
& (H..find)$!b
|
||||
& !N+1:?N:<6
|
||||
& out$(!a !b)
|
||||
|
|
||||
)
|
||||
& out$(semordnilap !N dnuoF)
|
||||
)
|
||||
);
|
||||
|
|
@ -15,7 +15,7 @@ static void reverse(char *s, int len)
|
|||
/* Wrap strcmp() for qsort(). */
|
||||
static int strsort(const void *s1, const void *s2)
|
||||
{
|
||||
return strcmp(*(char **) s1, *(char **) s2);
|
||||
return strcmp(*(char *const *) s1, *(char *const *) s2);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
|
|
|||
21
Task/Semordnilap/Clojure/semordnilap.clj
Normal file
21
Task/Semordnilap/Clojure/semordnilap.clj
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(use 'clojure.java.io)
|
||||
(require '[clojure.string :as string])
|
||||
|
||||
(def dict-file (or (first *command-line-args*) "unixdict.txt"))
|
||||
|
||||
(def dict (set (line-seq (reader dict-file))))
|
||||
|
||||
(defn semordnilap? [word]
|
||||
(let [rev (string/reverse word)]
|
||||
(and (not (= word rev)) (dict rev))))
|
||||
|
||||
(def semordnilaps
|
||||
(filter (fn [[x y]] (<= (compare x y) 0))
|
||||
(map (fn [word] [word (string/reverse word)])
|
||||
(filter semordnilap? dict))))
|
||||
|
||||
(printf "There are %d semordnilaps in %s. Here are 5:\n"
|
||||
(count semordnilaps)
|
||||
dict-file)
|
||||
|
||||
(dorun (map println (sort (take 5 (shuffle semordnilaps)))))
|
||||
35
Task/Semordnilap/Erlang/semordnilap.erl
Normal file
35
Task/Semordnilap/Erlang/semordnilap.erl
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env escript
|
||||
main([]) -> main(["unixdict.txt"]);
|
||||
|
||||
main([DictFile]) ->
|
||||
Dict = sets:from_list(read_lines(DictFile)),
|
||||
Semordnilaps =
|
||||
lists:filter(fun([W,R]) -> W < R end,
|
||||
lists:map(fun(W) -> [W, lists:reverse(W)] end,
|
||||
semordnilaps(Dict))),
|
||||
io:fwrite("There are ~b semordnilaps in ~s~n",
|
||||
[length(Semordnilaps), DictFile]),
|
||||
lists:map(fun([W,R]) -> io:fwrite("~s/~s~n", [W, R]) end,
|
||||
lists:sort(lists:sublist(shuffle(Semordnilaps),1,5))).
|
||||
|
||||
read_lines(Filename) when is_list(Filename) ->
|
||||
{ ok, File } = file:open(Filename, [read]),
|
||||
read_lines(File);
|
||||
|
||||
read_lines(File) when is_pid(File) ->
|
||||
case file:read_line(File) of
|
||||
{ok, Data} -> [chop(Data) | read_lines(File)];
|
||||
eof -> []
|
||||
end.
|
||||
|
||||
is_semordnilap(Word, Dict) ->
|
||||
Rev = lists:reverse(Word),
|
||||
sets:is_element(Word, Dict) and sets:is_element(Rev, Dict).
|
||||
|
||||
semordnilaps(Dict) ->
|
||||
lists:filter(fun(W) -> is_semordnilap(W, Dict) end, sets:to_list(Dict)).
|
||||
|
||||
shuffle(List) ->
|
||||
[X||{_,X} <- lists:sort([ {random:uniform(), N} || N <- List])].
|
||||
|
||||
chop(L) -> [_|T] = lists:reverse(L), lists:reverse(T).
|
||||
117
Task/Semordnilap/Fortran/semordnilap.f
Normal file
117
Task/Semordnilap/Fortran/semordnilap.f
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
!-*- mode: compilation; default-directory: "/tmp/" -*-
|
||||
!Compilation started at Sun May 19 21:50:08
|
||||
!
|
||||
!a=./F && make $a && $a < unixdict.txt
|
||||
!f95 -Wall -ffree-form F.F -o F
|
||||
! 5 of 158 semordnilaps
|
||||
!yaw
|
||||
!room
|
||||
!xi
|
||||
!tim
|
||||
!nova
|
||||
!
|
||||
!
|
||||
!Compilation finished at Sun May 19 21:50:08
|
||||
!
|
||||
!
|
||||
!
|
||||
!
|
||||
! unixdict.txt information
|
||||
! wc -l unixdict.txt #--> 25104 25 thousand entries
|
||||
! gawk 'length(a)<length($0){a=$0}END{print a}' unixdict.txt #--> electroencephalography longest word has 22 characters
|
||||
! gawk '/[A-Z]/{++a}END{print a}' unixdict.txt #--> <empty> the dictionary is lower case
|
||||
! sort unixdict.txt | cmp - unixdict.txt #--> - unixdict.txt differ: byte 45, line 12
|
||||
! the dictionary is unsorted
|
||||
! mmmmm the dictionary is sorted, according to subroutine bs. There's something about the ampersands within unixdict.txt I misunderstand.
|
||||
|
||||
program Semordnilap
|
||||
implicit none
|
||||
integer :: i, ios, words, swords
|
||||
character(len=24), dimension(32768) :: dictionary, backword
|
||||
real, dimension(5) :: harvest
|
||||
! read the dictionary
|
||||
open(7,file='unixdict.txt')
|
||||
do words = 1, 32768
|
||||
read(7, '(a)', iostat = ios) dictionary(words)
|
||||
if (ios .ne. 0) exit
|
||||
enddo
|
||||
close(7)
|
||||
if (iachar(dictionary(words)(1:1)) .eq. 0) words = words-1
|
||||
! sort the dictionary
|
||||
call bs(dictionary, words)
|
||||
!do i = 1, words
|
||||
! write(6,*) dictionary(i)(1:len_trim(dictionary(i))) ! with which we determine the dictionary was ordered
|
||||
!enddo
|
||||
swords = 0
|
||||
do i = 1, words
|
||||
call reverse(dictionary(i), backword(swords+1))
|
||||
if ((binary_search(dictionary, words, backword(swords+1))) & ! the reversed word is in the dictionary
|
||||
.and. (.not. binary_search(backword, swords, dictionary(i))) & ! and it's new
|
||||
.and. (dictionary(i) .ne. backword(swords+1))) then ! and it's not a palindrome
|
||||
swords = swords + 1
|
||||
call bs(backword, swords)
|
||||
endif
|
||||
enddo
|
||||
call random_number(harvest)
|
||||
call reverse('spalindromes', backword(swords+1))
|
||||
write(6, *) '5 of ', swords, backword(swords+1)
|
||||
write(6,'(5(a/))') (backword(1+int(harvest(i)*(swords-2))), i=1,5)
|
||||
|
||||
contains
|
||||
|
||||
subroutine reverse(inp, outp)
|
||||
character(len=*), intent(in) :: inp
|
||||
character(len=*), intent(inout) :: outp
|
||||
integer :: k, L
|
||||
L = len_trim(inp)
|
||||
do k = 1, L
|
||||
outp(L+1-k:L+1-k) = inp(k:k)
|
||||
enddo
|
||||
do k = L+1, len(outp)
|
||||
outp(k:k) = ' '
|
||||
enddo
|
||||
end subroutine reverse
|
||||
|
||||
subroutine bs(a, n) ! ok, despite having claimed that bubble sort should be unceremoniously buried, I'll use it anyway because I expect the dictionary is nearly ordered. It's also not a terrible sort for less than 5 items.
|
||||
! Please note, I tested bs using unixdict.txt randomized with sort --random .
|
||||
character(len=*),dimension(*),intent(inout) :: a
|
||||
integer, intent(in) :: n
|
||||
integer :: i, j, k
|
||||
logical :: done
|
||||
character(len=1) :: t
|
||||
do i=n-1, 1, -1
|
||||
done = .true.
|
||||
do j=1, i
|
||||
if (a(j+1) .lt. a(j)) then
|
||||
done = .false.
|
||||
do k = 1, max(len_trim(a(j+1)), len_trim(a(j)))
|
||||
t = a(j+1)(k:k)
|
||||
a(j+1)(k:k) = a(j)(k:k)
|
||||
a(j)(k:k) = t(1:1)
|
||||
enddo
|
||||
endif
|
||||
enddo
|
||||
if (done) return
|
||||
enddo
|
||||
end subroutine bs
|
||||
|
||||
logical function binary_search(source, n, target)
|
||||
character(len=*),dimension(*),intent(in) :: source
|
||||
character(len=*),intent(in) :: target
|
||||
integer, intent(in) :: n
|
||||
integer :: a,m,z
|
||||
a = 1
|
||||
z = n
|
||||
do while (a .lt. z)
|
||||
m = a + (z - a) / 2
|
||||
if (target .lt. source(m)) then
|
||||
z = m-1
|
||||
else
|
||||
if (m .eq. a) exit
|
||||
a = m
|
||||
endif
|
||||
enddo
|
||||
binary_search = (target .eq. source(a)) .or. (target .eq. source(z))
|
||||
end function binary_search
|
||||
|
||||
end program Semordnilap
|
||||
40
Task/Semordnilap/JavaScript/semordnilap-1.js
Normal file
40
Task/Semordnilap/JavaScript/semordnilap-1.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env node
|
||||
var fs = require('fs');
|
||||
var sys = require('sys');
|
||||
|
||||
var dictFile = process.argv[2] || "unixdict.txt";
|
||||
|
||||
var dict = {};
|
||||
fs.readFileSync(dictFile)
|
||||
.toString()
|
||||
.split('\n')
|
||||
.forEach(function(word) {
|
||||
dict[word] = word.split("").reverse().join("");
|
||||
});
|
||||
|
||||
function isSemordnilap(word) { return dict[dict[word]]; };
|
||||
|
||||
var semordnilaps = []
|
||||
for (var key in dict) {
|
||||
if (isSemordnilap(key)) {
|
||||
var rev = dict[key];
|
||||
if (key < rev) {
|
||||
semordnilaps.push([key,rev]) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var count = semordnilaps.length;
|
||||
sys.puts("There are " + count + " semordnilaps in " +
|
||||
dictFile + ". Here are 5:" );
|
||||
|
||||
var indices=[]
|
||||
for (var i=0; i<count; ++i) {
|
||||
if (Math.random() < 1/Math.ceil(i/5.0)) {
|
||||
indices[i%5] = i
|
||||
}
|
||||
}
|
||||
indices.sort()
|
||||
for (var i=0; i<5; ++i) {
|
||||
sys.puts(semordnilaps[indices[i]]);
|
||||
}
|
||||
38
Task/Semordnilap/JavaScript/semordnilap-2.js
Normal file
38
Task/Semordnilap/JavaScript/semordnilap-2.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env rhino
|
||||
|
||||
importPackage (java.io)
|
||||
|
||||
var dictFile = arguments[0] || "unixdict.txt";
|
||||
|
||||
var reader = new BufferedReader(new FileReader(dictFile));
|
||||
var dict = {};
|
||||
var word;
|
||||
while (word = reader.readLine()) {
|
||||
dict[word] = word.split("").reverse().join("");
|
||||
}
|
||||
|
||||
function isSemordnilap(word) { return dict[dict[word]]; };
|
||||
|
||||
var semordnilaps = []
|
||||
for (var key in dict) {
|
||||
if (isSemordnilap(key)) {
|
||||
var rev = dict[key];
|
||||
if (key < rev) {
|
||||
semordnilaps.push([key,rev]) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var count = semordnilaps.length;
|
||||
print("There are " + count + " semordnilaps in " +
|
||||
dictFile + ". Here are 5:" );
|
||||
var indices=[]
|
||||
for (var i=0; i<count; ++i) {
|
||||
if (Math.random() < 1/Math.ceil(i/5.0)) {
|
||||
indices[i%5] = i
|
||||
}
|
||||
}
|
||||
indices.sort()
|
||||
for (var i=0; i<5; ++i) {
|
||||
print(semordnilaps[indices[i]]);
|
||||
}
|
||||
14
Task/Semordnilap/Racket/semordnilap.rkt
Normal file
14
Task/Semordnilap/Racket/semordnilap.rkt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#lang racket
|
||||
(define seen (make-hash))
|
||||
(define semordnilaps '())
|
||||
(call-with-input-file "/usr/share/dict/words"
|
||||
(λ(i) (for ([l (in-lines i)])
|
||||
(define r (list->string (reverse (string->list l))))
|
||||
(unless (equal? r l)
|
||||
(hash-set! seen l #t)
|
||||
(when (hash-ref seen r #f)
|
||||
(set! semordnilaps (cons (list r l) semordnilaps)))))))
|
||||
(printf "Total semordnilaps found: ~s\n" (length semordnilaps))
|
||||
(printf "The five longest ones:\n")
|
||||
(for ([s (take (sort semordnilaps > #:key (compose1 string-length car)) 5)])
|
||||
(apply printf " ~s ~s\n" s))
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
DICT=File.readlines("unixdict.txt").collect{|line| line.tr("\n", "")}
|
||||
res=[]
|
||||
DICT=File.readlines("unixdict.txt").collect &:chomp
|
||||
i = 0
|
||||
DICT.collect {|z| z.reverse}.sort.each {|z|
|
||||
res = DICT.collect(&:reverse).sort.select {|z|
|
||||
i+=1 while z > DICT[i] and i < DICT.length-1
|
||||
res.push z if z.eql?(DICT[i]) and z < z.reverse
|
||||
z == DICT[i] and z < z.reverse
|
||||
}
|
||||
puts "There are #{res.length} semordnilaps, of which the following are 5:"
|
||||
res.sample(5).each {|z| puts "#{z} #{z.reverse}"}
|
||||
|
|
|
|||
28
Task/Semordnilap/Scala/semordnilap.scala
Normal file
28
Task/Semordnilap/Scala/semordnilap.scala
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
val wordsAll = scala.io.Source.fromURL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").getLines.map(_.toLowerCase).to[IndexedSeq]
|
||||
|
||||
/**
|
||||
* Given a sequence of lower-case words return a sub-sequence
|
||||
* of matches containing the word and its reverse if the two
|
||||
* words are different.
|
||||
*/
|
||||
def semordnilap( words:Seq[String] ) : Seq[(String,String)] = {
|
||||
|
||||
( words.
|
||||
zipWithIndex. // index will be needed to eliminate duplicate
|
||||
filter {
|
||||
case (w,i) =>
|
||||
val j = words.indexOf(w.reverse) // eg. (able,62) and (elba,7519)
|
||||
i < j && w != w.reverse // save the matches which are not palindromes
|
||||
}
|
||||
).
|
||||
map {
|
||||
case (w,i) => (w,w.reverse) // drop the index
|
||||
}
|
||||
}
|
||||
|
||||
val ss = semordnilap(wordsAll)
|
||||
|
||||
{
|
||||
println( ss.size + " matches, including: \n" )
|
||||
println( ss.take(5).mkString( "\n" ) )
|
||||
}
|
||||
37
Task/Semordnilap/Seed7/semordnilap.seed7
Normal file
37
Task/Semordnilap/Seed7/semordnilap.seed7
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "gethttp.s7i";
|
||||
|
||||
const func string: reverse (in string: word) is func
|
||||
result
|
||||
var string: drow is "";
|
||||
local
|
||||
var integer: index is 0;
|
||||
begin
|
||||
for index range length(word) downto 1 do
|
||||
drow &:= word[index];
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var array string: wordList is 0 times "";
|
||||
var set of string: words is (set of string).value;
|
||||
var string: word is "";
|
||||
var string: drow is "";
|
||||
var integer: count is 0;
|
||||
begin
|
||||
wordList := split(lower(getHttp("www.puzzlers.org/pub/wordlists/unixdict.txt")), "\n");
|
||||
for word range wordList do
|
||||
drow := reverse(word);
|
||||
if drow not in words then
|
||||
incl(words, word);
|
||||
else
|
||||
if count < 5 then
|
||||
writeln(word <& " " <& drow);
|
||||
end if;
|
||||
incr(count);
|
||||
end if;
|
||||
end for;
|
||||
writeln;
|
||||
writeln("Semordnilap pairs: " <& count);
|
||||
end func;
|
||||
Loading…
Add table
Add a link
Reference in a new issue