Ordered words: Difference between revisions

Added BaCon
m (It occurs to me since my last edit that it would be possible in TI-83 BASIC, you'd just need to preload unixdict.txt into Str1 or something. Strings in this language actually do allow for newlines, IIRC.)
(Added BaCon)
Line 355:
 
<pre>$ awk -f ordered-words.awk unixdict.txt
abbott
accent
accept
access
accost
almost
bellow
billow
biopsy
chilly
choosy
choppy
effort
floppy
glossy
knotty</pre>
 
=={{header|BaCon}}==
<lang freebasic>' Ordered words
elements = 5
DECLARE wordlist$ TYPE STRING ARRAY elements
 
FUNCTION ordered(s$)
f = ASC(CHR$(s$[0]))
FOR i = 1 TO LEN(s$) - 1
IF ASC(CHR$(s$[i])) < f THEN
RETURN FALSE
ELSE
f = ASC(CHR$(s$[i]))
ENDIF
NEXT
RETURN TRUE
END FUNCTION
 
' Get words list filename from command line
wordfile$ = TOKEN$(ARGUMENT$, 2)
IF wordfile$ = "" OR wordfile$ = TOKEN$(ARGUMENT$, 1) THEN
wordfile$ = "/usr/local/share/dict/unixdict.txt"
ENDIF
 
OPEN wordfile$ FOR READING AS words
cnt = 0
 
READLN word$ FROM words
longest = LEN(word$)
 
WHILE NOT(ENDFILE(words)) DO
wl = LEN(word$)
' reset list on new longest
IF wl > longest AND ordered(word$) THEN
cnt = 0
wordlist$[0] = word$
longest = wl
ELIF wl = longest AND ordered(word$) THEN
INCR cnt
' Double the size of the array if needed
IF cnt >= elements THEN
elements = elements * 2
REDIM wordlist$ TO elements
ENDIF
wordlist$[cnt] = word$
ENDIF
READLN word$ FROM words
WEND
 
CLOSE FILE words
 
FOR i = 0 TO cnt
PRINT wordlist$[i]
NEXT</lang>
 
{{out}}
<pre>prompt$ bacon ordered-words.bac
Converting 'ordered-words.bac'... done, 52 lines were processed in 0.010 seconds.
Compiling 'ordered-words.bac'... cc -c ordered-words.bac.c
cc -o ordered-words ordered-words.bac.o -lbacon -lm
Done, program 'ordered-words' ready.
prompt$ ./ordered-words
abbott
accent
Anonymous user