Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -1,62 +1,55 @@
|
|||
# find the semordnilaps in a list of words #
|
||||
# use the associative array in the Associate array/iteration task #
|
||||
PR read "aArray.a68" PR
|
||||
BEGIN # find the semordnilaps (words that are the reverse of another word) #
|
||||
|
||||
# returns text with the characters reversed #
|
||||
OP REVERSE = ( STRING text )STRING:
|
||||
BEGIN
|
||||
STRING reversed := text;
|
||||
INT start pos := LWB text;
|
||||
FOR end pos FROM UPB reversed BY -1 TO LWB reversed
|
||||
DO
|
||||
reversed[ end pos ] := text[ start pos ];
|
||||
start pos +:= 1
|
||||
OD;
|
||||
reversed
|
||||
END # REVERSE # ;
|
||||
PR read "files.incl.a68" PR # include file utilities #
|
||||
|
||||
# read the list of words and store the words in an associative array #
|
||||
# check for semordnilaps #
|
||||
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;
|
||||
INT semordnilap count := 0;
|
||||
WHILE NOT at eof
|
||||
DO
|
||||
STRING word;
|
||||
get( input file, ( word, newline ) );
|
||||
STRING reversed word = REVERSE word;
|
||||
IF ( words // reversed word ) = ""
|
||||
THEN
|
||||
# the reversed word isn't in the array #
|
||||
words // word := reversed word
|
||||
ELSE
|
||||
# we already have this reversed - we have a semordnilap #
|
||||
semordnilap count +:= 1;
|
||||
IF semordnilap count <= 5
|
||||
THEN
|
||||
print( ( reversed word, " & ", word, newline ) )
|
||||
FI
|
||||
FI
|
||||
OD;
|
||||
close( input file );
|
||||
print( ( whole( semordnilap count, 0 ), " semordnilaps found", newline ) )
|
||||
FI
|
||||
[ 1 : 30 000 ]STRING words; # guess that this will be enough words #
|
||||
INT semordnilap count := 0; # number of semordnilaps found so far #
|
||||
INT max report = 5; # number of semordnilaps to show #
|
||||
|
||||
# returns TRUE if words[ low : high ] comntains s, FALSE otherwise #
|
||||
PROC is word = ( STRING s, INT low, high )BOOL:
|
||||
IF high < low THEN FALSE
|
||||
ELSE INT mid = ( low + high ) OVER 2;
|
||||
IF words[ mid ] > s THEN is word( s, low, mid - 1 )
|
||||
ELIF words[ mid ] = s THEN TRUE
|
||||
ELSE is word( s, mid + 1, high )
|
||||
FI
|
||||
FI # is word # ;
|
||||
|
||||
# returns text with the characters reversed #
|
||||
OP REVERSE = ( STRING text )STRING:
|
||||
BEGIN
|
||||
STRING reversed := text;
|
||||
INT start pos := LWB text;
|
||||
FOR end pos FROM UPB reversed BY -1 TO LWB reversed
|
||||
DO
|
||||
reversed[ end pos ] := text[ start pos ];
|
||||
start pos +:= 1
|
||||
OD;
|
||||
reversed
|
||||
END # REVERSE # ;
|
||||
|
||||
# returns FALSE if the reverse of word is in words, TRUE otherwise #
|
||||
# if the reverse is not present, it is added to words #
|
||||
# if the reverse is present, it is reported as a seordnilap - if the #
|
||||
# maximum number hasn't been reported yet #
|
||||
# count so far will contain the number of words stored so far #
|
||||
PROC store non semordnilaps = ( STRING word, INT count so far )BOOL:
|
||||
IF STRING r word = REVERSE word;
|
||||
is word( r word, 1, count so far )
|
||||
THEN IF ( semordnilap count +:= 1 ) <= max report
|
||||
THEN print( ( word, " & ", r word, newline ) )
|
||||
FI;
|
||||
FALSE
|
||||
ELSE words[ count so far + 1 ] := word;
|
||||
TRUE
|
||||
FI # store non semordnilaps # ;
|
||||
|
||||
|
||||
# find the semordnilaps - assumes unixdict.txt is sorted #
|
||||
IF "unixdict.txt" EACHLINE store non semordnilaps < 0
|
||||
THEN print( ( "Unable to open unixdict.txt", newline ) )
|
||||
ELSE print( ( whole( semordnilap count, 0 ), " semordnilaps found", newline ) )
|
||||
FI
|
||||
|
||||
END
|
||||
|
|
|
|||
45
Task/Semordnilap/EasyLang/semordnilap.easy
Normal file
45
Task/Semordnilap/EasyLang/semordnilap.easy
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
repeat
|
||||
s$ = input
|
||||
until s$ = ""
|
||||
w$[] &= s$
|
||||
.
|
||||
func$ reverse s$ .
|
||||
a$[] = strchars s$
|
||||
for i = 1 to len a$[] div 2
|
||||
swap a$[i] a$[len a$[] - i + 1]
|
||||
.
|
||||
return strjoin a$[]
|
||||
.
|
||||
func search s$ .
|
||||
max = len w$[] + 1
|
||||
while min + 1 < max
|
||||
mid = min + (max - min) div 2
|
||||
h = strcmp w$[mid] s$
|
||||
if h = 0
|
||||
return 1
|
||||
elif h < 0
|
||||
min = mid
|
||||
else
|
||||
max = mid
|
||||
.
|
||||
.
|
||||
return 0
|
||||
.
|
||||
for w$ in w$[]
|
||||
r$ = reverse w$
|
||||
if strcmp r$ w$ < 0
|
||||
if search r$ = 1
|
||||
cnt += 1
|
||||
if cnt <= 5
|
||||
print w$ & " " & r$
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
print cnt
|
||||
# the content of unixdict.txt
|
||||
input_data
|
||||
10th
|
||||
.
|
||||
avid
|
||||
diva
|
||||
37
Task/Semordnilap/Ed/semordnilap.ed
Normal file
37
Task/Semordnilap/Ed/semordnilap.ed
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# by Artyom Bologov
|
||||
H
|
||||
# Remove words longer than 9 chars (unsupported)
|
||||
g/^.\{9,\}$/d
|
||||
# Join words insterspersed with |
|
||||
g/./s/$/|/
|
||||
,j
|
||||
# Put exclamations before the semordnilaps words
|
||||
g/./s/|\([^|]\)\([^|]\)\(|.*|\)\2\1|/|!\1\2\3/g
|
||||
g/./s/|\([^|]\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\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\(|.*|\)\6\5\4\3\2\1|/|!\1\2\3\4\5\6\7/g
|
||||
g/./s/|\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\(|.*|\)\7\6\5\4\3\2\1|/|!\1\2\3\4\5\6\7\8/g
|
||||
g/./s/|\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\(|.*|\)\7\6\5\4\3\2\1|/|!\1\2\3\4\5\6\7\8/g
|
||||
g/./s/|\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\([^|]\)\(|.*|\)\8\7\6\5\4\3\2\1|/|!\1\2\3\4\5\6\7\8\9/g
|
||||
# Split on exclamation
|
||||
g/./s/!/\
|
||||
/g
|
||||
# Remove the first (non-exclamated) line
|
||||
1d
|
||||
# Remove the rest of the line after semordnilap
|
||||
g/|/s/\([^|]*\)|.*/\1/
|
||||
,p
|
||||
Q
|
||||
Loading…
Add table
Add a link
Reference in a new issue