Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
14
Task/Semordnilap/ArkScript/semordnilap.ark
Normal file
14
Task/Semordnilap/ArkScript/semordnilap.ark
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(import std.IO)
|
||||
(import std.String)
|
||||
(import std.Dict)
|
||||
(import std.List)
|
||||
|
||||
(let words (string:split (io:readFile "tests/unittests/resources/RosettaSuite/res/unixdict.txt") "\n"))
|
||||
|
||||
(let d (dict))
|
||||
(list:forEach words (fun (word) (dict:add d word true)))
|
||||
(let keep (list:filter words (fun (word)
|
||||
(and (< word (string:reverse word)) (not (nil? (dict:get d (string:reverse word))))))))
|
||||
|
||||
(print (len keep))
|
||||
(print (list:slice keep 0 5 1))
|
||||
|
|
@ -3,20 +3,20 @@ S := [], M := []
|
|||
FileRead, dict, unixdict.txt
|
||||
Loop, Parse, dict, `n, `r`n
|
||||
{
|
||||
r := Reverse(A_LoopField)
|
||||
if (S[r])
|
||||
M.Insert(r " / " A_LoopField)
|
||||
else
|
||||
S[A_LoopField] := 1
|
||||
r := Reverse(A_LoopField)
|
||||
if (S[r])
|
||||
M.Insert(r " / " A_LoopField)
|
||||
else
|
||||
S[A_LoopField] := 1
|
||||
}
|
||||
|
||||
Loop, 5
|
||||
Out .= "`t" M[A_Index] "`n"
|
||||
Out .= "`t" M[A_Index] "`n"
|
||||
|
||||
MsgBox, % "5 Examples:`n" Out "`nTotal Pairs:`n`t" M.MaxIndex()
|
||||
|
||||
Reverse(s) {
|
||||
Loop, Parse, s
|
||||
r := A_LoopField . r
|
||||
return r
|
||||
Loop, Parse, s
|
||||
r := A_LoopField . r
|
||||
return r
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,18 +8,18 @@
|
|||
;; check reverse r of w is a word
|
||||
;; take only one pair : r < w
|
||||
(define (semordnilap? w)
|
||||
(define r (list->string (reverse (string->list w))))
|
||||
(and (word? r) (string<? r w)))
|
||||
|
||||
(define r (list->string (reverse (string->list w))))
|
||||
(and (word? r) (string<? r w)))
|
||||
|
||||
;; to get longest first
|
||||
(define (string-sort a b) (> (string-length a) (string-length b)))
|
||||
|
||||
|
||||
(define (task)
|
||||
;; select unique words into the list 'mots'
|
||||
(define mots (make-set (words-select #:any null 999999)))
|
||||
(define semordnilap
|
||||
(list-sort string-sort (for/list ((w mots))
|
||||
#:when (semordnilap? w)
|
||||
w )))
|
||||
(list-sort string-sort (for/list ((w mots))
|
||||
#:when (semordnilap? w)
|
||||
w )))
|
||||
(writeln 'pairs '→ (length semordnilap))
|
||||
(writeln 'longest '→ (take semordnilap 5)))
|
||||
|
|
|
|||
|
|
@ -1,79 +1,79 @@
|
|||
class
|
||||
SEMORDNILAP
|
||||
SEMORDNILAP
|
||||
|
||||
create
|
||||
make
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
--Semordnilaps in 'solution'.
|
||||
local
|
||||
count, i, middle, upper, lower: INTEGER
|
||||
reverse: STRING
|
||||
do
|
||||
read_wordlist
|
||||
create solution.make_empty
|
||||
from
|
||||
i := 1
|
||||
until
|
||||
i > word_array.count
|
||||
loop
|
||||
word_array [i].mirror
|
||||
reverse := word_array [i]
|
||||
from
|
||||
lower := i + 1
|
||||
upper := word_array.count
|
||||
until
|
||||
lower >= upper
|
||||
loop
|
||||
middle := (upper - lower) // 2 + lower
|
||||
if reverse.same_string (word_array [middle]) then
|
||||
count := count + 1
|
||||
upper := 0
|
||||
lower := 1
|
||||
solution.force (word_array [i], count)
|
||||
elseif reverse.is_less (word_array [middle]) then
|
||||
upper := middle - 1
|
||||
else
|
||||
lower := middle + 1
|
||||
end
|
||||
end
|
||||
if lower < word_array.count and then reverse.same_string (word_array [lower]) then
|
||||
count := count + 1
|
||||
upper := 0
|
||||
lower := 1
|
||||
solution.force (word_array [i], count)
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
make
|
||||
--Semordnilaps in 'solution'.
|
||||
local
|
||||
count, i, middle, upper, lower: INTEGER
|
||||
reverse: STRING
|
||||
do
|
||||
read_wordlist
|
||||
create solution.make_empty
|
||||
from
|
||||
i := 1
|
||||
until
|
||||
i > word_array.count
|
||||
loop
|
||||
word_array [i].mirror
|
||||
reverse := word_array [i]
|
||||
from
|
||||
lower := i + 1
|
||||
upper := word_array.count
|
||||
until
|
||||
lower >= upper
|
||||
loop
|
||||
middle := (upper - lower) // 2 + lower
|
||||
if reverse.same_string (word_array [middle]) then
|
||||
count := count + 1
|
||||
upper := 0
|
||||
lower := 1
|
||||
solution.force (word_array [i], count)
|
||||
elseif reverse.is_less (word_array [middle]) then
|
||||
upper := middle - 1
|
||||
else
|
||||
lower := middle + 1
|
||||
end
|
||||
end
|
||||
if lower < word_array.count and then reverse.same_string (word_array [lower]) then
|
||||
count := count + 1
|
||||
upper := 0
|
||||
lower := 1
|
||||
solution.force (word_array [i], count)
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
|
||||
solution: ARRAY [STRING]
|
||||
solution: ARRAY [STRING]
|
||||
|
||||
original_list: STRING = "unixdict.txt"
|
||||
original_list: STRING = "unixdict.txt"
|
||||
|
||||
|
||||
feature {NONE}
|
||||
|
||||
read_wordlist
|
||||
-- Preprocessed word_array for finding Semordnilaps.
|
||||
local
|
||||
l_file: PLAIN_TEXT_FILE
|
||||
wordlist: LIST [STRING]
|
||||
do
|
||||
create l_file.make_open_read_write (original_list)
|
||||
l_file.read_stream (l_file.count)
|
||||
wordlist := l_file.last_string.split ('%N')
|
||||
l_file.close
|
||||
create word_array.make_empty
|
||||
across
|
||||
1 |..| wordlist.count as i
|
||||
loop
|
||||
word_array.force (wordlist.at (i.item), i.item)
|
||||
end
|
||||
end
|
||||
read_wordlist
|
||||
-- Preprocessed word_array for finding Semordnilaps.
|
||||
local
|
||||
l_file: PLAIN_TEXT_FILE
|
||||
wordlist: LIST [STRING]
|
||||
do
|
||||
create l_file.make_open_read_write (original_list)
|
||||
l_file.read_stream (l_file.count)
|
||||
wordlist := l_file.last_string.split ('%N')
|
||||
l_file.close
|
||||
create word_array.make_empty
|
||||
across
|
||||
1 |..| wordlist.count as i
|
||||
loop
|
||||
word_array.force (wordlist.at (i.item), i.item)
|
||||
end
|
||||
end
|
||||
|
||||
word_array: ARRAY [STRING]
|
||||
word_array: ARRAY [STRING]
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,34 +1,34 @@
|
|||
class
|
||||
APPLICATION
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
local
|
||||
test: ARRAY [STRING]
|
||||
s: STRING
|
||||
do
|
||||
create se.make
|
||||
test := se.solution
|
||||
create sort.sort (test)
|
||||
across
|
||||
test.subarray (1, 5) as t
|
||||
loop
|
||||
s := t.item
|
||||
io.put_string (t.item + "%T")
|
||||
s.mirror
|
||||
io.put_string (s)
|
||||
io.new_line
|
||||
end
|
||||
io.put_string ("Total number of semordnilaps: ")
|
||||
io.put_integer (test.count)
|
||||
end
|
||||
make
|
||||
local
|
||||
test: ARRAY [STRING]
|
||||
s: STRING
|
||||
do
|
||||
create se.make
|
||||
test := se.solution
|
||||
create sort.sort (test)
|
||||
across
|
||||
test.subarray (1, 5) as t
|
||||
loop
|
||||
s := t.item
|
||||
io.put_string (t.item + "%T")
|
||||
s.mirror
|
||||
io.put_string (s)
|
||||
io.new_line
|
||||
end
|
||||
io.put_string ("Total number of semordnilaps: ")
|
||||
io.put_integer (test.count)
|
||||
end
|
||||
|
||||
se: SEMORDNILAP
|
||||
se: SEMORDNILAP
|
||||
|
||||
sort: MERGE_SORT [STRING]
|
||||
sort: MERGE_SORT [STRING]
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,30 +3,30 @@ wordlist constant dict
|
|||
: load-dict ( c-addr u -- )
|
||||
r/o open-file throw >r
|
||||
begin
|
||||
pad 1024 r@ read-line throw while
|
||||
pad swap ['] create execute-parsing
|
||||
pad 1024 r@ read-line throw while
|
||||
pad swap ['] create execute-parsing
|
||||
repeat
|
||||
drop r> close-file throw ;
|
||||
|
||||
: xreverse {: c-addr u -- c-addr2 u :}
|
||||
u allocate throw u + c-addr swap over u + >r begin ( from to r:end)
|
||||
over r@ u< while
|
||||
over r@ over - x-size dup >r - 2dup r@ cmove
|
||||
swap r> + swap repeat
|
||||
over r@ u< while
|
||||
over r@ over - x-size dup >r - 2dup r@ cmove
|
||||
swap r> + swap repeat
|
||||
r> drop nip u ;
|
||||
|
||||
: .example ( c-addr u u1 -- )
|
||||
5 < if
|
||||
cr 2dup type space 2dup xreverse 2dup type drop free throw then
|
||||
cr 2dup type space 2dup xreverse 2dup type drop free throw then
|
||||
2drop ;
|
||||
|
||||
: nt-semicheck ( u1 nt -- u2 f )
|
||||
dup >r name>string xreverse 2dup dict find-name-in dup if ( u1 c-addr u nt2)
|
||||
r@ < if ( u1 c-addr u ) \ count pairs only once and not palindromes
|
||||
2dup 4 pick .example
|
||||
rot 1+ -rot then
|
||||
r@ < if ( u1 c-addr u ) \ count pairs only once and not palindromes
|
||||
2dup 4 pick .example
|
||||
rot 1+ -rot then
|
||||
else
|
||||
drop then
|
||||
drop then
|
||||
drop free throw r> drop true ;
|
||||
|
||||
get-current dict set-current s" unixdict.txt" load-dict set-current
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
raw = readdlm("unixdict.txt",String)[:]
|
||||
raw = readlines("unixdict.txt")
|
||||
inter = intersect(raw,map(reverse,raw)) #find the matching strings/revstrings
|
||||
res = String[b == 1 && a != reverse(a) && a < reverse(a) ? a : reverse(a) for a in inter, b in 1:2] #create pairs
|
||||
res = res[res[:,1] .!= res[:,2],:] #get rid of duplicates, palindromes
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
# Semordnilap
|
||||
|
||||
# # Variables:
|
||||
# # Variables:
|
||||
#
|
||||
integer MIN_WORD_LEN=1 TRUE=1 FALSE=0
|
||||
dict='/home/ostrande/prj/roscode/unixdict.txt'
|
||||
|
|
@ -10,62 +10,62 @@ dict='/home/ostrande/prj/roscode/unixdict.txt'
|
|||
integer i j=0 k=0
|
||||
typeset -A word
|
||||
|
||||
# # Functions:
|
||||
# # Functions:
|
||||
#
|
||||
|
||||
# # Function _flipit(string) - return flipped string
|
||||
# # Function _flipit(string) - return flipped string
|
||||
#
|
||||
function _flipit {
|
||||
typeset _buf ; _buf="$1"
|
||||
typeset _tmp ; unset _tmp
|
||||
typeset _buf ; _buf="$1"
|
||||
typeset _tmp ; unset _tmp
|
||||
|
||||
for (( _i=$(( ${#_buf}-1 )); _i>=0; _i-- )); do
|
||||
_tmp="${_tmp}${_buf:${_i}:1}"
|
||||
done
|
||||
for (( _i=$(( ${#_buf}-1 )); _i>=0; _i-- )); do
|
||||
_tmp="${_tmp}${_buf:${_i}:1}"
|
||||
done
|
||||
|
||||
echo "${_tmp}"
|
||||
echo "${_tmp}"
|
||||
}
|
||||
|
||||
# # Function _isword(word, wordlist) - return 1 if word in wordlist
|
||||
# # Function _isword(word, wordlist) - return 1 if word in wordlist
|
||||
#
|
||||
function _isword {
|
||||
typeset _word ; _word="$1"
|
||||
typeset _wordlist ; nameref _wordlist="$2"
|
||||
typeset _word ; _word="$1"
|
||||
typeset _wordlist ; nameref _wordlist="$2"
|
||||
|
||||
[[ ${_word} == @(${_wordlist}) ]] && return $TRUE
|
||||
return $FALSE
|
||||
[[ ${_word} == @(${_wordlist}) ]] && return $TRUE
|
||||
return $FALSE
|
||||
}
|
||||
|
||||
######
|
||||
# main #
|
||||
######
|
||||
# # Due to the large number of words in unixdist.txt subgroup by 1st letter and length
|
||||
# # only accept words containing alpha chars and > 1 chars
|
||||
# # Due to the large number of words in unixdist.txt subgroup by 1st letter and length
|
||||
# # only accept words containing alpha chars and > 1 chars
|
||||
#
|
||||
while read; do
|
||||
[[ $REPLY != *+(\W)* ]] && [[ $REPLY != *+(\d)* ]] && \
|
||||
(( ${#REPLY} > MIN_WORD_LEN )) && word[${REPLY:0:1}][${#REPLY}]+=( $REPLY )
|
||||
[[ $REPLY != *+(\W)* ]] && [[ $REPLY != *+(\d)* ]] && \
|
||||
(( ${#REPLY} > MIN_WORD_LEN )) && word[${REPLY:0:1}][${#REPLY}]+=( $REPLY )
|
||||
done < ${dict}
|
||||
|
||||
print Examples:
|
||||
for fl in ${!word[*]}; do # Over $fl first letter
|
||||
for len in ${!word[${fl}][*]}; do # Over $len word length
|
||||
for ((i=0; i<${#word[${fl}][${len}][*]}; i++)); do
|
||||
Word=${word[${fl}][${len}][i]} # dummy
|
||||
Try=$(_flipit ${Word})
|
||||
if [[ ${Try} != ${Word} ]]; then # no palindromes
|
||||
unset words
|
||||
oldIFS="$IFS" ; IFS='|' ; words=${word[${Try:0:1}][${#Try}][*]} ; IFS="${oldIFS}"
|
||||
_isword "${Try}" words
|
||||
if (( $? )); then
|
||||
if [[ ${Try} != @(${uniq%\|*}) ]]; then
|
||||
((++j))
|
||||
(( ${#Word} >= 5 )) && (( k<=5 )) && print $((++k)). ${Word} ${Try}
|
||||
uniq+="${Try}|${Word}|"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
for fl in ${!word[*]}; do # Over $fl first letter
|
||||
for len in ${!word[${fl}][*]}; do # Over $len word length
|
||||
for ((i=0; i<${#word[${fl}][${len}][*]}; i++)); do
|
||||
Word=${word[${fl}][${len}][i]} # dummy
|
||||
Try=$(_flipit ${Word})
|
||||
if [[ ${Try} != ${Word} ]]; then # no palindromes
|
||||
unset words
|
||||
oldIFS="$IFS" ; IFS='|' ; words=${word[${Try:0:1}][${#Try}][*]} ; IFS="${oldIFS}"
|
||||
_isword "${Try}" words
|
||||
if (( $? )); then
|
||||
if [[ ${Try} != @(${uniq%\|*}) ]]; then
|
||||
((++j))
|
||||
(( ${#Word} >= 5 )) && (( k<=5 )) && print $((++k)). ${Word} ${Try}
|
||||
uniq+="${Try}|${Word}|"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
echo ; print ${j} pairs found.
|
||||
|
|
|
|||
|
|
@ -1,27 +1,27 @@
|
|||
local(
|
||||
words = string(include_url('http://www.puzzlers.org/pub/wordlists/unixdict.txt')) -> split('\n'),
|
||||
semordnilaps = array,
|
||||
found_size,
|
||||
example,
|
||||
haveexamples = false,
|
||||
examples = array
|
||||
words = string(include_url('http://www.puzzlers.org/pub/wordlists/unixdict.txt')) -> split('\n'),
|
||||
semordnilaps = array,
|
||||
found_size,
|
||||
example,
|
||||
haveexamples = false,
|
||||
examples = array
|
||||
)
|
||||
|
||||
#words -> removeall('')
|
||||
|
||||
with word in #words do {
|
||||
local(reversed = string(#word) -> reverse&)
|
||||
if(not(#word == #reversed) and not(#semordnilaps >> #word) and not(#semordnilaps >> #reversed) and #words >> #reversed) => {
|
||||
#semordnilaps -> insert(#word = #reversed)
|
||||
}
|
||||
local(reversed = string(#word) -> reverse&)
|
||||
if(not(#word == #reversed) and not(#semordnilaps >> #word) and not(#semordnilaps >> #reversed) and #words >> #reversed) => {
|
||||
#semordnilaps -> insert(#word = #reversed)
|
||||
}
|
||||
}
|
||||
|
||||
#found_size = #semordnilaps -> size
|
||||
|
||||
while(not(#haveexamples)) => {
|
||||
#example = #semordnilaps -> get(integer_random(#found_size, 1))
|
||||
not(#examples >> #example -> name) ? #examples -> insert(#example)
|
||||
#examples -> size >= 5 ? #haveexamples = true
|
||||
#example = #semordnilaps -> get(integer_random(#found_size, 1))
|
||||
not(#examples >> #example -> name) ? #examples -> insert(#example)
|
||||
#examples -> size >= 5 ? #haveexamples = true
|
||||
}
|
||||
'Total found: '
|
||||
#found_size
|
||||
|
|
|
|||
|
|
@ -22,13 +22,18 @@ end
|
|||
-- print the report
|
||||
print("There are " .. #semordnilaps .. " semordnilaps in " .. dictfile .. ". Here are " .. sample_size .. ":")
|
||||
|
||||
math.randomseed( os.time() )
|
||||
for i = 1, sample_size do
|
||||
local j
|
||||
repeat
|
||||
j = math.random(1,#semordnilaps)
|
||||
until semordnilaps[j]
|
||||
local f, r = unpack(semordnilaps[j])
|
||||
semordnilaps[j] = nil
|
||||
print(f .. " -> " .. r)
|
||||
do
|
||||
-- unpack is in table in Lua 5.4
|
||||
local unpack = table.unpack or unpack
|
||||
|
||||
math.randomseed( os.time() )
|
||||
for i = 1, sample_size do
|
||||
local j
|
||||
repeat
|
||||
j = math.random(1,#semordnilaps)
|
||||
until semordnilaps[j]
|
||||
local f, r = unpack(semordnilaps[j])
|
||||
semordnilaps[j] = nil
|
||||
print(f .. " -> " .. r)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
;;; Get the words as a list, splitting at newline
|
||||
;;; Get the words as a list, splitting at newline
|
||||
(setq data
|
||||
(parse (get-url "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
|
||||
"\n"))
|
||||
(parse (get-url "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
|
||||
"\n"))
|
||||
;
|
||||
;;; destructive reverse wrapped into a function
|
||||
;;; destructive reverse wrapped into a function
|
||||
(define (get-reverse x) (reverse x))
|
||||
;
|
||||
;;; stack of the results
|
||||
;;; stack of the results
|
||||
(setq res '())
|
||||
;
|
||||
;;; Find the semordlinap and put them on the stack
|
||||
;;; Find the semordlinap and put them on the stack
|
||||
(dolist (x data)
|
||||
(let (y (get-reverse x))
|
||||
(if (and
|
||||
(member y data) ; reverse is a dictionary word
|
||||
(!= x y) ; but not a palindrome
|
||||
(not (member y res))) ; not already stacked
|
||||
(push x res -1))))
|
||||
(let (y (get-reverse x))
|
||||
(if (and
|
||||
(member y data) ; reverse is a dictionary word
|
||||
(!= x y) ; but not a palindrome
|
||||
(not (member y res))) ; not already stacked
|
||||
(push x res -1))))
|
||||
;
|
||||
;;; Count results
|
||||
;;; Count results
|
||||
(println "Found " (length res) " pairs.")
|
||||
(println)
|
||||
;;; Show the longest ones
|
||||
;;; Show the longest ones
|
||||
(dolist (x res)
|
||||
(if (> (length x) 4) (println x " -- " (get-reverse x))))
|
||||
(if (> (length x) 4) (println x " -- " (get-reverse x))))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
while (<>) {
|
||||
chomp;
|
||||
my $r = reverse;
|
||||
$seen{$r}++ and $c++ < 5 and print "$_ $r\n" or $seen{$_}++;
|
||||
chomp;
|
||||
my $r = reverse;
|
||||
$seen{$r}++ and $c++ < 5 and print "$_ $r\n" or $seen{$_}++;
|
||||
}
|
||||
|
||||
print "$c\n"
|
||||
|
|
|
|||
|
|
@ -5,30 +5,30 @@ include ..\Utilitys.pmt
|
|||
"unixdict.txt" "r" fopen var f
|
||||
|
||||
true while
|
||||
f fgets
|
||||
dup -1 == if
|
||||
drop
|
||||
f fclose
|
||||
false
|
||||
else
|
||||
-1 del
|
||||
0 put
|
||||
true
|
||||
endif
|
||||
f fgets
|
||||
dup -1 == if
|
||||
drop
|
||||
f fclose
|
||||
false
|
||||
else
|
||||
-1 del
|
||||
0 put
|
||||
true
|
||||
endif
|
||||
endwhile
|
||||
|
||||
len while
|
||||
len 1 > if
|
||||
pop swap reverse find dup
|
||||
if
|
||||
extract rot swap 0 put swap
|
||||
else
|
||||
drop
|
||||
endif
|
||||
true
|
||||
else
|
||||
drop false
|
||||
endif
|
||||
len 1 > if
|
||||
pop swap reverse find dup
|
||||
if
|
||||
extract rot swap 0 put swap
|
||||
else
|
||||
drop
|
||||
endif
|
||||
true
|
||||
else
|
||||
drop false
|
||||
endif
|
||||
endwhile
|
||||
|
||||
( 50 54 ) for get dup reverse print " -> " print ? endfor nl
|
||||
|
|
|
|||
21
Task/Semordnilap/Pluto/semordnilap.pluto
Normal file
21
Task/Semordnilap/Pluto/semordnilap.pluto
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
local file = io.open("unixdict.txt", "r")
|
||||
local wmap = {}
|
||||
for word in file:lines() do wmap[word] = true end
|
||||
local pairs = {}
|
||||
local used = {}
|
||||
file:seek("set")
|
||||
for word in file:lines() do
|
||||
if word != "" then
|
||||
local pal = string.reverse(word)
|
||||
if word != pal and wmap[pal] and !used[pal] then
|
||||
pairs:insert({word, pal})
|
||||
used[word] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
file:close()
|
||||
print($"There are {#pairs} unique semordnilap pairs in the dictionary.")
|
||||
print("\nIn sorted order, the first five are:")
|
||||
for i = 1, 5 do print($" {pairs[i][1]}, {pairs[i][2]}") end
|
||||
print("\nand the last five are:")
|
||||
for i = #pairs - 4, #pairs do print($" {pairs[i][1]}, {pairs[i][2]}") end
|
||||
28
Task/Semordnilap/Prolog/semordnilap.pro
Normal file
28
Task/Semordnilap/Prolog/semordnilap.pro
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
:- initialization(main, main).
|
||||
:- use_module(library(apply)).
|
||||
:- use_module(library(dcg/basics)).
|
||||
:- use_module(library(dcg/high_order)).
|
||||
:- use_module(library(ordsets)).
|
||||
|
||||
load(Filename, Words) :- once(phrase_from_file(sequence(word, Words), Filename)).
|
||||
word(Word) --> nonblanks(Word), eol, { Word \= [] }.
|
||||
|
||||
find_all_semordnilaps(Words, Semordnilaps) :-
|
||||
convlist([Word, Reversed] >> (
|
||||
reverse(Word, Reversed),
|
||||
Reversed @< Word
|
||||
), Words, ReversedWords0),
|
||||
sort(ReversedWords0, ReversedWords),
|
||||
ord_intersection(Words, ReversedWords, Semordnilaps).
|
||||
|
||||
main([Filename]) :-
|
||||
load(Filename, Words),
|
||||
find_all_semordnilaps(Words, Semordnilaps),
|
||||
length(Semordnilaps, Count),
|
||||
format("~d~n", Count),
|
||||
length(FirstFive, 5),
|
||||
prefix(FirstFive, Semordnilaps),
|
||||
maplist([Word] >> (
|
||||
reverse(Word, Reversed),
|
||||
format("~s / ~s~n", [Word, Reversed])
|
||||
), FirstFive).
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
>>> with open('unixdict.txt') as f:
|
||||
wordset = set(f.read().strip().split())
|
||||
wordset = set(f.read().strip().split())
|
||||
|
||||
>>> revlist = (''.join(word[::-1]) for word in wordset)
|
||||
>>> pairs = set((word, rev) for word, rev in zip(wordset, revlist)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
library(stringi)
|
||||
|
||||
unixdict <- read.table("unixdict.txt", col.names="forwards")
|
||||
unixdict <- read.table("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt", col.names="forwards")
|
||||
unixdict$backwards <- stri_reverse(unixdict$forwards)
|
||||
|
||||
#Remove all actual palindromes, as we do not want those
|
||||
|
|
|
|||
26
Task/Semordnilap/REBOL/semordnilap-1.rebol
Normal file
26
Task/Semordnilap/REBOL/semordnilap-1.rebol
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Semordnilap"
|
||||
file: %Semordnilap.r
|
||||
url: https://rosettacode.org/wiki/Semordnilap
|
||||
needs: 2.7.0
|
||||
]
|
||||
|
||||
words: read/lines http://wiki.puzzlers.org/pub/wordlists/unixdict.txt
|
||||
seen: make hash! 200
|
||||
count: 0
|
||||
|
||||
print ["Number of input words: " length? words LF]
|
||||
|
||||
foreach word words [
|
||||
reversed: reverse copy word
|
||||
either find seen reversed [
|
||||
count: count + 1
|
||||
if count <= 5 [
|
||||
print [word "is reversed:" reversed]
|
||||
]
|
||||
][
|
||||
append seen word
|
||||
]
|
||||
]
|
||||
|
||||
print [LF "Found total" count "pairs."]
|
||||
25
Task/Semordnilap/REBOL/semordnilap-2.rebol
Normal file
25
Task/Semordnilap/REBOL/semordnilap-2.rebol
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Semordnilap"
|
||||
file: %Semordnilap.r3
|
||||
url: https://rosettacode.org/wiki/Semordnilap
|
||||
needs: 3.14.0
|
||||
]
|
||||
|
||||
words: read/lines http://wiki.puzzlers.org/pub/wordlists/unixdict.txt
|
||||
seen: make map! 200
|
||||
count: 0
|
||||
|
||||
print ["Number of input words: " length? words LF]
|
||||
|
||||
foreach word words [
|
||||
reversed: reverse copy word
|
||||
either find seen reversed [
|
||||
if ++ count <= 5 [
|
||||
print [word "is reversed:" reversed]
|
||||
]
|
||||
][
|
||||
put seen word reversed
|
||||
]
|
||||
]
|
||||
|
||||
print [LF "Found total" count "pairs."]
|
||||
|
|
@ -7,28 +7,28 @@
|
|||
* Reads file unixdict.txt
|
||||
* Comment: Tested using the Spitbol for Linux version of SNOBOL4
|
||||
|
||||
output = "Some Semordnilap Pairs from File unixdict.txt"
|
||||
atable = table(25200,,-1)
|
||||
ntable = table(25200,,-2)
|
||||
output = "Some Semordnilap Pairs from File unixdict.txt"
|
||||
atable = table(25200,,-1)
|
||||
ntable = table(25200,,-2)
|
||||
|
||||
* Read dictionary file into memory
|
||||
in1
|
||||
word = input :f(p1)
|
||||
count = count + 1
|
||||
atable[word] = word
|
||||
ntable[count] = word
|
||||
:(in1)
|
||||
word = input :f(p1)
|
||||
count = count + 1
|
||||
atable[word] = word
|
||||
ntable[count] = word
|
||||
:(in1)
|
||||
|
||||
* Process dictionary to find unique semordnilaps
|
||||
p1
|
||||
i = lt(i,count) i + 1 :f(p2)
|
||||
newword = atable[reverse(ntable[i])]
|
||||
leq(newword,-1) :s(p1)
|
||||
ident(ntable[i],newword) :s(p1)
|
||||
output = lt(outcount,5) ntable[i] ', ' newword
|
||||
atable[ntable[i]] = atable[newword] = -1
|
||||
outcount = outcount + 1
|
||||
:(p1)
|
||||
i = lt(i,count) i + 1 :f(p2)
|
||||
newword = atable[reverse(ntable[i])]
|
||||
leq(newword,-1) :s(p1)
|
||||
ident(ntable[i],newword) :s(p1)
|
||||
output = lt(outcount,5) ntable[i] ', ' newword
|
||||
atable[ntable[i]] = atable[newword] = -1
|
||||
outcount = outcount + 1
|
||||
:(p1)
|
||||
p2
|
||||
output = 'The number of semordnilap pairs is: ' outcount
|
||||
output = 'The number of semordnilap pairs is: ' outcount
|
||||
END
|
||||
|
|
|
|||
22
Task/Semordnilap/TAV/semordnilap.tav
Normal file
22
Task/Semordnilap/TAV/semordnilap.tav
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
main(params):+
|
||||
seen =: new map
|
||||
inwrds =: 0
|
||||
pairs =: 0
|
||||
palindromes =: 0
|
||||
?# line =: file 'unixdict.txt' give lines
|
||||
inwrds =+ 1
|
||||
wrd =: string line case to lower
|
||||
rwrd =: string wrd from -1 length wrd.Count step -1
|
||||
? wrd = rwrd
|
||||
palindromes =+ 1
|
||||
?^
|
||||
? seen{rwrd} = ()
|
||||
seen{wrd} =: line \ not yet seen, remember
|
||||
|
|
||||
pairs =+ 1
|
||||
? pairs < 7
|
||||
print wrd __ rwrd
|
||||
|
||||
print pairs, "pairs, " nonl
|
||||
print palindromes, "palindromes, " nonl
|
||||
print inwrds, "input words."
|
||||
|
|
@ -14,10 +14,10 @@ foreach word $wordlist {
|
|||
# Find where a reversal exists
|
||||
foreach word $wordlist {
|
||||
if {[info exists reversed($word)] && $word ne [string reverse $word]} {
|
||||
# Remove to prevent pairs from being printed twice
|
||||
unset reversed([string reverse $word])
|
||||
# Add to collection of pairs
|
||||
set pairs($word/[string reverse $word]) "dummy"
|
||||
# Remove to prevent pairs from being printed twice
|
||||
unset reversed([string reverse $word])
|
||||
# Add to collection of pairs
|
||||
set pairs($word/[string reverse $word]) "dummy"
|
||||
}
|
||||
}
|
||||
set pairlist [array names pairs] ;# NB: pairs are in *arbitrary* order
|
||||
|
|
|
|||
|
|
@ -1,30 +1,30 @@
|
|||
Set objFSO = CreateObject("Scripting.FileSystemObject")
|
||||
Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
|
||||
"\unixdict.txt",1)
|
||||
"\unixdict.txt",1)
|
||||
|
||||
Set objUnixDict = CreateObject("Scripting.Dictionary")
|
||||
Set objSemordnilap = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Do Until objInFile.AtEndOfStream
|
||||
line = objInFile.ReadLine
|
||||
If Len(line) > 1 Then
|
||||
objUnixDict.Add line,""
|
||||
End If
|
||||
reverse_line = StrReverse(line)
|
||||
If reverse_line <> line And objUnixDict.Exists(reverse_line) Then
|
||||
objSemordnilap.Add line, reverse_line
|
||||
End If
|
||||
Loop
|
||||
line = objInFile.ReadLine
|
||||
If Len(line) > 1 Then
|
||||
objUnixDict.Add line,""
|
||||
End If
|
||||
reverse_line = StrReverse(line)
|
||||
If reverse_line <> line And objUnixDict.Exists(reverse_line) Then
|
||||
objSemordnilap.Add line, reverse_line
|
||||
End If
|
||||
Loop
|
||||
|
||||
'Display the first 5 keys.
|
||||
k = 0
|
||||
For Each Key In objSemordnilap.Keys
|
||||
WScript.StdOut.Write Key & " - " & objSemordnilap.Item(Key)
|
||||
WScript.StdOut.WriteLine
|
||||
k = k + 1
|
||||
If k = 5 Then
|
||||
Exit For
|
||||
End If
|
||||
WScript.StdOut.Write Key & " - " & objSemordnilap.Item(Key)
|
||||
WScript.StdOut.WriteLine
|
||||
k = k + 1
|
||||
If k = 5 Then
|
||||
Exit For
|
||||
End If
|
||||
Next
|
||||
|
||||
WScript.StdOut.Write "Total Count: " & objSemordnilap.Count
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue