Ordered words: Difference between revisions

→‎{{header|ALGOL 68}}: shorter, and not relying on external code
(→‎{{header|ALGOL 68}}: shorter, and not relying on external code)
Line 217:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}.
<lang algol68>PROC ordered = (STRING s)BOOL:
The Algol 68 G "read" PRAGMA is used to include the associative array code from [[Associative_array/Iteration]].
BEGIN
<br>
FOR i TO UPB s - 1 DO IF s[i] > s[i+1] THEN return false FI OD;
Note that as the associative array does not store the elements in sorted order, the output is not sorted.
TRUE EXIT
<lang algol68># find the longrst words in a list that have all letters in order #
return false: FALSE
# use the associative array in the Associate array/iteration task #
END;
PR read "aArray.a68" PR
 
IF FILE input file;
# returns the length of word #
STRING file name = "unixdict.txt";
PROC length = ( STRING word )INT: 1 + ( UPB word - LWB word );
open(input file, file name, stand in channel) /= 0
THEN
print(("Unable to open file """ + file name + """", newline))
ELSE
BOOL at eof := FALSE;
on logical file end (input file, (REF FILE f)BOOL: at eof := TRUE);
 
FLEX [1:0] STRING words;
# returns text with the characters sorted into ascending order #
INT idx := 1;
PROC char sort = ( STRING text )STRING:
INT max length BEGIN:= 0;
STRING sorted := text;
FOR end pos FROM UPB sorted - 1 BY -1 TO LWB sorted
WHILE
BOOL swapped := FALSE;
FOR pos FROM LWB sorted TO end pos DO
IF sorted[ pos ] > sorted[ pos + 1 ]
THEN
CHAR t := sorted[ pos ];
sorted[ pos ] := sorted[ pos + 1 ];
sorted[ pos + 1 ] := t;
swapped := TRUE
FI
OD;
swapped
DO SKIP OD;
sorted
END # char sort # ;
 
WHILE NOT at eof
# read the list of words and store the ordered ones in an associative array #
DO
 
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
);
# store the ordered words and find the longest #
INT max length := 0;
REF AARRAY words := INIT LOC AARRAY;
STRING word;
get(input file, (word, newline));
WHILE NOT at eof
IF UPB word >= max length
DO
THEN IF STRING ordered(word;)
THEN
get( input file, ( word, newline ) );
IF char sort( wordmax )length := UPB word;
THEN IF idx > UPB words
# have an ordered word #THEN
IF [1 INT: wordUPB lengthwords :=+ length(20] wordSTRING )tmp;
tmp[1 : UPB wordwords] length:= > max lengthwords;
THEN words := tmp
max length := word lengthFI;
words[idx] FI:= word;
idx #+:= store the word #1
words // word := ""FI
FI
OD;
print(("Maximum length of ordered words: ", whole(max length, -4), newline));
# close the file #
FOR i TO idx-1
close( input file );
DO
 
print( ( "Maximum length ofIF orderedUPB words:[i] ", whole(= max length, -4THEN )print((words[i], newline ) ); FI
OD
# show the ordered words with the maximum length #
REF AAELEMENT e := FIRST words;
WHILE e ISNT nil element DO
IF max length = length( key OF e )
THEN
print( ( key OF e, newline ) )
FI;
e := NEXT words
OD
FI</lang>
{{out}}
<pre>Maximum length of ordered words: 6
<pre>
abbott
Maximum length of ordered words: 6
accent
accept
access
abbott
accost
almost
chilly
bellow
effort
billow
biopsy
almost
chilly
choosy
choppy
effort
choosy
biopsy
floppy
glossy
26

edits