Ordered words: Difference between revisions
imported>GoulashAmateur m (Fix functional CL dict path) |
|||
(70 intermediate revisions by 39 users not shown) | |||
Line 7: | Line 7: | ||
{{task heading}} |
{{task heading}} |
||
Find ''and display'' all the ordered words in the dictionary [http://www.puzzlers.org/pub/wordlists/unixdict.txt unixdict.txt] that have the longest word length. |
Find ''and display'' all the ordered words in the dictionary [https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt unixdict.txt] that have the longest word length. |
||
(Examples that access the dictionary file locally assume that you have downloaded this file yourself.) |
(Examples that access the dictionary file locally assume that you have downloaded this file yourself.) |
||
Line 17: | Line 17: | ||
{{Related tasks/Word plays}} |
{{Related tasks/Word plays}} |
||
<hr> |
|||
{{Template:Strings}} |
|||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">V words = File(‘unixdict.txt’).read().split("\n") |
|||
V ordered = words.filter(word -> word == sorted(word)) |
|||
V maxlen = max(ordered, key' w -> w.len).len |
|||
V maxorderedwords = ordered.filter(word -> word.len == :maxlen) |
|||
print(maxorderedwords.join(‘ ’))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
|||
</pre> |
|||
=={{header|Acornsoft Lisp}}== |
|||
In <code>find-longest-ordered-words</code>, <code>len</code>, <code>word</code>, and <code>words</code> are used as local variables. <code>open</code> returns an integer as a file-handle. <code>chars</code> returns the number of characters in a symbol's name. <code>eq</code> can be used to compare numbers for equality. |
|||
<syntaxhighlight lang="lisp"> |
|||
(defun longest-ordered-words () |
|||
(find-longest-ordered-words |
|||
(open 'notes/unixdict!.txt t))) |
|||
(defun find-longest-ordered-words |
|||
(h (len . 0) (word) (words)) |
|||
(loop |
|||
(until (eof h) |
|||
(close h) |
|||
words) |
|||
(setq word (readline h)) |
|||
(cond ((lessp (chars word) len)) |
|||
((not (ordered-p word))) |
|||
((eq (chars word) len) |
|||
(setq words (cons word words))) |
|||
(t |
|||
(setq len (chars word)) |
|||
(setq words (list word)))))) |
|||
(defun ordered-p (word) |
|||
(nondecreasing-p |
|||
(mapc ordinal (explode word)))) |
|||
(defun nondecreasing-p (numbers) |
|||
(or (null numbers) |
|||
(null (cdr numbers)) |
|||
(and (not (greaterp (car numbers) (cadr numbers))) |
|||
(nondecreasing-p (cdr numbers))))) |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<code>(longest-ordered-words)</code> will return |
|||
<pre> |
|||
(knotty glossy floppy effort |
|||
choppy choosy chilly biopsy |
|||
billow bellow almost accost |
|||
access accept accent abbott) |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
In the following solution the input file is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used. |
|||
<syntaxhighlight lang="action!">CHAR ARRAY line(256) |
|||
BYTE FUNC IsOrderedWord(CHAR ARRAY word) |
|||
BYTE len,i |
|||
len=word(0) |
|||
IF len<=1 THEN RETURN (1) FI |
|||
FOR i=1 TO len-1 |
|||
DO |
|||
IF word(i)>word(i+1) THEN |
|||
RETURN (0) |
|||
FI |
|||
OD |
|||
RETURN (1) |
|||
BYTE FUNC FindLongestOrdered(CHAR ARRAY fname) |
|||
BYTE max,dev=[1] |
|||
max=0 |
|||
Close(dev) |
|||
Open(dev,fname,4) |
|||
WHILE Eof(dev)=0 |
|||
DO |
|||
InputSD(dev,line) |
|||
IF line(0)>max AND IsOrderedWord(line)=1 THEN |
|||
max=line(0) |
|||
FI |
|||
OD |
|||
Close(dev) |
|||
RETURN (max) |
|||
PROC FindWords(CHAR ARRAY fname BYTE n) |
|||
BYTE count,dev=[1] |
|||
Close(dev) |
|||
Open(dev,fname,4) |
|||
WHILE Eof(dev)=0 |
|||
DO |
|||
InputSD(dev,line) |
|||
IF line(0)=n AND IsOrderedWord(line)=1 THEN |
|||
Print(line) Put(32) |
|||
FI |
|||
OD |
|||
Close(dev) |
|||
RETURN |
|||
PROC Main() |
|||
CHAR ARRAY fname="H6:UNIXDICT.TXT" |
|||
BYTE max |
|||
PrintE("Finding the longest words...") |
|||
PutE() |
|||
max=FindLongestOrdered(fname) |
|||
FindWords(fname,max) |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ordered_words.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Finding the longest words... |
|||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; |
with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; |
||
use Ada.Text_IO; |
use Ada.Text_IO; |
||
Line 54: | Line 182: | ||
Close (File); |
Close (File); |
||
end Ordered_Words; |
end Ordered_Words; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 75: | Line 203: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">integer |
||
ordered(data s) |
ordered(data s) |
||
{ |
{ |
||
Line 113: | Line 241: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>abbott |
<pre>abbott |
||
Line 134: | Line 262: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}. |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}. |
||
<syntaxhighlight 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 |
|||
# returns text with the characters sorted into ascending order # |
|||
PROC char sort = ( STRING text )STRING: |
|||
BEGIN |
|||
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 # ; |
|||
# read the list of words and store the ordered ones in an associative array # |
|||
IF FILE input file; |
|||
STRING file name = "unixdict.txt"; |
|||
open( input file, file name, stand in channel ) /= 0 |
|||
THEN |
THEN |
||
print(("Unable to open file """ + file name + """", newline)) |
|||
print( ( "Unable to open """ + file name + """", newline ) ) |
|||
ELSE |
ELSE |
||
BOOL at eof := FALSE; |
|||
# file opened OK # |
|||
BOOL at eof := |
on logical file end (input file, (REF FILE f)BOOL: at eof := TRUE); |
||
# set the EOF handler for the file # |
|||
FLEX [1:0] STRING words; |
|||
on logical file end( input file, ( REF FILE f )BOOL: |
|||
INT idx := 1; |
|||
BEGIN |
|||
INT max length := 0; |
|||
# note that we reached EOF on the # |
|||
# latest read # |
|||
WHILE NOT at eof |
|||
at eof := TRUE; |
|||
DO |
|||
# 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; |
STRING word; |
||
get(input file, (word, newline)); |
|||
WHILE NOT at eof |
|||
IF UPB word >= max length |
|||
DO |
|||
THEN IF ordered(word) |
|||
THEN |
|||
get( input file, ( word, newline ) ); |
|||
max length := UPB word; |
|||
IF idx > UPB words |
|||
THEN |
|||
[1 : UPB words + 20] STRING tmp; |
|||
tmp[1 : UPB words] := words; |
|||
words := tmp |
|||
FI; |
|||
words[idx] := word; |
|||
idx +:= 1 |
|||
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 |
|||
IF UPB words[i] = max length THEN print((words[i], newline)) FI |
|||
OD |
|||
FI</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum length of ordered words: 6 |
|||
abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|Amazing Hopper}}== |
|||
print( ( "Maximum length of ordered words: ", whole( max length, -4 ), newline ) ); |
|||
<syntaxhighlight lang="c"> |
|||
# show the ordered words with the maximum length # |
|||
#include <basico.h> |
|||
REF AAELEMENT e := FIRST words; |
|||
WHILE e ISNT nil element DO |
|||
#define MAX_LINE 30 |
|||
IF max length = length( key OF e ) |
|||
THEN |
|||
algoritmo |
|||
print( ( key OF e, newline ) ) |
|||
fd=0 |
|||
word={}, result={} |
|||
OD |
|||
old_word="",new_word="" |
|||
FI</lang> |
|||
fijar separador (NULO) |
|||
abrir para leer("basica/unixdict.txt",fd) |
|||
iterar mientras ' no es fin de archivo (fd) ' |
|||
usando 'MAX_LINE', leer línea desde(fd), |
|||
---copiar en 'old_word'---, separar para 'word ' |
|||
word, ---retener--- ordenar esto, |
|||
encadenar en 'new_word' |
|||
new_word, meter según (#(old_word == new_word), result) |
|||
reiterar |
|||
cerrar archivo(fd) |
|||
result ---retener---, obtener largo ---retener--- |
|||
obtener máximo valor, es mayor o igual?, replicar esto |
|||
compactar esto |
|||
fijar separador 'NL', luego imprime todo |
|||
terminar |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
abbott |
|||
Maximum length of ordered words: 6 |
|||
accent |
accent |
||
accept |
accept |
||
access |
access |
||
abbott |
|||
accost |
accost |
||
almost |
|||
chilly |
|||
bellow |
bellow |
||
effort |
|||
billow |
billow |
||
almost |
|||
choppy |
|||
choosy |
|||
biopsy |
biopsy |
||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
floppy |
||
glossy |
glossy |
||
knotty |
knotty |
||
</pre> |
|||
=={{header|APL}}== |
|||
Works in NARS2000 APL; ("Objects" menu -> New, paste this in, save. Then run in the main session manager). |
|||
<syntaxhighlight lang="apl">result←longest_ordered_words file_path |
|||
f←file_path ⎕NTIE 0 ⍝ open file |
|||
text←⎕NREAD f 'char8' ⍝ read vector of 8bit chars |
|||
⎕NUNTIE f ⍝ close file |
|||
lines←text⊂⍨~text∊(⎕UCS 10 13) ⍝ split into lines (\r\n) |
|||
⍝ filter only words with ordered characters |
|||
ordered_words←lines/⍨{(⍳∘≢≡⍋)⍵}¨lines |
|||
⍝ find max of word lengths, filter only words with that length |
|||
result←ordered_words/⍨lengths=⍨⌈/lengths←≢¨ordered_words |
|||
</syntaxhighlight> |
|||
The ordered character filter is a train which uses gradeup to say which order you would have to pick the characters, to put them in order. e.g. ⍋ 'zxy' is 2 3 1 because you'd have to pick the second character, then the third, then the first, to put them in order. If they are in order then the result is the integers 1 2 3 .. to the length of the word. |
|||
And it generates the integers up to the length of the word, e.g. 1 2 3. and compares if those two arrays are the same. |
|||
Then uses that information as a filter against the original word list. |
|||
The result is: lengths of each ordered word, which equal the max-reduce of the list of lengths (longest word), used as a filter against the ordered word list. |
|||
{{out}} |
|||
<pre> |
|||
longest_ordered_words 'd:\unixdict.txt' |
|||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
|||
</pre> |
|||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS 10.9 (Mavericks) or later — for these 'use' commands. |
|||
use sorter : script "Insertion sort" -- https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript. |
|||
use scripting additions |
|||
on longestOrderedWords(wordList) |
|||
script o |
|||
property allWords : wordList |
|||
property orderedWords : {} |
|||
end script |
|||
set longestWordLength to 0 |
|||
set astid to AppleScript's text item delimiters |
|||
set AppleScript's text item delimiters to "" |
|||
ignoring case |
|||
repeat with i from 1 to (count o's allWords) |
|||
set thisWord to item i of o's allWords |
|||
set thisWordLength to (count thisWord) |
|||
if (thisWordLength ≥ longestWordLength) then |
|||
set theseCharacters to thisWord's characters |
|||
tell sorter to sort(theseCharacters, 1, -1) |
|||
set sortedWord to theseCharacters as text |
|||
if (sortedWord = thisWord) then |
|||
if (thisWordLength > longestWordLength) then |
|||
set o's orderedWords to {thisWord} |
|||
set longestWordLength to thisWordLength |
|||
else |
|||
set end of o's orderedWords to thisWord |
|||
end if |
|||
end if |
|||
end if |
|||
end repeat |
|||
end ignoring |
|||
set AppleScript's text item delimiters to astid |
|||
return (o's orderedWords) |
|||
end longestOrderedWords |
|||
-- Test code: |
|||
local wordList |
|||
set wordList to paragraphs of (read (((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as alias) as «class utf8») |
|||
-- ignoring white space, punctuation and diacriticals |
|||
return longestOrderedWords(wordList) |
|||
--- end ignoring</syntaxhighlight> |
|||
{{output}} |
|||
<syntaxhighlight lang="applescript">{"abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"}</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">ordered?: function [w]-> |
|||
w = join sort split w |
|||
words: read.lines relative "unixdict.txt" |
|||
ret: new [] |
|||
loop words 'wrd [ |
|||
if ordered? wrd -> |
|||
'ret ++ #[w: wrd l: size wrd] |
|||
] |
|||
sort.descending.by: 'l 'ret |
|||
maxl: get first ret 'l |
|||
print sort map select ret 'x -> maxl = x\l |
|||
'x -> x\w</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 244: | Line 486: | ||
StringLower could be used. This script assumes a locally downloaded copy of the dictionary, but UrlDownloadToFile could be used. |
StringLower could be used. This script assumes a locally downloaded copy of the dictionary, but UrlDownloadToFile could be used. |
||
The purpose of the GUI is simply to display a field where the user can copy the list. MsgBox could be used, or FileAppend. |
The purpose of the GUI is simply to display a field where the user can copy the list. MsgBox could be used, or FileAppend. |
||
<syntaxhighlight lang="autohotkey"> |
|||
<lang AutoHotkey> |
|||
MaxLen=0 |
MaxLen=0 |
||
Loop, Read, UnixDict.txt ; Assigns A_LoopReadLine to each line of the file |
Loop, Read, UnixDict.txt ; Assigns A_LoopReadLine to each line of the file |
||
Line 278: | Line 520: | ||
GUIClose: |
GUIClose: |
||
ExitApp |
ExitApp |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty</pre> |
<pre>abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
abc = "abcdefghijklmnopqrstuvwxyz" |
abc = "abcdefghijklmnopqrstuvwxyz" |
||
} |
} |
||
Line 318: | Line 560: | ||
for (i = 1; i <= best["count"]; i++) |
for (i = 1; i <= best["count"]; i++) |
||
print best[i] |
print best[i] |
||
}</ |
}</syntaxhighlight> |
||
You must provide <tt>unixdict.txt</tt> as input. |
You must provide <tt>unixdict.txt</tt> as input. |
||
Line 341: | Line 583: | ||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
< |
<syntaxhighlight lang="freebasic">'Ordered words - improved version |
||
OPTION COLLAPSE TRUE |
OPTION COLLAPSE TRUE |
||
Line 361: | Line 603: | ||
PRINT result$ |
PRINT result$ |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 389: | Line 631: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
An optimisation is that the word isn't checked for being ordered unless it is at least as long as the current maximum. |
An optimisation is that the word isn't checked for being ordered unless it is at least as long as the current maximum. |
||
< |
<syntaxhighlight lang="bbcbasic"> dict% = OPENIN("unixdict.txt") |
||
IF dict%=0 ERROR 100, "Failed to open dictionary file" |
IF dict%=0 ERROR 100, "Failed to open dictionary file" |
||
Line 407: | Line 649: | ||
CLOSE #dict% |
CLOSE #dict% |
||
PRINT list$ |
PRINT list$ |
||
END</ |
END</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 433: | Line 675: | ||
Memory usage isn't very efficient - the matching list is stored one word per line - so on a standard Befunge-93 interpreter there is an upper limit of 22 matches. This is not a problem for the unix dictionary, which only requires 16, but it's theoretically possible that other data sets could run out of space. |
Memory usage isn't very efficient - the matching list is stored one word per line - so on a standard Befunge-93 interpreter there is an upper limit of 22 matches. This is not a problem for the unix dictionary, which only requires 16, but it's theoretically possible that other data sets could run out of space. |
||
< |
<syntaxhighlight lang="befunge">00p30p>_010p120p0>#v0~>>\$::48*\`\"~"`+!>>#v_$:#v_>30g:!#v_1-30p55+0>:30g3+g\1v |
||
>0#v _$^#::\p04:<^+>#1^#\p01:p02*g02!`\g01:<@$ _ ,#!>#:<$<^<!:g03$<_^#!`\g00:+< |
>0#v _$^#::\p04:<^+>#1^#\p01:p02*g02!`\g01:<@$ _ ,#!>#:<$<^<!:g03$<_^#!`\g00:+< |
||
^<o>\30g2+p40g1+^0p00p03+1*g03!-g00 < < < < < <:>#$:#$00g#<\#<`#<!#<2#$0g#<*#<_</ |
^<o>\30g2+p40g1+^0p00p03+1*g03!-g00 < < < < < <:>#$:#$00g#<\#<`#<!#<2#$0g#<*#<_</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 455: | Line 697: | ||
knotty</pre> |
knotty</pre> |
||
=={{header|BQN}}== |
|||
<code>(↕∘≠≡⍋)</code> checks for an ordered word. |
|||
<code>¯1⊑(≠¨⊔⊢)</code> groups by length, and takes the last (longest length) group. |
|||
<syntaxhighlight lang="bqn"> words←•FLines "unixdict.txt" |
|||
¯1⊑(≠¨⊔⊢)(↕∘≠≡⍋)¨⊸/words |
|||
⟨ "abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" "biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty" ⟩</syntaxhighlight> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat"> ( orderedWords |
||
= bow result longestLength word character |
= bow result longestLength word character |
||
. 0:?bow |
. 0:?bow |
||
Line 480: | Line 730: | ||
| !result |
| !result |
||
) |
) |
||
& orderedWords$"unixdict.txt"</ |
& orderedWords$"unixdict.txt"</syntaxhighlight> |
||
<pre> knotty |
<pre> knotty |
||
glossy |
glossy |
||
Line 499: | Line 749: | ||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
< |
<syntaxhighlight lang="burlesque">ln{so}f[^^{L[}>mL[bx(==)[+(L[)+]f[uN</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 521: | Line 771: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 603: | Line 853: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>abbott |
<pre>abbott |
||
Line 622: | Line 872: | ||
knotty</pre> |
knotty</pre> |
||
Alternative version with dynamic array: |
Alternative version with dynamic array: |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 708: | Line 958: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
===Mmap=== |
===Mmap=== |
||
Shorter and potentially much faster version with <code>mmap (2)</code>. No stinky <code>malloc</code> or <code>scanf</code> calls. |
Shorter and potentially much faster version with <code>mmap (2)</code>. No stinky <code>malloc</code> or <code>scanf</code> calls. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <sys/mman.h> |
#include <sys/mman.h> |
||
#include <sys/types.h> |
#include <sys/types.h> |
||
Line 764: | Line 1,014: | ||
close(fd); |
close(fd); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp">using System; |
|||
using System.Linq; |
|||
using System.Net; |
|||
static class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
WebClient client = new WebClient(); |
|||
string text = client.DownloadString("http://www.puzzlers.org/pub/wordlists/unixdict.txt"); |
|||
string[] words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); |
|||
var query = from w in words |
|||
where IsOrderedWord(w) |
|||
group w by w.Length into ows |
|||
orderby ows.Key descending |
|||
select ows; |
|||
Console.WriteLine(string.Join(", ", query.First().ToArray())); |
|||
} |
|||
private static bool IsOrderedWord(string w) |
|||
{ |
|||
for (int i = 1; i < w.Length; i++) |
|||
if (w[i] < w[i - 1]) |
|||
return false; |
|||
return true; |
|||
} |
|||
}</syntaxhighlight> |
|||
Output: |
|||
<pre>abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <fstream> |
#include <fstream> |
||
#include <iostream> |
#include <iostream> |
||
Line 805: | Line 1,090: | ||
} |
} |
||
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n")); |
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n")); |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 825: | Line 1,110: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.Linq; |
|||
using System.Net; |
|||
static class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
WebClient client = new WebClient(); |
|||
string text = client.DownloadString("http://www.puzzlers.org/pub/wordlists/unixdict.txt"); |
|||
string[] words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); |
|||
var query = from w in words |
|||
where IsOrderedWord(w) |
|||
group w by w.Length into ows |
|||
orderby ows.Key descending |
|||
select ows; |
|||
Console.WriteLine(string.Join(", ", query.First().ToArray())); |
|||
} |
|||
private static bool IsOrderedWord(string w) |
|||
{ |
|||
for (int i = 1; i < w.Length; i++) |
|||
if (w[i] < w[i - 1]) |
|||
return false; |
|||
return true; |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre>abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn is-sorted? [coll] |
||
(not-any? pos? (map compare coll (next coll)))) |
(not-any? pos? (map compare coll (next coll)))) |
||
Line 878: | Line 1,127: | ||
take-while-eqcount |
take-while-eqcount |
||
(clojure.string/join ", ") |
(clojure.string/join ", ") |
||
println))</ |
println))</syntaxhighlight> |
||
Output |
Output |
||
< |
<syntaxhighlight lang="clojure">abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty</syntaxhighlight> |
||
=={{header|CLU}}== |
|||
<syntaxhighlight lang="clu">is_ordered = proc (s: string) returns (bool) |
|||
last: char := '\000' |
|||
for c: char in string$chars(s) do |
|||
if last > c then return(false) end |
|||
last := c |
|||
end |
|||
return(true) |
|||
end is_ordered |
|||
lines = iter (s: stream) yields (string) |
|||
while true do |
|||
yield(stream$getl(s)) |
|||
except when end_of_file: break end |
|||
end |
|||
end lines |
|||
ordered_words = proc (s: stream) returns (array[string]) |
|||
words: array[string] |
|||
max_len: int := 0 |
|||
for word: string in lines(s) do |
|||
if is_ordered(word) then |
|||
len: int := string$size(word) |
|||
if len > max_len then |
|||
max_len := len |
|||
words := array[string]$[] |
|||
elseif len = max_len then |
|||
array[string]$addh(words,word) |
|||
end |
|||
end |
|||
end |
|||
return(words) |
|||
end ordered_words |
|||
start_up = proc () |
|||
dict: stream := stream$open(file_name$parse("unixdict.txt"), "read") |
|||
words: array[string] := ordered_words(dict) |
|||
stream$close(dict) |
|||
po: stream := stream$primary_output() |
|||
for word: string in array[string]$elements(words) do |
|||
stream$putl(po, word) |
|||
end |
|||
end start_up</syntaxhighlight> |
|||
{{out}} |
|||
<pre>accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|COBOL}}== |
|||
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
|||
PROGRAM-ID. ABC-WORDS. |
|||
ENVIRONMENT DIVISION. |
|||
INPUT-OUTPUT SECTION. |
|||
FILE-CONTROL. |
|||
SELECT DICT ASSIGN TO DISK |
|||
ORGANIZATION LINE SEQUENTIAL. |
|||
DATA DIVISION. |
|||
FILE SECTION. |
|||
FD DICT |
|||
LABEL RECORD STANDARD |
|||
VALUE OF FILE-ID IS "unixdict.txt". |
|||
01 ENTRY. |
|||
03 WORD PIC X(32). |
|||
03 LETTERS PIC X OCCURS 32 TIMES, REDEFINES WORD. |
|||
WORKING-STORAGE SECTION. |
|||
01 LEN PIC 99. |
|||
01 MAXLEN PIC 99 VALUE 0. |
|||
01 I PIC 99. |
|||
01 OK-FLAG PIC X. |
|||
88 OK VALUE '*'. |
|||
PROCEDURE DIVISION. |
|||
BEGIN. |
|||
OPEN INPUT DICT. |
|||
FIND-LONGEST-WORD. |
|||
READ DICT, AT END CLOSE DICT, GO TO PRINT-LONGEST-WORDS. |
|||
PERFORM CHECK-WORD. |
|||
GO TO FIND-LONGEST-WORD. |
|||
PRINT-LONGEST-WORDS. |
|||
ALTER VALID-WORD TO PROCEED TO SHOW-WORD. |
|||
OPEN INPUT DICT. |
|||
READ-WORDS. |
|||
READ DICT, AT END CLOSE DICT, STOP RUN. |
|||
PERFORM CHECK-WORD. |
|||
GO TO READ-WORDS. |
|||
CHECK-WORD. |
|||
MOVE ZERO TO LEN. |
|||
INSPECT WORD TALLYING LEN |
|||
FOR CHARACTERS BEFORE INITIAL SPACE. |
|||
MOVE '*' TO OK-FLAG. |
|||
PERFORM CHECK-CHAR-PAIR VARYING I FROM 2 BY 1 |
|||
UNTIL NOT OK OR I IS GREATER THAN LEN. |
|||
IF OK, PERFORM DO-WORD. |
|||
CHECK-CHAR-PAIR. |
|||
IF LETTERS(I - 1) IS GREATER THAN LETTERS(I), |
|||
MOVE SPACE TO OK-FLAG. |
|||
DO-WORD SECTION. |
|||
VALID-WORD. |
|||
GO TO CHECK-LENGTH. |
|||
CHECK-LENGTH. |
|||
IF LEN IS GREATER THAN MAXLEN, MOVE LEN TO MAXLEN. |
|||
GO TO DONE. |
|||
SHOW-WORD. |
|||
IF LEN IS EQUAL TO MAXLEN, DISPLAY WORD. |
|||
DONE. |
|||
EXIT.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
ordered_word = (word) -> |
ordered_word = (word) -> |
||
for i in [0...word.length - 1] |
for i in [0...word.length - 1] |
||
Line 906: | Line 1,301: | ||
dict_words = file_content.toString().split '\n' |
dict_words = file_content.toString().split '\n' |
||
show_longest_ordered_words dict_words, dict_file_name |
show_longest_ordered_words dict_words, dict_file_name |
||
</syntaxhighlight> |
|||
</lang> |
|||
output |
output |
||
<lang> |
<syntaxhighlight lang="text"> |
||
> coffee ordered_words.coffee |
> coffee ordered_words.coffee |
||
Longest Ordered Words (source=unixdict.txt): |
Longest Ordered Words (source=unixdict.txt): |
||
Line 927: | Line 1,322: | ||
glossy |
glossy |
||
knotty |
knotty |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun orderedp (word) |
||
(reduce (lambda (prev curr) |
(reduce (lambda (prev curr) |
||
(when (char> prev curr) (return-from orderedp nil)) |
(when (char> prev curr) (return-from orderedp nil)) |
||
Line 956: | Line 1,351: | ||
("abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" |
("abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" |
||
"biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty") |
"biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty") |
||
</syntaxhighlight> |
|||
</lang> |
|||
Or in a more functional way: |
|||
<syntaxhighlight lang="lisp">(defun orderedp (word) |
|||
(apply #'char<= (coerce word 'list))) |
|||
(let* ((words (uiop:read-file-lines "unixdict.txt")) |
|||
(ordered (delete-if-not #'orderedp words)) |
|||
(maxlen (apply #'max (mapcar #'length ordered))) |
|||
(result (delete-if-not (lambda (l) (= l maxlen)) ordered :key #'length))) |
|||
(format t "~{~A~^, ~}" result))</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty</pre> |
|||
=={{header|Cowgol}}== |
|||
Because Cowgol is meant to run on small systems, this program does not store the words |
|||
in memory. Instead, it scans through the file twice; once to find the maximum length |
|||
of the ordered words it finds, and once to actually print the words. |
|||
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
|||
include "strings.coh"; |
|||
include "file.coh"; |
|||
var filename: [uint8] := "unixdict.txt"; |
|||
# Call a subroutine for every line in a file |
|||
interface LineCb(line: [uint8]); |
|||
sub ForEachLine(fcb: [FCB], fn: LineCb) is |
|||
var linebuf: uint8[256]; |
|||
var bufptr := &linebuf[0]; |
|||
var len := FCBExt(fcb); # get length of file |
|||
FCBSeek(fcb, 0); # start at beginning of file |
|||
while len > 0 loop |
|||
var ch := FCBGetChar(fcb); |
|||
if ch == '\n' then |
|||
# end of line, terminate string |
|||
[bufptr] := 0; |
|||
fn(&linebuf[0]); |
|||
bufptr := &linebuf[0]; |
|||
else |
|||
# add char to buffer |
|||
[bufptr] := ch; |
|||
bufptr := @next bufptr; |
|||
end if; |
|||
len := len - 1; |
|||
end loop; |
|||
# If the file doesn't cleanly end on a line terminator, |
|||
# also call for last incomplete line |
|||
if ch != '\n' then |
|||
[bufptr] := 0; |
|||
fn(&linebuf[0]); |
|||
end if; |
|||
end sub; |
|||
# Check if the letters in a word appear in alphabetical order |
|||
sub isOrdered(word: [uint8]): (r: uint8) is |
|||
var cr := [word]; |
|||
word := @next word; |
|||
loop |
|||
var cl := cr; |
|||
cr := [word]; |
|||
word := @next word; |
|||
if cr < 32 then |
|||
r := 1; |
|||
return; |
|||
elseif (cl | 32) > (cr | 32) then |
|||
r := 0; |
|||
return; |
|||
end if; |
|||
end loop; |
|||
end sub; |
|||
# Find maximum length of ordered words |
|||
var maxLen: uint8 := 0; |
|||
sub MaxOrderedLength implements LineCb is |
|||
var len := StrLen(line) as uint8; |
|||
if maxLen < len and isOrdered(line) != 0 then |
|||
maxLen := len; |
|||
end if; |
|||
end sub; |
|||
# Print all ordered words matching maximum length |
|||
sub PrintMaxLenWord implements LineCb is |
|||
if maxLen == StrLen(line) as uint8 and isOrdered(line) != 0 then |
|||
print(line); |
|||
print_nl(); |
|||
end if; |
|||
end sub; |
|||
var fcb: FCB; |
|||
if FCBOpenIn(&fcb, filename) != 0 then |
|||
print("cannot open unixdict.txt\n"); |
|||
ExitWithError(); |
|||
end if; |
|||
ForEachLine(&fcb, MaxOrderedLength); |
|||
ForEachLine(&fcb, PrintMaxLenWord); |
|||
var foo := FCBClose(&fcb);</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
===Simple Procedural Version=== |
===Simple Procedural Version=== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.range, std.string; |
import std.stdio, std.algorithm, std.range, std.string; |
||
Line 979: | Line 1,493: | ||
writefln("%-(%s\n%)", result); |
writefln("%-(%s\n%)", result); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>abbott |
<pre>abbott |
||
Line 1,000: | Line 1,514: | ||
===Faster Procedural Version=== |
===Faster Procedural Version=== |
||
Faster, same output. |
Faster, same output. |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.file, std.range; |
import std.stdio, std.algorithm, std.file, std.range; |
||
Line 1,018: | Line 1,532: | ||
writefln("%-(%s\n%)", result); |
writefln("%-(%s\n%)", result); |
||
}</ |
}</syntaxhighlight> |
||
===Functional Version=== |
===Functional Version=== |
||
Shorter, same output. |
Shorter, same output. |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.range, std.file, std.string; |
import std.stdio, std.algorithm, std.range, std.file, std.string; |
||
Line 1,028: | Line 1,542: | ||
immutable maxLen = words.map!q{a.length}.reduce!max; |
immutable maxLen = words.map!q{a.length}.reduce!max; |
||
writefln("%-(%s\n%)", words.filter!(w => w.length == maxLen)); |
writefln("%-(%s\n%)", words.filter!(w => w.length == maxLen)); |
||
}</ |
}</syntaxhighlight> |
||
===Fastest Memory Mapped Version=== |
===Fastest Memory Mapped Version=== |
||
Lower level, much faster with large input files (about as fast as the memory mapped C version). The output is the same. It works only with ASCII texts, but unlike the C entry this is portable. |
Lower level, much faster with large input files (about as fast as the memory mapped C version). The output is the same. It works only with ASCII texts, but unlike the C entry this is portable. |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, core.stdc.string, std.mmfile, std.algorithm; |
||
const(char)[] findWord(const char[] s) pure nothrow @safe @nogc { |
const(char)[] findWord(const char[] s) pure nothrow @safe @nogc { |
||
Line 1,073: | Line 1,587: | ||
txt[0 .. outStart].write; |
txt[0 .. outStart].write; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program POrderedWords; |
program POrderedWords; |
||
Line 1,131: | Line 1,645: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: dictionary directly processed from the URL |
Output: dictionary directly processed from the URL |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Draco}}== |
|||
<syntaxhighlight lang="draco">\util.g |
|||
proc nonrec is_ordered(*char str) bool: |
|||
while str* /= '\e' and str* <= (str+1)* do |
|||
str := str + 1 |
|||
od; |
|||
str* = '\e' or (str+1)* = '\e' |
|||
corp |
|||
proc nonrec main() void: |
|||
[64]char buf; |
|||
*char str; |
|||
word length, max_length; |
|||
file(1024) dictfile; |
|||
channel input text dict; |
|||
str := &buf[0]; |
|||
max_length := 0; |
|||
open(dict, dictfile, "unixdict.txt"); |
|||
while readln(dict; str) do |
|||
if is_ordered(str) then |
|||
length := CharsLen(str); |
|||
if length > max_length then max_length := length fi |
|||
fi |
|||
od; |
|||
close(dict); |
|||
open(dict, dictfile, "unixdict.txt"); |
|||
while readln(dict; str) do |
|||
if is_ordered(str) then |
|||
length := CharsLen(str); |
|||
if length = max_length then writeln(str) fi |
|||
fi |
|||
od; |
|||
close(dict) |
|||
corp</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="e">pragma.enable("accumulator") |
||
def words := <http://www.puzzlers.org/pub/wordlists/unixdict.txt>.getText().split("\n") |
def words := <http://www.puzzlers.org/pub/wordlists/unixdict.txt>.getText().split("\n") |
||
Line 1,148: | Line 1,717: | ||
def maxLen := accum 0 for word in ordered { _.max(word.size()) } |
def maxLen := accum 0 for word in ordered { _.max(word.size()) } |
||
def maxOrderedWords := accum [] for word ? (word.size() <=> maxLen) in ordered { _.with(word) } |
def maxOrderedWords := accum [] for word ? (word.size() <=> maxLen) in ordered { _.with(word) } |
||
println(" ".rjoin(maxOrderedWords))</ |
println(" ".rjoin(maxOrderedWords))</syntaxhighlight> |
||
One-pass procedural algorithm which avoids keeping the entire data set in memory: |
One-pass procedural algorithm which avoids keeping the entire data set in memory: |
||
< |
<syntaxhighlight lang="e">def best := [].diverge() |
||
for `@word$\n` ? (word.sort() <=> word) in <http://www.puzzlers.org/pub/wordlists/unixdict.txt> { |
for `@word$\n` ? (word.sort() <=> word) in <http://www.puzzlers.org/pub/wordlists/unixdict.txt> { |
||
if (best.size() == 0) { |
if (best.size() == 0) { |
||
Line 1,162: | Line 1,731: | ||
} |
} |
||
} |
} |
||
println(" ".rjoin(best.snapshot()))</ |
println(" ".rjoin(best.snapshot()))</syntaxhighlight> |
||
Output: <code>abbott accent accept access accost almost bellow billow biopsy chilly choosy |
Output: <code>abbott accent accept access accost almost bellow billow biopsy chilly choosy |
||
Line 1,168: | Line 1,737: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define (ordered? str) |
(define (ordered? str) |
||
(for/and ([i (in-range 1 (string-length str))]) |
(for/and ([i (in-range 1 (string-length str))]) |
||
Line 1,195: | Line 1,764: | ||
;; english |
;; english |
||
→ (Adelops alloquy beefily begorry billowy egilops) |
→ (Adelops alloquy beefily begorry billowy egilops) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">File.read!("unixdict.txt") |
||
|> String.split |
|> String.split |
||
|> Enum.filter(fn word -> String.codepoints(word) |> Enum.sort |> Enum.join == word end) |
|> Enum.filter(fn word -> String.codepoints(word) |> Enum.sort |> Enum.join == word end) |
||
Line 1,206: | Line 1,774: | ||
|> elem(1) |
|> elem(1) |
||
|> Enum.sort |
|> Enum.sort |
||
|> Enum.each(fn word -> IO.puts word end)</ |
|> Enum.each(fn word -> IO.puts word end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,229: | Line 1,797: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( ordered_words ). |
-module( ordered_words ). |
||
Line 1,251: | Line 1,819: | ||
words() -> anagrams_deranged:words_from_url( "http://www.puzzlers.org/pub/wordlists/unixdict.txt" ). |
words() -> anagrams_deranged:words_from_url( "http://www.puzzlers.org/pub/wordlists/unixdict.txt" ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,261: | Line 1,829: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">include misc.e |
||
type ordered(sequence s) |
type ordered(sequence s) |
||
Line 1,296: | Line 1,864: | ||
close(fn) |
close(fn) |
||
pretty_print(1,words,{2})</ |
pretty_print(1,words,{2})</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,319: | Line 1,887: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
open System.IO |
open System.IO |
||
Line 1,331: | Line 1,899: | ||
|> Seq.head |> snd |
|> Seq.head |> snd |
||
longestOrderedWords() |> Seq.iter (printfn "%s")</ |
longestOrderedWords() |> Seq.iter (printfn "%s")</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,353: | Line 1,921: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor"> |
||
USING: grouping http.client io io.encodings.utf8 io.files |
USING: grouping http.client io io.encodings.utf8 io.files |
||
io.files.temp kernel math memoize sequences sequences.extras |
io.files.temp kernel math memoize sequences sequences.extras |
||
Line 1,371: | Line 1,939: | ||
word-list [ ordered-word? ] filter |
word-list [ ordered-word? ] filter |
||
all-longest [ print ] each ; |
all-longest [ print ] each ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: <pre>( scratchpad ) USING: ordered-words-main ; |
Output: <pre>( scratchpad ) USING: ordered-words-main ; |
||
Line 1,394: | Line 1,962: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,425: | Line 1,993: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
||
</pre> |
</pre> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
Downloads the list from puzzlers.org. |
Downloads the list from puzzlers.org. |
||
< |
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE |
||
FUNCTION RESTfulGET(url) |
FUNCTION RESTfulGET(url) |
||
Line 1,465: | Line 2,034: | ||
RETURN TRUE |
RETURN TRUE |
||
END FUNCTION |
END FUNCTION |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output |
Output |
||
<pre> abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
<pre> abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
||
Line 1,475: | Line 2,044: | ||
This program uses a string stack, which means that all matching words are stored on a stack. |
This program uses a string stack, which means that all matching words are stored on a stack. |
||
The longest word ends up on the top of the stack. |
The longest word ends up on the top of the stack. |
||
<syntaxhighlight lang="forth"> |
|||
<lang Forth> |
|||
include lib/stmstack.4th \ include string stack library |
include lib/stmstack.4th \ include string stack library |
||
Line 1,519: | Line 2,088: | ||
; \ open file, clear the stack, read file |
; \ open file, clear the stack, read file |
||
\ read it back and close the file |
\ read it back and close the file |
||
ordered</ |
ordered</syntaxhighlight> |
||
Since the longest word is on top of the stack, the only thing to be done is to pop |
Since the longest word is on top of the stack, the only thing to be done is to pop |
||
all words from the stack until a shorter word is encountered. Consequently, all |
all words from the stack until a shorter word is encountered. Consequently, all |
||
Line 1,542: | Line 2,111: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran"> |
||
!*************************************************************************************** |
!*************************************************************************************** |
||
module ordered_module |
module ordered_module |
||
Line 1,656: | Line 2,225: | ||
end program main |
end program main |
||
!**************************************************** |
!**************************************************** |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output''' |
'''Output''' |
||
Line 1,662: | Line 2,231: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function isOrdered(s As Const String) As Boolean |
Function isOrdered(s As Const String) As Boolean |
||
Line 1,703: | Line 2,272: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,726: | Line 2,295: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
=={{header|Frink}}== |
|||
<syntaxhighlight lang="frink">url="https://web.archive.org/web/20180611003215if_/http://www.puzzlers.org:80/pub/wordlists/unixdict.txt" |
|||
a = sort[select[lines[url], {|w| charList[w] == sort[charList[w]] } ], {|a,b| length[a] <=> length[b]}] |
|||
println[sort[select[a, {|w, longest| length[w] == longest}, length[last[a]]]]]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty] |
|||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES} |
|||
local fn Words as CFArrayRef |
|||
CFURLRef url = fn URLWithString( @"https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt" ) |
|||
CFStringRef string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL ) |
|||
end fn = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet ) |
|||
local fn IsOrderedWord( string as CFStringRef ) as BOOL |
|||
BOOL flag = YES |
|||
long i |
|||
unichar chr, prevChr = 0 |
|||
for i = 0 to len(string) - 1 |
|||
chr = fn StringCharacterAtIndex( string, i ) |
|||
if ( chr < prevChr ) |
|||
flag = NO : break |
|||
end if |
|||
prevChr = chr |
|||
next |
|||
end fn = flag |
|||
void local fn DoIt |
|||
CFStringRef string |
|||
CFArrayRef words = fn Words |
|||
long length, maxLen = 0 |
|||
CFMutableStringRef orderedWords = fn MutableStringWithCapacity(0) |
|||
for string in words |
|||
length = len(string) |
|||
if ( length < maxLen ) then continue |
|||
if ( fn IsOrderedWord( string ) ) |
|||
if ( length > maxLen ) |
|||
MutableStringSetString( orderedWords, @"" ) |
|||
maxLen = length |
|||
end if |
|||
MutableStringAppendFormat( orderedWords, @"%@\n", string ) |
|||
end if |
|||
next |
|||
print orderedWords |
|||
end fn |
|||
fn DoIt |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sDict As String = File.Load(User.Home &/ "unixdict.txt") 'Store the 'Dictionary' |
Dim sDict As String = File.Load(User.Home &/ "unixdict.txt") 'Store the 'Dictionary' |
||
Dim sOrdered As New String[] 'To store ordered words |
Dim sOrdered As New String[] 'To store ordered words |
||
Line 1,757: | Line 2,386: | ||
Print sHold.Join(gb.NewLine) 'Display the result |
Print sHold.Join(gb.NewLine) 'Display the result |
||
End </ |
End </syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,781: | Line 2,410: | ||
Go has strings and Unicode and stuff, but with the dictionary all ASCII |
Go has strings and Unicode and stuff, but with the dictionary all ASCII |
||
and lower case, strings and Unicode seem overkill. I just worked with byte slices here, only converting the final result to strings for easy output. |
and lower case, strings and Unicode seem overkill. I just worked with byte slices here, only converting the final result to strings for easy output. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,837: | Line 2,466: | ||
fmt.Println(string(bs)) |
fmt.Println(string(bs)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,860: | Line 2,489: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def isOrdered = { word -> def letters = word as List; letters == ([] + letters).sort() } |
||
assert isOrdered('abbey') |
assert isOrdered('abbey') |
||
assert !isOrdered('cat') |
assert !isOrdered('cat') |
||
Line 1,868: | Line 2,497: | ||
def owMax = orderedWords*.size().max() |
def owMax = orderedWords*.size().max() |
||
orderedWords.findAll { it.size() == owMax }.each { println it }</ |
orderedWords.findAll { it.size() == owMax }.each { println it }</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,887: | Line 2,516: | ||
glossy |
glossy |
||
knotty</pre> |
knotty</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell"> |
||
-- Words are read from the standard input. We keep in memory only the current |
-- Words are read from the standard input. We keep in memory only the current |
||
-- set of longest, ordered words. |
-- set of longest, ordered words. |
||
Line 1,908: | Line 2,538: | ||
let ws = longestOrderedWords $ words str |
let ws = longestOrderedWords $ words str |
||
mapM_ putStrLn ws |
mapM_ putStrLn ws |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
< |
<syntaxhighlight lang="haskell"> |
||
abbott |
abbott |
||
accent |
accent |
||
Line 1,928: | Line 2,558: | ||
glossy |
glossy |
||
knotty |
knotty |
||
</syntaxhighlight> |
|||
</lang> |
|||
Alternative version: |
Alternative version: |
||
< |
<syntaxhighlight lang="haskell">import Control.Monad (liftM) |
||
isSorted wws@(_ : ws) = and $ zipWith (<=) wws ws |
isSorted wws@(_ : ws) = and $ zipWith (<=) wws ws |
||
Line 1,940: | Line 2,570: | ||
let ow = filter isSorted ls |
let ow = filter isSorted ls |
||
let maxl = foldr max 0 (map length ow) |
let maxl = foldr max 0 (map length ow) |
||
print $ filter (\w -> (length w) == maxl) ow</ |
print $ filter (\w -> (length w) == maxl) ow</syntaxhighlight> |
||
=={{header|Huginn}}== |
|||
<syntaxhighlight lang="huginn">import Algorithms as algo; |
|||
import Mathematics as math; |
|||
import Network as net; |
|||
import Text as text; |
|||
main( argv_ ) { |
|||
url = size( argv_ ) > 1 |
|||
? argv_[1] |
|||
: "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"; |
|||
words = algo.materialize( algo.map( net.get( url ).stream, string.strip ), list ); |
|||
ordered = algo.materialize( |
|||
algo.filter( |
|||
words, |
|||
@( word ){ word == ∑( algo.map( algo.sorted( word ), string ) ); } |
|||
), |
|||
list |
|||
); |
|||
maxLen = algo.reduce( ordered, @( x, y ){ math.max( x, size( y ) ); }, 0 ); |
|||
maxOrderedWords = algo.materialize( |
|||
algo.filter( ordered, @[maxLen]( word ){ size( word ) == maxLen; } ), |
|||
list |
|||
); |
|||
print( "{}\n".format( text.join( algo.sorted( maxOrderedWords ), " " ) ) ); |
|||
return ( 0 ); |
|||
}</syntaxhighlight> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="unicon">link strings |
||
procedure main(A) |
procedure main(A) |
||
Line 1,952: | Line 2,609: | ||
} |
} |
||
every write(!\maxList) |
every write(!\maxList) |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,978: | Line 2,635: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">file := File clone openForReading("./unixdict.txt") |
||
words := file readLines |
words := file readLines |
||
file close |
file close |
||
Line 1,994: | Line 2,651: | ||
) |
) |
||
orderedWords join(" ") println</ |
orderedWords join(" ") println</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
||
Line 2,000: | Line 2,657: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j">NB. from http://wiki.puzzlers.org/pub/wordlists/unixdict.txt |
|||
<lang j> require'web/gethttp' |
|||
oWords=: (#~ ] = /:~L:0) cutLF fread 'unixdict.txt' |
|||
;:inv (#~ (= >./)@:(#@>)) oWords |
|||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</syntaxhighlight> |
|||
;:inv (#~ (= >./)@:(#@>))oWords |
|||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</lang> |
|||
Recap: |
Recap: |
||
# |
# read dictionary (<code>fread 'unixdict.txt'</code>) |
||
# break into words, one per line (<code> |
# break into words, one per line (<code>cutLF</code>) |
||
# find ordered words (<code> |
# find ordered words (<code>(#~ ] = /:~L:0)</code>) |
||
# select the longest ordered words (<code>(#~ (= >./)@:(#@>))oWords</code>) |
# select the longest ordered words (<code>(#~ (= >./)@:(#@>))oWords</code>) |
||
# format for display (using ;:inv) |
# format for display (using ;:inv) |
||
Line 2,017: | Line 2,673: | ||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
This example assumes there is a local copy of the dictionary whose path is given as the first argument to the program. |
This example assumes there is a local copy of the dictionary whose path is given as the first argument to the program. |
||
< |
<syntaxhighlight lang="java5">import java.io.BufferedReader; |
||
import java.io.FileReader; |
import java.io.FileReader; |
||
import java.io.IOException; |
import java.io.IOException; |
||
Line 2,059: | Line 2,715: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>abbott |
<pre>abbott |
||
Line 2,077: | Line 2,733: | ||
glossy |
glossy |
||
knotty</pre> |
knotty</pre> |
||
===Using Java 16=== |
|||
<syntaxhighlight lang="java"> |
|||
import java.io.IOException; |
|||
import java.nio.file.Files; |
|||
import java.nio.file.Path; |
|||
import java.util.List; |
|||
public final class OrderedWords { |
|||
public static void main(String[] aArgs) throws IOException { |
|||
List<String> ordered = Files.lines(Path.of("unixdict.txt")) |
|||
.filter( word -> isOrdered(word) ).toList(); |
|||
final int maxLength = ordered.stream().map( word -> word.length() ).max(Integer::compare).get(); |
|||
ordered.stream().filter( word -> word.length() == maxLength ).forEach(System.out::println); |
|||
} |
|||
private static boolean isOrdered(String aWord) { |
|||
return aWord.chars() |
|||
.mapToObj( i -> (char) i ) |
|||
.sorted() |
|||
.map(String::valueOf) |
|||
.reduce("", String::concat) |
|||
.equals(aWord); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
<pre> |
|||
The same as the Java example above. |
|||
</pre> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Using [http://nodejs.org/ node.js]: |
Using [http://nodejs.org/ node.js]: |
||
< |
<syntaxhighlight lang="javascript">var fs = require('fs'), print = require('sys').print; |
||
fs.readFile('./unixdict.txt', 'ascii', function (err, data) { |
fs.readFile('./unixdict.txt', 'ascii', function (err, data) { |
||
var is_ordered = function(word){return word.split('').sort().join('') === word;}, |
var is_ordered = function(word){return word.split('').sort().join('') === word;}, |
||
Line 2,091: | Line 2,780: | ||
}; |
}; |
||
print(longest.sort().join(', ') + '\n'); |
print(longest.sort().join(', ') + '\n'); |
||
});</ |
});</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,098: | Line 2,787: | ||
Alternative version (also using Node.js): |
Alternative version (also using Node.js): |
||
< |
<syntaxhighlight lang="javascript">var http = require('http'); |
||
http.get({ |
http.get({ |
||
Line 2,123: | Line 2,812: | ||
console.log(ordered.join(', ')); |
console.log(ordered.join(', ')); |
||
}); |
}); |
||
});</ |
});</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
< |
<syntaxhighlight lang="jq">def is_sorted: |
||
if length <= 1 then true |
if length <= 1 then true |
||
else .[0] <= .[1] and (.[1:] | is_sorted) |
else .[0] <= .[1] and (.[1:] | is_sorted) |
||
Line 2,139: | Line 2,829: | ||
split("\n") | longest_ordered_words</ |
split("\n") | longest_ordered_words</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
["abbott","accent","accept","access","accost","almost","bellow","billow","biopsy","chilly","choosy","choppy","effort","floppy","glossy","knotty"] |
["abbott","accent","accept","access","accost","almost","bellow","billow","biopsy","chilly","choosy","choppy","effort","floppy","glossy","knotty"] |
||
Line 2,147: | Line 2,837: | ||
'''Built-in function''': |
'''Built-in function''': |
||
< |
<syntaxhighlight lang="julia">issorted("abc") # true</syntaxhighlight> |
||
'''Main''': |
'''Main''': |
||
< |
<syntaxhighlight lang="julia">lst = readlines("data/unixdict.txt") |
||
filter!(issorted, lst) |
filter!(issorted, lst) |
||
filter!(x -> length(x) == maximum(length, lst), lst) |
filter!(x -> length(x) == maximum(length, lst), lst) |
||
println.(lst)</ |
println.(lst)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,174: | Line 2,864: | ||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k"> w@&d=|/d:#:'w:d@&&/'{~x<y}':'d:0:"unixdict.txt" |
||
("abbott" |
("abbott" |
||
"accent" |
"accent" |
||
Line 2,190: | Line 2,880: | ||
"floppy" |
"floppy" |
||
"glossy" |
"glossy" |
||
"knotty")</ |
"knotty")</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="kotlin">import java.net.URI |
||
fun main( |
fun main() { |
||
val |
val url = URI( |
||
"https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt" |
|||
val result = mutableListOf<String>() |
|||
).toURL() |
|||
val words = url.openStream().bufferedReader().use { |
|||
file.forEachLine { |
|||
var max = 0 |
|||
if (it.toCharArray().sorted().joinToString(separator = "") == it) { |
|||
it.lineSequence() |
|||
.filter { it.asSequence().windowed(2).all { it[0] <= it[1] } } |
|||
} |
|||
.sortedByDescending(String::length) |
|||
.takeWhile { word -> word.length >= max.also { max = word.length } } |
|||
.toList() |
|||
} |
} |
||
words.forEach(::println) |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Lambdatalk}}== |
|||
result.sortByDescending { it.length } |
|||
<syntaxhighlight lang="scheme"> |
|||
val max = result[0].length |
|||
{def maxOrderedWords |
|||
{def isOrdered |
|||
for (word in result) { |
|||
{lambda {:w} |
|||
if (word.length == max) { |
|||
{W.equal? :w {W.sort before :w}}}} |
|||
println(word) |
|||
} |
|||
{def getOrdered |
|||
} |
|||
{lambda {:w} |
|||
}</lang> |
|||
{if {isOrdered :w} then :w else}}} |
|||
{def pushOrdered |
|||
{lambda {:m :w} |
|||
{if {= {W.length :w} :m} then {br}:w else}}} |
|||
{def maxOrderedWords.i |
|||
{lambda {:sortedWords} |
|||
{let { {:orderedWords {S.map getOrdered :sortedWords}} } |
|||
{S.map {{lambda {:m :w} {pushOrdered :m :w}} |
|||
{max {S.map {lambda {:w} {W.length :w}} |
|||
:orderedWords}}} |
|||
:orderedWords}}}} |
|||
{lambda {:s} |
|||
{maxOrderedWords.i {S.replace else by el_se in :s}}}} |
|||
-> maxOrderedWords |
|||
{maxOrderedWords UNIX.DICT} |
|||
-> |
|||
abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty |
|||
</syntaxhighlight> |
|||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
< |
<syntaxhighlight lang="lang5">: >string-index |
||
"" split |
"" split |
||
"&'0123456789abcdefghijklmnopqrstuvwxyz" "" split |
"&'0123456789abcdefghijklmnopqrstuvwxyz" "" split |
||
Line 2,253: | Line 2,987: | ||
fh fin filtering fh close ; |
fh fin filtering fh close ; |
||
ordered-words</ |
ordered-words</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[ abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty ]</pre> |
<pre>[ abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty ]</pre> |
||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(f = file('unixdict.txt'), words = array, ordered = array, maxleng = 0) |
||
#f->dowithclose => { |
#f->dowithclose => { |
||
#f->foreachLine => { |
#f->foreachLine => { |
||
Line 2,276: | Line 3,009: | ||
with w in #ordered |
with w in #ordered |
||
where #w->size == #maxleng |
where #w->size == #maxleng |
||
do => {^ #w + '\r' ^}</ |
do => {^ #w + '\r' ^}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
'Ordered wordsFrom Rosetta Code |
'Ordered wordsFrom Rosetta Code |
||
open "unixdict.txt" for input as #1 |
open "unixdict.txt" for input as #1 |
||
Line 2,315: | Line 3,048: | ||
close #1 |
close #1 |
||
print wordList$ |
print wordList$ |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 2,337: | Line 3,070: | ||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Code ported from Lua solution. |
Code ported from Lua solution. |
||
< |
<syntaxhighlight lang="lingo">-- Contents of unixdict.txt passed as string |
||
on printLongestOrderedWords (words) |
on printLongestOrderedWords (words) |
||
res = [] |
res = [] |
||
Line 2,363: | Line 3,096: | ||
end repeat |
end repeat |
||
put res |
put res |
||
end</ |
end</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>-- ["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]</pre> |
<pre>-- ["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">fp = io.open( "dictionary.txt" ) |
||
maxlen = 0 |
maxlen = 0 |
||
Line 2,396: | Line 3,129: | ||
end |
end |
||
fp:close()</ |
fp:close()</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>abbott |
<pre>abbott |
||
Line 2,416: | Line 3,149: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">lst := StringTools:-Split(Import("http://www.puzzlers.org/pub/wordlists/unixdict.txt"), "\n"): |
||
longest := 0: |
longest := 0: |
||
words := Array(): |
words := Array(): |
||
Line 2,434: | Line 3,167: | ||
end if; |
end if; |
||
end do; |
end do; |
||
for word in words do print(word); end do;</ |
for word in words do print(word); end do;</syntaxhighlight> |
||
{{Out|Output}} |
{{Out|Output}} |
||
<pre>"abbott" |
<pre>"abbott" |
||
Line 2,453: | Line 3,186: | ||
"knotty"</pre> |
"knotty"</pre> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">Module[{max, |
|||
=={{header|Mathematica}}== |
|||
<lang mathematica>Module[{max, |
|||
data = Select[Import["http://www.puzzlers.org/pub/wordlists/unixdict.txt", "List"], |
data = Select[Import["http://www.puzzlers.org/pub/wordlists/unixdict.txt", "List"], |
||
OrderedQ[Characters[#]] &]}, |
OrderedQ[Characters[#]] &]}, |
||
max = Max[StringLength /@ data]; |
max = Max[StringLength /@ data]; |
||
Select[data, StringLength[#] == max &]] |
Select[data, StringLength[#] == max &]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
However Mathematica has built in dictionaries for many languages, so here is a more general version... |
However Mathematica has built in dictionaries for many languages, so here is a more general version... |
||
< |
<syntaxhighlight lang="mathematica">maxWords[language_String] := Module[{max,data = Select[DictionaryLookup[{language, "*"}],OrderedQ[Characters[#]] &]}, |
||
max = Max[StringLength /@ data]; |
max = Max[StringLength /@ data]; |
||
Select[data, StringLength[#] == max &]] |
Select[data, StringLength[#] == max &]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,482: | Line 3,214: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">maxlen = 0; |
||
listlen= 0; |
listlen= 0; |
||
fid = fopen('unixdict.txt','r'); |
fid = fopen('unixdict.txt','r'); |
||
Line 2,497: | Line 3,229: | ||
end |
end |
||
fclose(fid); |
fclose(fid); |
||
printf('%s\n',list{:}); </ |
printf('%s\n',list{:}); </syntaxhighlight> |
||
Returns: |
Returns: |
||
Line 2,516: | Line 3,248: | ||
glossy |
glossy |
||
knotty</pre> |
knotty</pre> |
||
=={{header|Nanoquery}}== |
|||
<syntaxhighlight lang="nanoquery">import Nanoquery.IO |
|||
def is_ordered(word) |
|||
word = str(word) |
|||
if len(word) < 2 |
|||
return true |
|||
end |
|||
for i in range(1, len(word) - 1) |
|||
if str(word[i]) < str(word[i - 1]) |
|||
return false |
|||
end |
|||
end |
|||
return true |
|||
end |
|||
longest = 0 |
|||
words = {} |
|||
for word in new(File, "unixdict.txt").read() |
|||
if is_ordered(word) |
|||
if len(word) > longest |
|||
longest = len(word) |
|||
words.clear() |
|||
words.append(word) |
|||
else if len(word) = longest |
|||
words.append(word) |
|||
end |
|||
end |
|||
end |
|||
println words</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty]</pre> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols binary |
options replace format comments java crossref savelog symbols binary |
||
Line 2,557: | Line 3,325: | ||
Arrays.sort(wchars) |
Arrays.sort(wchars) |
||
return dword.equalsIgnoreCase(String(wchars)) |
return dword.equalsIgnoreCase(String(wchars)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
<pre> |
<pre> |
||
Line 2,564: | Line 3,332: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<lang |
<syntaxhighlight lang="nim">import strutils |
||
const DictFile = "unixdict.txt" |
|||
func isSorted(s: string): bool = |
|||
var last = |
var last = char.low |
||
for c in s: |
for c in s: |
||
if c < last: |
if c < last: return false |
||
return false |
|||
last = c |
last = c |
||
result = true |
|||
var |
|||
const url = "http://www.puzzlers.org/pub/wordlists/unixdict.txt" |
|||
mx = 0 |
|||
words: seq[string] |
|||
for word in |
for word in DictFile.lines: |
||
if word.len >= mx and |
if word.len >= mx and word.isSorted: |
||
if word.len > mx: |
if word.len > mx: |
||
words |
words.setLen(0) |
||
mx = word.len |
mx = word.len |
||
words.add |
words.add word |
||
echo words.join(" ")</ |
echo words.join(" ")</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let input_line_opt ic = |
||
try Some(input_line ic) |
try Some(input_line ic) |
||
with End_of_file -> None |
with End_of_file -> None |
||
Line 2,641: | Line 3,410: | ||
let ordered_words = List.filter is_ordered lower_words in |
let ordered_words = List.filter is_ordered lower_words in |
||
let longest_ordered_words = longest_words ordered_words in |
let longest_ordered_words = longest_words ordered_words in |
||
List.iter print_endline longest_ordered_words</ |
List.iter print_endline longest_ordered_words</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,665: | Line 3,434: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: longWords |
||
| w longest l s | |
| w longest l s | |
||
0 ->longest |
0 ->longest |
||
Line 2,673: | Line 3,442: | ||
s longest > ifTrue: [ s ->longest ListBuffer new ->l ] |
s longest > ifTrue: [ s ->longest ListBuffer new ->l ] |
||
l add(w) |
l add(w) |
||
] l ; </ |
] l ; </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,684: | Line 3,453: | ||
{{trans|REXX}} |
{{trans|REXX}} |
||
Adapted for ooRexx |
Adapted for ooRexx |
||
< |
<syntaxhighlight lang="oorexx">/*REXX list (the longest) ordered word(s) from a supplied dictionary. */ |
||
iFID= 'UNIXDICT.TXT' |
iFID= 'UNIXDICT.TXT' |
||
w.='' |
w.='' |
||
Line 2,710: | Line 3,479: | ||
End |
End |
||
Exit |
Exit |
||
s: Return left('s',arg(1)>1)</ |
s: Return left('s',arg(1)>1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Same as REXX' |
Same as REXX' |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">ordered(s)=my(v=Vecsmall(s),t=97);for(i=1,#v,if(v[i]>64&&v[i]<91,v[i]+=32);if(v[i]<97||v[i]>122||v[i]<t,return(0),t=v[i]));1 |
||
v=select(ordered,readstr("~/unixdict.txt")); |
v=select(ordered,readstr("~/unixdict.txt")); |
||
N=vecmax(apply(length,v)); |
N=vecmax(apply(length,v)); |
||
select(s->#s==N, v)</ |
select(s->#s==N, v)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = ["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]</pre> |
<pre>%1 = ["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
use warnings; |
use warnings; |
||
Line 2,735: | Line 3,504: | ||
close FH; |
close FH; |
||
print "@{$words[-1]}\n"; |
print "@{$words[-1]}\n"; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,741: | Line 3,510: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unix_dict</span><span style="color: #0000FF;">()</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">maxlen</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">maxlen</span> <span style="color: #008080;">and</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">></span><span style="color: #000000;">maxlen</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">found</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxlen</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">found</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">word</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %d longest ordered words:\n %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">found</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">found</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n "</span><span style="color: #0000FF;">)})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
|||
<pre> |
|||
The 16 longest ordered words: |
|||
abbott accent accept access |
|||
accost almost bellow billow |
|||
biopsy chilly choosy choppy |
|||
effort floppy glossy knotty |
|||
</pre> |
|||
=={{header|Phixmonti}}== |
|||
Here we assume the dictionary is provided on standard input. |
|||
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
|||
0 var maxlen |
|||
{{works with|Rakudo|2016.07}} |
|||
( ) var words |
|||
<lang perl6>say lines.grep({ [le] .comb }).classify(*.chars).max(*.key).value</lang> |
|||
0 var f |
|||
def getword f fgets dup -1 == if drop false else -1 del endif enddef |
|||
def ordered? dup dup sort == enddef |
|||
def greater? len maxlen > enddef |
|||
"unixdict.txt" "r" fopen var f |
|||
f -1 != |
|||
while |
|||
getword dup if |
|||
ordered? if |
|||
greater? if |
|||
len var maxlen |
|||
( ) var words |
|||
endif |
|||
len maxlen == if |
|||
words over 0 put var words |
|||
endif |
|||
endif |
|||
endif |
|||
endwhile |
|||
f fclose |
|||
words print</syntaxhighlight> |
|||
Another solution |
|||
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
|||
0 var f |
|||
def getword f fgets dup -1 == if drop false else -1 del endif enddef |
|||
def ordered? dup dup sort == enddef |
|||
def greater? len rot 1 get len nip rot swap over over > enddef |
|||
def append over 0 put enddef |
|||
( " " ) |
|||
"unixdict.txt" "r" fopen var f |
|||
f -1 != |
|||
while |
|||
getword dup if |
|||
ordered? if |
|||
greater? if |
|||
drop drop flush append |
|||
else |
|||
== if append endif |
|||
endif |
|||
swap |
|||
endif |
|||
endif |
|||
endwhile |
|||
f fclose |
|||
print</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty] |
["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"] |
||
</pre> |
</pre> |
||
=={{header| |
=={{header|Picat}}== |
||
<syntaxhighlight lang="picat">go => |
|||
Copy of [[Ordered_words#Euphoria|Euphoria]] |
|||
Dict = "unixdict.txt", |
|||
<lang Phix>type ordered(sequence s) |
|||
Words = new_map([Word=Word.length : Word in read_file_lines(Dict), Word == Word.sort()]), |
|||
for i=1 to length(s)-1 do |
|||
MaxLen = max([Len : _Word=Len in Words]), |
|||
-- assume all items in the sequence are atoms |
|||
println([Word : Word=Len in Words, Len=MaxLen].sort).</syntaxhighlight> |
|||
if s[i]>s[i+1] then |
|||
return 0 |
|||
end if |
|||
end for |
|||
return 1 |
|||
end type |
|||
integer maxlen |
|||
sequence words |
|||
object word |
|||
constant fn = open("demo\\unixdict.txt","r") |
|||
maxlen = -1 |
|||
while 1 do |
|||
word = gets(fn) |
|||
if atom(word) then |
|||
exit |
|||
end if |
|||
word = trim(word) |
|||
if length(word)>=maxlen and ordered(lower(word)) then |
|||
if length(word)>maxlen then |
|||
maxlen = length(word) |
|||
words = {} |
|||
end if |
|||
words = append(words,word) |
|||
end if |
|||
end while |
|||
close(fn) |
|||
?words</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[abbott,accent,accept,access,accost,almost,bellow,billow,biopsy,chilly,choosy,choppy,effort,floppy,glossy,knotty]</pre> |
|||
<pre> |
|||
{"abbott","accent","accept","access","accost","almost","bellow","billow","biopsy","chilly","choosy","choppy","effort","floppy","glossy","knotty"} |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(in "unixdict.txt" |
||
(mapc prinl |
(mapc prinl |
||
(maxi '((L) (length (car L))) |
(maxi '((L) (length (car L))) |
||
(by length group |
(by length group |
||
(filter '((S) (apply <= S)) |
(filter '((S) (apply <= S)) |
||
(make (while (line) (link @))) ) ) ) ) )</ |
(make (while (line) (link @))) ) ) ) ) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>abbott |
<pre>abbott |
||
Line 2,820: | Line 3,632: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli"> |
||
order: procedure options (main); /* 24/11/2011 */ |
order: procedure options (main); /* 24/11/2011 */ |
||
declare word character (20) varying; |
declare word character (20) varying; |
||
Line 2,876: | Line 3,688: | ||
end in_order; |
end in_order; |
||
end order; |
end order; |
||
</syntaxhighlight> |
|||
</lang> |
|||
OUTPUT: |
OUTPUT: |
||
<pre> |
<pre> |
||
Line 2,899: | Line 3,711: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt' |
$url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt' |
||
Line 2,916: | Line 3,728: | ||
($ordered.Words -join ", ") |
($ordered.Words -join ", ") |
||
Remove-Item -Path "$env:TEMP\unixdict.txt" -Force -ErrorAction SilentlyContinue |
Remove-Item -Path "$env:TEMP\unixdict.txt" -Force -ErrorAction SilentlyContinue |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,928: | Line 3,740: | ||
Works with SWI-Prolog |
Works with SWI-Prolog |
||
< |
<syntaxhighlight lang="prolog">:- use_module(library( http/http_open )). |
||
ordered_words :- |
ordered_words :- |
||
Line 2,972: | Line 3,784: | ||
my_compare(R, K1-_V1, K2-_V2) :- |
my_compare(R, K1-_V1, K2-_V2) :- |
||
( K1 < K2 -> R = >; K1 > K2 -> R = <; =). |
( K1 < K2 -> R = >; K1 > K2 -> R = <; =). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
Output : |
||
<pre> ?- ordered_words. |
<pre> ?- ordered_words. |
||
Line 2,995: | Line 3,807: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.s sortLetters(*word.Character, wordLength) ;returns a string with the letters of a word sorted |
||
Protected Dim letters.c(wordLength) |
Protected Dim letters.c(wordLength) |
||
Protected *letAdr = @letters() |
Protected *letAdr = @letters() |
||
Line 3,040: | Line 3,852: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly |
||
Line 3,047: | Line 3,859: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: First Solution=== |
===Python: First Solution=== |
||
< |
<syntaxhighlight lang="python">import urllib.request |
||
url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt' |
url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt' |
||
Line 3,054: | Line 3,866: | ||
maxlen = len(max(ordered, key=len)) |
maxlen = len(max(ordered, key=len)) |
||
maxorderedwords = [word for word in ordered if len(word) == maxlen] |
maxorderedwords = [word for word in ordered if len(word) == maxlen] |
||
print(' '.join(maxorderedwords))</ |
print(' '.join(maxorderedwords))</syntaxhighlight> |
||
===Python: Alternate Solution using one explicit loop=== |
===Python: Alternate Solution using one explicit loop=== |
||
< |
<syntaxhighlight lang="python">import urllib.request |
||
mx, url = 0, 'http://www.puzzlers.org/pub/wordlists/unixdict.txt' |
mx, url = 0, 'http://www.puzzlers.org/pub/wordlists/unixdict.txt' |
||
Line 3,067: | Line 3,879: | ||
words, mx = [], lenword |
words, mx = [], lenword |
||
words.append(word) |
words.append(word) |
||
print(' '.join(words))</ |
print(' '.join(words))</syntaxhighlight> |
||
'''Sample Output''' |
'''Sample Output''' |
||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
||
===Python: As a fold=== |
|||
{{Works with|Python|3.7}} |
|||
<syntaxhighlight lang="python">'''The longest ordered words in a list''' |
|||
from functools import reduce |
|||
from operator import le |
|||
import urllib.request |
|||
# longestOrds :: [String] -> [String] |
|||
def longestOrds(ws): |
|||
'''The longest ordered words in a given list. |
|||
''' |
|||
return reduce(triage, ws, (0, []))[1] |
|||
# triage :: (Int, [String]) -> String -> (Int, [String]) |
|||
def triage(nxs, w): |
|||
'''The maximum length seen for an ordered word, |
|||
and the ordered words of this length seen so far. |
|||
''' |
|||
n, xs = nxs |
|||
lng = len(w) |
|||
return ( |
|||
(lng, ([w] if n != lng else xs + [w])) if ( |
|||
ordered(w) |
|||
) else nxs |
|||
) if lng >= n else nxs |
|||
# ordered :: String -> Bool |
|||
def ordered(w): |
|||
'''True if the word w is ordered.''' |
|||
return all(map(le, w, w[1:])) |
|||
# ------------------------- TEST ------------------------- |
|||
if __name__ == '__main__': |
|||
print( |
|||
'\n'.join(longestOrds( |
|||
urllib.request.urlopen( |
|||
'http://wiki.puzzlers.org/pub/wordlists/unixdict.txt' |
|||
).read().decode("utf-8").split() |
|||
)) |
|||
)</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="quackery"> [ - -1 1 clamp 1+ ]'[ swap peek do ] is <=> ( n n --> ) |
|||
[ true swap |
|||
behead swap witheach |
|||
[ tuck > if |
|||
[ dip not conclude ] ] |
|||
drop ] is ordered ( [ --> b ) |
|||
[ stack ] is largest ( [ --> s ) |
|||
[ 1 largest put |
|||
[] swap witheach |
|||
[ dup size |
|||
largest share <=> |
|||
[ drop |
|||
[ dup ordered iff |
|||
[ nested join ] |
|||
else drop ] |
|||
[ dup ordered iff |
|||
[ dup size largest replace |
|||
nip nested ] |
|||
else drop ] ] ] |
|||
largest release ] is task ( [ --> [ ) |
|||
$ 'unixdict.txt' sharefile drop nest$ |
|||
task |
|||
witheach [ echo$ sp ]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty </pre> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="r">words = scan("https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt",what = "character") |
|||
ordered = logical() |
|||
for(i in 1:length(words)){ |
|||
first = strsplit(words[i],"")[[1]][1:nchar(words[i])-1] |
|||
second = strsplit(words[i],"")[[1]][2:nchar(words[i])] |
|||
ordered[i] = all(first<=second) |
|||
} |
|||
cat(words[ordered][which(nchar(words[ordered])==max(nchar(words[ordered])))],sep="\n")</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
(require net/url) |
(require net/url) |
||
Line 3,095: | Line 4,034: | ||
(loop len longs words) |
(loop len longs words) |
||
(loop wlen (cons word (if (> wlen len) '() longs)) words))))) |
(loop wlen (cons word (if (> wlen len) '() longs)) words))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 3,116: | Line 4,055: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Here we assume the dictionary is provided on standard input. |
|||
{{works with|Rakudo|2016.07}} |
|||
<syntaxhighlight lang="raku" line>say lines.grep({ [le] .comb }).classify(*.chars).max(*.key).value</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty] |
|||
</pre> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">Red [] |
||
;; code to read url and save to local file: |
;; code to read url and save to local file: |
||
;;data: read/binary http://www.puzzlers.org/pub/wordlists/unixdict.txt |
;;data: read/binary http://www.puzzlers.org/pub/wordlists/unixdict.txt |
||
Line 3,131: | Line 4,084: | ||
] |
] |
||
] |
] |
||
probe max</ |
probe max</syntaxhighlight> |
||
'''Sample Output''' |
'''Sample Output''' |
||
<pre>["abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" "biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty"] |
<pre>["abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" "biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty"] |
||
</pre> |
</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This problem assumes (or implies) an order of letter case, but fortunately, there're no uppercase letters |
This problem assumes (or implies) an order of letter case, but fortunately, there're no uppercase letters |
||
Line 3,145: | Line 4,099: | ||
<br>but since the word list (in this case) is already in alphabetical order, this would-be improvement is |
<br>but since the word list (in this case) is already in alphabetical order, this would-be improvement is |
||
<br>mostly moot. |
<br>mostly moot. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program lists (the longest) ordered word(s) from a supplied dictionary. */ |
||
iFID= 'UNIXDICT.TXT' /*the filename of the word dictionary. */ |
iFID= 'UNIXDICT.TXT' /*the filename of the word dictionary. */ |
||
m= 1 /*maximum length of an ordered word(s).*/ |
|||
mL=0 /*maximum length of the ordered words. */ |
|||
call linein iFID, 1, 0 /*point to the first word in dictionary*/ |
call linein iFID, 1, 0 /*point to the first word in dictionary*/ |
||
@.= /*placeholder array for list of words. */ |
|||
do j=1 while lines(iFID)\==0; |
do j=1 while lines(iFID)\==0; x=linein(iFID) /*keep reading until file is exhausted.*/ |
||
w=length(x); if w< |
w= length(x); if w<m then iterate /*Word not long enough? Then ignore it.*/ |
||
if \datatype(x, 'M') then iterate /*Is it not a letter? Then ignore it. */ |
|||
parse upper var x xU 1 z 2 /*get uppercase version of X & 1st char*/ |
|||
do k=2 |
do k=2 for w-1; _= substr(xU, k, 1) /*process each letter in uppercase word*/ |
||
if |
if _<z then iterate j /*is letter < than the previous letter?*/ |
||
z= _ /*we have a newer current letter. */ |
|||
z=_ /*we have a newer current letter. */ |
|||
end /*k*/ /* [↑] logic includes ≥ order. */ |
end /*k*/ /* [↑] logic includes ≥ order. */ |
||
m= w /*maybe define a new maximum length. */ |
|||
@.w=@.w x |
@.w= @.w x /*add the original word to a word list.*/ |
||
end /*j*/ |
end /*j*/ /*the 1st DO needs an index for ITERATE*/ |
||
#=words(@. |
#= words(@.m) /*just a handy─dandy variable to have. */ |
||
say # 'word's(#) "found (of length" |
say # 'word's(#) "found (of length" m')'; say /*show the number of words and length. */ |
||
do n=1 for #; say word(@. |
do n=1 for #; say word(@.m, n); end /*display all the words, one to a line.*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
ghijk |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer (merely adds "S")*/</ |
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer (merely adds "S")*/</syntaxhighlight> |
||
{{out|output|text= when using the default supplied word dictionary:}} |
|||
<pre> |
<pre> |
||
16 words found (of length 6) |
16 words found (of length 6) |
||
Line 3,192: | Line 4,145: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
|||
fn = "C:\Ring\unixdict.txt" |
|||
fp = fopen(fn,"r") |
|||
str = fread(fp, getFileSize(fp)) |
|||
str = substr(str, windowsnl(), nl) |
|||
clist = str2list(str) |
|||
fclose(fp) |
|||
dlist = [] |
|||
cStr = read("unixdict.txt") |
|||
for m = 1 to len(clist) |
|||
wordList = str2list(cStr) |
|||
flag = 1 |
|||
sum = 0 |
|||
for n = 1 to len(clist[m])-1 |
|||
sortList = [] |
|||
if ascii(clist[m][n+1]) < ascii(clist[m][n]) |
|||
flag=0 exit ok |
|||
next |
|||
if flag = 1 |
|||
add(dlist, clist[m]) ok |
|||
next |
|||
see "working..." + nl + nl |
|||
nr = 0 |
|||
for m = 1 to len(dlist) |
|||
for n = 1 to len(wordList) |
|||
num = 0 |
|||
len = len(wordList[n])-1 |
|||
for m = 1 to len |
|||
asc1 = ascii(wordList[n][m]) |
|||
asc2 = ascii(wordList[n][m+1]) |
|||
if asc1 <= asc2 |
|||
num = num + 1 |
|||
ok |
|||
next |
|||
if num = len |
|||
sum = sum + 1 |
|||
add(sortList,[wordList[n],len]) |
|||
ok |
|||
next |
next |
||
sortList = sort(sortList,2) |
|||
for n = 1 to len(dlist) |
|||
sortList = reverse(sortList) |
|||
if len(dlist[n]) = nr |
|||
endList = [] |
|||
see dlist[n] + nl ok |
|||
len = sortList[1][2] |
|||
for n = 1 to len(sortList) |
|||
if sortList[n][2] = len |
|||
add(endList,sortList[n][1]) |
|||
else |
|||
exit |
|||
ok |
|||
next |
next |
||
endList = sort(endList) |
|||
func getFileSize fp |
|||
see endList |
|||
c_filestart = 0 |
|||
c_fileend = 2 |
|||
see nl + "done..." + nl |
|||
fseek(fp,0,c_fileend) |
|||
</syntaxhighlight> |
|||
nfilesize = ftell(fp) |
|||
fseek(fp,0,c_filestart) |
|||
return nfilesize |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
working... |
|||
abbott |
abbott |
||
accent |
accent |
||
Line 3,249: | Line 4,211: | ||
glossy |
glossy |
||
knotty |
knotty |
||
done... |
|||
</pre> |
|||
=={{header|RPL}}== |
|||
The only way to use <code>unixdict.txt</code> as input is to download it locally and to convert it into a list of 25104 strings. Fortunately, emulators can handle such a big data structure in RAM. |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
≪ "a" DUP |
|||
1 4 PICK SIZE '''FOR''' j |
|||
SWAP DROP |
|||
OVER j DUP SUB |
|||
'''IF''' DUP2 > '''THEN''' 99 'j' STO '''END''' |
|||
'''NEXT''' |
|||
≤ SWAP DROP |
|||
≫ '<span style="color:blue">ORDWORD?</span>' STO |
|||
≪ 0 → words sizemax |
|||
≪ { } |
|||
1 words EVAL SIZE '''FOR''' j |
|||
words j GET DUP SIZE |
|||
'''IF''' DUP sizemax ≥ '''THEN''' |
|||
'''IF''' OVER <span style="color:blue">ORDWORD?</span> '''THEN''' <span style="color:grey">@ 2-step test, rather than one with an AND, to save execution time </span> |
|||
'''IF''' DUP sizemax > '''THEN''' |
|||
'sizemax' STO |
|||
SWAP DROP { } SWAP |
|||
'''ELSE''' DROP '''END''' |
|||
+ |
|||
'''ELSE''' DROP2 '''END''' |
|||
'''ELSE''' DROP2 '''END''' |
|||
'''NEXT''' |
|||
≫ ≫ '<span style="color:blue">ORDWORDS</span>' STO |
|||
'Unixdict' <span style="color:blue">ORDWORDS</span> |
|||
{{out}} |
|||
<pre> |
|||
1: { abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty } |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'open-uri' |
||
ordered_words = open('http://www.puzzlers.org/pub/wordlists/unixdict.txt', 'r').select do |word| |
ordered_words = open('http://www.puzzlers.org/pub/wordlists/unixdict.txt', 'r').select do |word| |
||
word.strip! |
word.strip! |
||
Line 3,259: | Line 4,257: | ||
grouped = ordered_words.group_by &:size |
grouped = ordered_words.group_by &:size |
||
puts grouped[grouped.keys.max]</ |
puts grouped[grouped.keys.max]</syntaxhighlight> |
||
'''Sample Output''' |
'''Sample Output''' |
||
Line 3,280: | Line 4,278: | ||
knotty</pre> |
knotty</pre> |
||
Local version: |
Local version: |
||
< |
<syntaxhighlight lang="ruby">words = IO.foreach('unixdict.txt').map(&:chomp).select {|word| word.chars.sort.join == word} |
||
puts words.group_by(&:size).sort_by(&:first).last.last</ |
puts words.group_by(&:size).sort_by(&:first).last.last</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">a$ = httpget$("http://www.puzzlers.org/pub/wordlists/unixdict.txt") |
||
j = 1 |
j = 1 |
||
i = instr(a$,chr$(10),j) |
i = instr(a$,chr$(10),j) |
||
Line 3,303: | Line 4,301: | ||
if len(a3$) = maxL then print a3$ |
if len(a3$) = maxL then print a3$ |
||
n = n + 1 |
n = n + 1 |
||
wend</ |
wend</syntaxhighlight> |
||
<pre>abbott |
<pre>abbott |
||
accent |
accent |
||
Line 3,322: | Line 4,320: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">const FILE: &'static str = include_str!("./unixdict.txt"); |
||
fn is_ordered(s: &str) -> bool { |
fn is_ordered(s: &str) -> bool { |
||
Line 3,364: | Line 4,362: | ||
println!("{}", s.to_string()); |
println!("{}", s.to_string()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">val wordsAll = scala.io.Source.fromURL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").getLines.toSeq |
||
/** |
/** |
||
Line 3,386: | Line 4,384: | ||
val ww = orderedWords( wordsAll ).sortBy( -_.length ) |
val ww = orderedWords( wordsAll ).sortBy( -_.length ) |
||
println( ww.takeWhile( _.length == ww.head.length ).mkString("\n") )</ |
println( ww.takeWhile( _.length == ww.head.length ).mkString("\n") )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,406: | Line 4,404: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
The following implementation uses a <tt>char>=?</tt> procedure that accepts an arbitrary number of arguments. This is allowed, but not required, by R5RS, and is provided by many Scheme implementations. It has been tested under GNU Guile 1.8.8. |
The following implementation uses a <tt>char>=?</tt> procedure that accepts an arbitrary number of arguments. This is allowed, but not required, by R5RS, and is provided by many Scheme implementations. It has been tested under GNU Guile 1.8.8. |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define sorted-words |
(define sorted-words |
||
(let ((port (open-input-file "unixdict.txt"))) |
(let ((port (open-input-file "unixdict.txt"))) |
||
Line 3,430: | Line 4,429: | ||
(newline))) |
(newline))) |
||
sorted-words) |
sorted-words) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 3,452: | Line 4,451: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func boolean: isOrdered (in string: word) is func |
const func boolean: isOrdered (in string: word) is func |
||
Line 3,500: | Line 4,499: | ||
end if; |
end if; |
||
write(wordList); |
write(wordList); |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,523: | Line 4,522: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var words = [[]] |
||
var file = %f'unixdict.txt' |
var file = %f'unixdict.txt' |
||
Line 3,536: | Line 4,535: | ||
} |
} |
||
say words[-1].join(' ')</ |
say words[-1].join(' ')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">BEGIN |
||
BOOLEAN PROCEDURE ISORDERED(W); TEXT W; |
BOOLEAN PROCEDURE ISORDERED(W); TEXT W; |
||
Line 3,594: | Line 4,594: | ||
END. |
END. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,620: | Line 4,620: | ||
{{works with|GNU Smalltalk}} |
{{works with|GNU Smalltalk}} |
||
< |
<syntaxhighlight lang="smalltalk">|file dict r t| |
||
file := FileStream open: 'unixdict.txt' mode: FileStream read. |
file := FileStream open: 'unixdict.txt' mode: FileStream read. |
||
dict := Set new. |
dict := Set new. |
||
Line 3,636: | Line 4,636: | ||
r := (r select: [:w| (w size) = ((r at: 1) size)]) asSortedCollection. |
r := (r select: [:w| (w size) = ((r at: 1) size)]) asSortedCollection. |
||
r do: [:e| e displayNl].</ |
r do: [:e| e displayNl].</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,657: | Line 4,657: | ||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
< |
<syntaxhighlight lang="spl">words = #.split(#.readtext("unixdict.txt","ascii"),#.lf) |
||
max = 0 |
max = 0 |
||
> i, 1..#.size(words,1) |
> i, 1..#.size(words,1) |
||
Line 3,671: | Line 4,671: | ||
result += word+#.crlf |
result += word+#.crlf |
||
< |
< |
||
#.output(result)</ |
#.output(result)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,691: | Line 4,691: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
=={{header|Standard ML}}== |
|||
<syntaxhighlight lang="sml">fun isOrdered s = |
|||
let |
|||
fun loop (i, c) = |
|||
let |
|||
val c' = String.sub (s, i) |
|||
in |
|||
c <= c' andalso loop (i + 1, c') |
|||
end |
|||
handle Subscript => true |
|||
in |
|||
loop (0, #"\^@") |
|||
end |
|||
fun longestOrdereds (s, prev as (len, lst)) = |
|||
let |
|||
val len' = size s |
|||
in |
|||
if len' >= len andalso isOrdered s then |
|||
if len' = len then (len, s :: lst) else (len', [s]) |
|||
else |
|||
prev |
|||
end |
|||
val () = print ((String.concatWith " " |
|||
o #2 |
|||
o foldr longestOrdereds (0, []) |
|||
o String.tokens Char.isSpace |
|||
o TextIO.inputAll) TextIO.stdIn ^ "\n")</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty</pre> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
guard |
guard |
||
Line 3,722: | Line 4,754: | ||
maxLengthGroup.forEach { print($0) } |
maxLengthGroup.forEach { print($0) } |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 3,746: | Line 4,778: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">package require http |
||
# Pick the ordered words (of maximal length) from a list |
# Pick the ordered words (of maximal length) from a list |
||
Line 3,770: | Line 4,802: | ||
set t [http::geturl "http://www.puzzlers.org/pub/wordlists/unixdict.txt"] |
set t [http::geturl "http://www.puzzlers.org/pub/wordlists/unixdict.txt"] |
||
puts [chooseOrderedWords [http::data $t]] |
puts [chooseOrderedWords [http::data $t]] |
||
http::cleanup $t</ |
http::cleanup $t</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty |
||
</pre> |
|||
=={{header|Transd}}== |
|||
<syntaxhighlight lang="Scheme">#lang transd |
|||
MainModule: { |
|||
_start: (lambda |
|||
(with fs FileStream() len 0 maxlen 0 words Vector<String>() |
|||
(open-r fs "/mnt/vault/tmp/unixdict.txt") ) |
|||
(for w in (read-lines fs) do |
|||
(= len (size w)) |
|||
(if (< len maxlen) continue) |
|||
(if (is-sorted w) |
|||
(if (< maxlen len) |
|||
(clear words) (= maxlen len)) |
|||
(append words w) |
|||
)) |
|||
(lout words) |
|||
)) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"] |
|||
</pre> |
</pre> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
SET data = REQUEST ("http://www.puzzlers.org/pub/wordlists/unixdict.txt") |
SET data = REQUEST ("http://www.puzzlers.org/pub/wordlists/unixdict.txt") |
||
Line 3,805: | Line 4,862: | ||
ENDLOOP |
ENDLOOP |
||
ENDCOMPILE |
ENDCOMPILE |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,828: | Line 4,885: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#show+ |
#show+ |
||
main = leql@bh$^ eql|= (ordered lleq)*~ unixdict_dot_txt</ |
main = leql@bh$^ eql|= (ordered lleq)*~ unixdict_dot_txt</syntaxhighlight> |
||
* <code>leql</code> is a binary predicate testing a pair of lists for less or equal length |
* <code>leql</code> is a binary predicate testing a pair of lists for less or equal length |
||
* <code>eql</code> tests for equal length |
* <code>eql</code> tests for equal length |
||
Line 3,863: | Line 4,920: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vba"> |
|||
<lang VBA> |
|||
Public Sub orderedwords(fname As String) |
Public Sub orderedwords(fname As String) |
||
' find ordered words in dict file that have the longest word length |
' find ordered words in dict file that have the longest word length |
||
Line 3,940: | Line 4,997: | ||
End If |
End If |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results: |
Results: |
||
Line 3,963: | Line 5,020: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
=={{header|VBScript}}== |
|||
Note: The input file is in the same folder as the script. |
|||
<syntaxhighlight lang="vb">Set objFSO = CreateObject("Scripting.FileSystemObject") |
|||
Set infile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\" &_ |
|||
"unixdict.txt",1) |
|||
list = "" |
|||
length = 0 |
|||
Do Until inFile.AtEndOfStream |
|||
line = infile.ReadLine |
|||
If IsOrdered(line) Then |
|||
If Len(line) > length Then |
|||
length = Len(line) |
|||
list = line & vbCrLf |
|||
ElseIf Len(line) = length Then |
|||
list = list & line & vbCrLf |
|||
End If |
|||
End If |
|||
Loop |
|||
WScript.StdOut.Write list |
|||
Function IsOrdered(word) |
|||
IsOrdered = True |
|||
prev_val = 0 |
|||
For i = 1 To Len(word) |
|||
If i = 1 Then |
|||
prev_val = Asc(Mid(word,i,1)) |
|||
ElseIf Asc(Mid(word,i,1)) >= prev_val Then |
|||
prev_val = Asc(Mid(word,i,1)) |
|||
Else |
|||
IsOrdered = False |
|||
Exit For |
|||
End If |
|||
Next |
|||
End Function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty</pre> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
< |
<syntaxhighlight lang="vedit">File_Open("unixdict.txt", BROWSE) |
||
#1 = 2 // length of longest word found |
#1 = 2 // length of longest word found |
||
Repeat (ALL) { |
Repeat (ALL) { |
||
Line 3,990: | Line 5,102: | ||
} |
} |
||
Buf_Quit(OK) // close file |
Buf_Quit(OK) // close file |
||
Reg_Type(10) // display results </ |
Reg_Type(10) // display results </syntaxhighlight> |
||
<pre>abbott |
<pre>abbott |
||
Line 4,009: | Line 5,121: | ||
knotty </pre> |
knotty </pre> |
||
=={{header| |
=={{header|V (Vlang)}}== |
||
{{trans|Wren}} |
|||
Note: The input file is in the same folder as the script. |
|||
<syntaxhighlight lang="Zig"> |
|||
<lang vb>Set objFSO = CreateObject("Scripting.FileSystemObject") |
|||
fn main() { |
|||
Set infile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\" &_ |
|||
words := os.read_file("./unixdict.txt") or {println(err) exit(-1)}.split_into_lines() |
|||
"unixdict.txt",1) |
|||
mut longest_len := 0 |
|||
list = "" |
|||
mut longest := []string{} |
|||
length = 0 |
|||
mut u_word := []u8{} |
|||
for word in words { |
|||
for chars in word {u_word << chars} |
|||
u_word.sort() |
|||
if word.len > longest_len { |
|||
if word == u_word.bytestr() { |
|||
longest_len = word.len |
|||
longest.clear() |
|||
longest << word |
|||
} |
|||
} |
|||
else if word.len == longest_len { |
|||
if word == u_word.bytestr() {longest << word} |
|||
} |
|||
u_word.clear() |
|||
} |
|||
println("The ${longest.len} ordered words with the longest length (${longest_len}) are:") |
|||
print(longest.join("\n")) |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
Do Until inFile.AtEndOfStream |
|||
<pre> |
|||
line = infile.ReadLine |
|||
The 16 ordered words with the longest length (6) are: |
|||
If IsOrdered(line) Then |
|||
abbott |
|||
If Len(line) > length Then |
|||
accent |
|||
length = Len(line) |
|||
accept |
|||
list = line & vbCrLf |
|||
access |
|||
ElseIf Len(line) = length Then |
|||
accost |
|||
list = list & line & vbCrLf |
|||
almost |
|||
End If |
|||
bellow |
|||
End If |
|||
billow |
|||
Loop |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knott |
|||
</pre> |
|||
=={{header|Wren}}== |
|||
WScript.StdOut.Write list |
|||
{{libheader|Wren-sort}} |
|||
<syntaxhighlight lang="wren">import "io" for File |
|||
import "./sort" for Sort |
|||
var words = File.read("unixdict.txt").split("\n") |
|||
Function IsOrdered(word) |
|||
var longestLen = 0 |
|||
IsOrdered = True |
|||
var longest = [] |
|||
prev_val = 0 |
|||
for (word in words) { |
|||
For i = 1 To Len(word) |
|||
if (word.count > longestLen) { |
|||
If i = 1 Then |
|||
if (Sort.isSorted(word.toList)) { |
|||
prev_val = Asc(Mid(word,i,1)) |
|||
longestLen = word.count |
|||
ElseIf Asc(Mid(word,i,1)) >= prev_val Then |
|||
longest.clear() |
|||
prev_val = Asc(Mid(word,i,1)) |
|||
longest.add(word) |
|||
Else |
|||
} |
|||
IsOrdered = False |
|||
} else if (word.count == longestLen) { |
|||
Exit For |
|||
if (Sort.isSorted(word.toList)) longest.add(word) |
|||
End If |
|||
} |
|||
Next |
|||
} |
|||
End Function</lang> |
|||
System.print("The %(longest.count) ordered words with the longest length (%(longestLen)) are:") |
|||
System.print(longest.join("\n"))</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
The 16 ordered words with the longest length (6) are: |
|||
abbott |
|||
accent |
accent |
||
accept |
accept |
||
Line 4,062: | Line 5,210: | ||
floppy |
floppy |
||
glossy |
glossy |
||
knotty |
knotty |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">string 0; \use zero-terminated strings |
|||
int MaxLen, Pass, I, Ch, Ch0; |
|||
char Word(25); |
|||
def LF=$0A, CR=$0D, EOF=$1A; |
|||
[FSet(FOpen("unixdict.txt", 0), ^I); |
|||
MaxLen:= 0; |
|||
for Pass:= 1 to 2 do |
|||
[OpenI(3); |
|||
repeat I:= 0; Ch0:= 0; |
|||
loop [repeat Ch:= ChIn(3) until Ch # CR; \remove possible CR |
|||
if Ch=LF or Ch=EOF then |
|||
[if I > MaxLen then MaxLen:= I; |
|||
if I=MaxLen & Pass=2 then |
|||
[Word(I):= 0; Text(0, Word); CrLf(0)]; |
|||
quit; |
|||
]; |
|||
Word(I):= Ch; |
|||
if Ch < Ch0 then |
|||
[repeat Ch:= ChIn(3) until Ch=LF or Ch=EOF; |
|||
quit; |
|||
]; |
|||
Ch0:= Ch; |
|||
I:= I+1; |
|||
]; |
|||
until Ch = EOF; |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
abbott |
|||
accent |
|||
accept |
|||
access |
|||
accost |
|||
almost |
|||
bellow |
|||
billow |
|||
biopsy |
|||
chilly |
|||
choosy |
|||
choppy |
|||
effort |
|||
floppy |
|||
glossy |
|||
knotty |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
One pass, dictionary stays on disk: |
One pass, dictionary stays on disk: |
||
< |
<syntaxhighlight lang="zkl">var words=L(), sz=0; // some state |
||
fcn isLex(word){ word.reduce(fcn(p,c){ (p<=c) and c or T(Void.Stop,False) }) } |
fcn isLex(word){ word.reduce(fcn(p,c){ (p<=c) and c or T(Void.Stop,False) }) } |
||
File("dict.txt").pump(Void,fcn(w){ |
File("dict.txt").pump(Void,fcn(w){ |
||
Line 4,076: | Line 5,274: | ||
}) |
}) |
||
println("Num words: %d, all size %d\n".fmt(words.len(),sz)); |
println("Num words: %d, all size %d\n".fmt(words.len(),sz)); |
||
words.pump(Console.println);</ |
words.pump(Console.println);</syntaxhighlight> |
||
Or, reading dictionary into memory: |
Or, reading dictionary into memory: |
||
< |
<syntaxhighlight lang="zkl">fcn isLex(word){ word.reduce(fcn(p,c){ (p<=c) and c or T(Void.Stop,False) }) } |
||
lwords:=File("dict.txt").readln(*).apply("strip").filter(isLex); |
lwords:=File("dict.txt").readln(*).apply("strip").filter(isLex); |
||
max:=lwords.reduce(fcn(n,w){ w.len()>n and w.len() or n },0); |
max:=lwords.reduce(fcn(n,w){ w.len()>n and w.len() or n },0); |
||
lwords=lwords.filter(fcn(w,n){ w.len()==n },max); |
lwords=lwords.filter(fcn(w,n){ w.len()==n },max); |
||
println("Num words: %d, all size %d\n".fmt(lwords.len(),max)); |
println("Num words: %d, all size %d\n".fmt(lwords.len(),max)); |
||
words.pump(Console.println);</ |
words.pump(Console.println);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,104: | Line 5,302: | ||
knotty |
knotty |
||
</pre> |
</pre> |
||
{{omit from|6502 Assembly|unixdict.txt is much larger than the CPU's address space.}} |
|||
{{omit from|8080 Assembly|See 6502 Assembly.}} |
|||
{{omit from|Z80 Assembly|See 6502 Assembly.}} |
Revision as of 14:38, 10 January 2024
You are encouraged to solve this task according to the task description, using any language you may know.
An ordered word is a word in which the letters appear in alphabetic order.
Examples include abbey and dirt.
Find and display all the ordered words in the dictionary unixdict.txt that have the longest word length.
(Examples that access the dictionary file locally assume that you have downloaded this file yourself.)
The display needs to be shown on this page.
- Metrics
- Counting
- Word frequency
- Letter frequency
- Jewels and stones
- I before E except after C
- Bioinformatics/base count
- Count occurrences of a substring
- Count how many vowels and consonants occur in a string
- Remove/replace
- XXXX redacted
- Conjugate a Latin verb
- Remove vowels from a string
- String interpolation (included)
- Strip block comments
- Strip comments from a string
- Strip a set of characters from a string
- Strip whitespace from a string -- top and tail
- Strip control codes and extended characters from a string
- Anagrams/Derangements/shuffling
- Word wheel
- ABC problem
- Sattolo cycle
- Knuth shuffle
- Ordered words
- Superpermutation minimisation
- Textonyms (using a phone text pad)
- Anagrams
- Anagrams/Deranged anagrams
- Permutations/Derangements
- Find/Search/Determine
- ABC words
- Odd words
- Word ladder
- Semordnilap
- Word search
- Wordiff (game)
- String matching
- Tea cup rim text
- Alternade words
- Changeable words
- State name puzzle
- String comparison
- Unique characters
- Unique characters in each string
- Extract file extension
- Levenshtein distance
- Palindrome detection
- Common list elements
- Longest common suffix
- Longest common prefix
- Compare a list of strings
- Longest common substring
- Find common directory path
- Words from neighbour ones
- Change e letters to i in words
- Non-continuous subsequences
- Longest common subsequence
- Longest palindromic substrings
- Longest increasing subsequence
- Words containing "the" substring
- Sum of the digits of n is substring of n
- Determine if a string is numeric
- Determine if a string is collapsible
- Determine if a string is squeezable
- Determine if a string has all unique characters
- Determine if a string has all the same characters
- Longest substrings without repeating characters
- Find words which contains all the vowels
- Find words which contains most consonants
- Find words which contains more than 3 vowels
- Find words which first and last three letters are equals
- Find words which odd letters are consonants and even letters are vowels or vice_versa
- Formatting
- Substring
- Rep-string
- Word wrap
- String case
- Align columns
- Literals/String
- Repeat a string
- Brace expansion
- Brace expansion using ranges
- Reverse a string
- Phrase reversals
- Comma quibbling
- Special characters
- String concatenation
- Substring/Top and tail
- Commatizing numbers
- Reverse words in a string
- Suffixation of decimal numbers
- Long literals, with continuations
- Numerical and alphabetical suffixes
- Abbreviations, easy
- Abbreviations, simple
- Abbreviations, automatic
- Song lyrics/poems/Mad Libs/phrases
- Mad Libs
- Magic 8-ball
- 99 Bottles of Beer
- The Name Game (a song)
- The Old lady swallowed a fly
- The Twelve Days of Christmas
- Tokenize
- Text between
- Tokenize a string
- Word break problem
- Tokenize a string with escaping
- Split a character string based on change of character
- Sequences
11l
V words = File(‘unixdict.txt’).read().split("\n")
V ordered = words.filter(word -> word == sorted(word))
V maxlen = max(ordered, key' w -> w.len).len
V maxorderedwords = ordered.filter(word -> word.len == :maxlen)
print(maxorderedwords.join(‘ ’))
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Acornsoft Lisp
In find-longest-ordered-words
, len
, word
, and words
are used as local variables. open
returns an integer as a file-handle. chars
returns the number of characters in a symbol's name. eq
can be used to compare numbers for equality.
(defun longest-ordered-words ()
(find-longest-ordered-words
(open 'notes/unixdict!.txt t)))
(defun find-longest-ordered-words
(h (len . 0) (word) (words))
(loop
(until (eof h)
(close h)
words)
(setq word (readline h))
(cond ((lessp (chars word) len))
((not (ordered-p word)))
((eq (chars word) len)
(setq words (cons word words)))
(t
(setq len (chars word))
(setq words (list word))))))
(defun ordered-p (word)
(nondecreasing-p
(mapc ordinal (explode word))))
(defun nondecreasing-p (numbers)
(or (null numbers)
(null (cdr numbers))
(and (not (greaterp (car numbers) (cadr numbers)))
(nondecreasing-p (cdr numbers)))))
- Output:
(longest-ordered-words)
will return
(knotty glossy floppy effort choppy choosy chilly biopsy billow bellow almost accost access accept accent abbott)
Action!
In the following solution the input file is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
CHAR ARRAY line(256)
BYTE FUNC IsOrderedWord(CHAR ARRAY word)
BYTE len,i
len=word(0)
IF len<=1 THEN RETURN (1) FI
FOR i=1 TO len-1
DO
IF word(i)>word(i+1) THEN
RETURN (0)
FI
OD
RETURN (1)
BYTE FUNC FindLongestOrdered(CHAR ARRAY fname)
BYTE max,dev=[1]
max=0
Close(dev)
Open(dev,fname,4)
WHILE Eof(dev)=0
DO
InputSD(dev,line)
IF line(0)>max AND IsOrderedWord(line)=1 THEN
max=line(0)
FI
OD
Close(dev)
RETURN (max)
PROC FindWords(CHAR ARRAY fname BYTE n)
BYTE count,dev=[1]
Close(dev)
Open(dev,fname,4)
WHILE Eof(dev)=0
DO
InputSD(dev,line)
IF line(0)=n AND IsOrderedWord(line)=1 THEN
Print(line) Put(32)
FI
OD
Close(dev)
RETURN
PROC Main()
CHAR ARRAY fname="H6:UNIXDICT.TXT"
BYTE max
PrintE("Finding the longest words...")
PutE()
max=FindLongestOrdered(fname)
FindWords(fname,max)
RETURN
- Output:
Screenshot from Atari 8-bit computer
Finding the longest words... abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Ada
with Ada.Text_IO, Ada.Containers.Indefinite_Vectors;
use Ada.Text_IO;
procedure Ordered_Words is
package Word_Vectors is new Ada.Containers.Indefinite_Vectors
(Index_Type => Positive, Element_Type => String);
use Word_Vectors;
File : File_Type;
Ordered_Words : Vector;
Max_Length : Positive := 1;
begin
Open (File, In_File, "unixdict.txt");
while not End_Of_File (File) loop
declare
Word : String := Get_Line (File);
begin
if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
if Word'Length > Max_Length then
Max_Length := Word'Length;
Ordered_Words.Clear;
Ordered_Words.Append (Word);
elsif Word'Length = Max_Length then
Ordered_Words.Append (Word);
end if;
end if;
end;
end loop;
for Word of Ordered_Words loop
Put_Line (Word);
end loop;
Close (File);
end Ordered_Words;
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Aime
integer
ordered(data s)
{
integer a, c, p;
a = 1;
p = -1;
for (, c in s) {
if (c < p) {
a = 0;
break;
} else {
p = c;
}
}
a;
}
integer
main(void)
{
file f;
text s;
index x;
f.affix("unixdict.txt");
while (f.line(s) != -1) {
if (ordered(s)) {
x.v_list(~s).append(s);
}
}
l_ucall(x.back, o_, 0, "\n");
return 0;
}
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
ALGOL 68
.
PROC ordered = (STRING s)BOOL:
BEGIN
FOR i TO UPB s - 1 DO IF s[i] > s[i+1] THEN return false FI OD;
TRUE EXIT
return false: FALSE
END;
IF FILE input file;
STRING file name = "unixdict.txt";
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;
INT idx := 1;
INT max length := 0;
WHILE NOT at eof
DO
STRING word;
get(input file, (word, newline));
IF UPB word >= max length
THEN IF ordered(word)
THEN
max length := UPB word;
IF idx > UPB words
THEN
[1 : UPB words + 20] STRING tmp;
tmp[1 : UPB words] := words;
words := tmp
FI;
words[idx] := word;
idx +:= 1
FI
FI
OD;
print(("Maximum length of ordered words: ", whole(max length, -4), newline));
FOR i TO idx-1
DO
IF UPB words[i] = max length THEN print((words[i], newline)) FI
OD
FI
- Output:
Maximum length of ordered words: 6 abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Amazing Hopper
#include <basico.h>
#define MAX_LINE 30
algoritmo
fd=0
word={}, result={}
old_word="",new_word=""
fijar separador (NULO)
abrir para leer("basica/unixdict.txt",fd)
iterar mientras ' no es fin de archivo (fd) '
usando 'MAX_LINE', leer línea desde(fd),
---copiar en 'old_word'---, separar para 'word '
word, ---retener--- ordenar esto,
encadenar en 'new_word'
new_word, meter según (#(old_word == new_word), result)
reiterar
cerrar archivo(fd)
result ---retener---, obtener largo ---retener---
obtener máximo valor, es mayor o igual?, replicar esto
compactar esto
fijar separador 'NL', luego imprime todo
terminar
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
APL
Works in NARS2000 APL; ("Objects" menu -> New, paste this in, save. Then run in the main session manager).
result←longest_ordered_words file_path
f←file_path ⎕NTIE 0 ⍝ open file
text←⎕NREAD f 'char8' ⍝ read vector of 8bit chars
⎕NUNTIE f ⍝ close file
lines←text⊂⍨~text∊(⎕UCS 10 13) ⍝ split into lines (\r\n)
⍝ filter only words with ordered characters
ordered_words←lines/⍨{(⍳∘≢≡⍋)⍵}¨lines
⍝ find max of word lengths, filter only words with that length
result←ordered_words/⍨lengths=⍨⌈/lengths←≢¨ordered_words
The ordered character filter is a train which uses gradeup to say which order you would have to pick the characters, to put them in order. e.g. ⍋ 'zxy' is 2 3 1 because you'd have to pick the second character, then the third, then the first, to put them in order. If they are in order then the result is the integers 1 2 3 .. to the length of the word.
And it generates the integers up to the length of the word, e.g. 1 2 3. and compares if those two arrays are the same.
Then uses that information as a filter against the original word list.
The result is: lengths of each ordered word, which equal the max-reduce of the list of lengths (longest word), used as a filter against the ordered word list.
- Output:
longest_ordered_words 'd:\unixdict.txt' abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
AppleScript
use AppleScript version "2.3.1" -- Mac OS 10.9 (Mavericks) or later — for these 'use' commands.
use sorter : script "Insertion sort" -- https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript.
use scripting additions
on longestOrderedWords(wordList)
script o
property allWords : wordList
property orderedWords : {}
end script
set longestWordLength to 0
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
ignoring case
repeat with i from 1 to (count o's allWords)
set thisWord to item i of o's allWords
set thisWordLength to (count thisWord)
if (thisWordLength ≥ longestWordLength) then
set theseCharacters to thisWord's characters
tell sorter to sort(theseCharacters, 1, -1)
set sortedWord to theseCharacters as text
if (sortedWord = thisWord) then
if (thisWordLength > longestWordLength) then
set o's orderedWords to {thisWord}
set longestWordLength to thisWordLength
else
set end of o's orderedWords to thisWord
end if
end if
end if
end repeat
end ignoring
set AppleScript's text item delimiters to astid
return (o's orderedWords)
end longestOrderedWords
-- Test code:
local wordList
set wordList to paragraphs of (read (((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as alias) as «class utf8»)
-- ignoring white space, punctuation and diacriticals
return longestOrderedWords(wordList)
--- end ignoring
- Output:
{"abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"}
Arturo
ordered?: function [w]->
w = join sort split w
words: read.lines relative "unixdict.txt"
ret: new []
loop words 'wrd [
if ordered? wrd ->
'ret ++ #[w: wrd l: size wrd]
]
sort.descending.by: 'l 'ret
maxl: get first ret 'l
print sort map select ret 'x -> maxl = x\l
'x -> x\w
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
AutoHotkey
This script uses the ASCII value of each letter to determine its place in the alphabet. Given a dictionary not all in the same case, StringLower could be used. This script assumes a locally downloaded copy of the dictionary, but UrlDownloadToFile could be used. The purpose of the GUI is simply to display a field where the user can copy the list. MsgBox could be used, or FileAppend.
MaxLen=0
Loop, Read, UnixDict.txt ; Assigns A_LoopReadLine to each line of the file
{
thisword := A_LoopReadLine ; Just for readability
blSort := isSorted(thisWord) ; reduce calls to IsSorted to improve performance
ThisLen := StrLen(ThisWord) ; reduce calls to StrLen to improve performance
If (blSort = true and ThisLen = maxlen)
list .= ", " . thisword
Else If (blSort = true and ThisLen > maxlen)
{
list := thisword
maxlen := ThisLen
}
}
IsSorted(word){ ; This function uses the ASCII value of the letter to determine its place in the alphabet.
; Thankfully, the dictionary is in all lowercase
lastchar=0
Loop, parse, word
{
if ( Asc(A_LoopField) < lastchar )
return false
lastchar := Asc(A_loopField)
}
return true
}
GUI, Add, Edit, w300 ReadOnly, %list%
GUI, Show
return ; End Auto-Execute Section
GUIClose:
ExitApp
Output:
abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty
AWK
BEGIN {
abc = "abcdefghijklmnopqrstuvwxyz"
}
{
# Check if this line is an ordered word.
ordered = 1 # true
left = -1
for (i = 1; i <= length($0); i++) {
right = index(abc, substr($0, i, 1))
if (right == 0 || left > right) {
ordered = 0 # false
break
}
left = right
}
if (ordered) {
score = length($0)
if (score > best["score"]) {
# Reset the list of best ordered words.
best["score"] = score
best["count"] = 1
best[1] = $0
} else if (score == best["score"]) {
# Add this word to the list.
best[++best["count"]] = $0
}
}
}
END {
# Print the list of best ordered words.
for (i = 1; i <= best["count"]; i++)
print best[i]
}
You must provide unixdict.txt as input.
$ awk -f ordered-words.awk unixdict.txt abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
BaCon
'Ordered words - improved version
OPTION COLLAPSE TRUE
list$ = LOAD$("unixdict.txt")
FOR word$ IN list$ STEP NL$
term$ = EXTRACT$(SORT$(EXPLODE$(word$, 1)), " ")
IF word$ = term$ THEN
IF LEN(term$) > MaxLen THEN
MaxLen = LEN(term$)
result$ = word$
ELIF LEN(term$) = MaxLen THEN
result$ = APPEND$(result$, 0, word$, NL$)
END IF
END IF
NEXT
PRINT result$
- Output:
prompt$ bacon ordered-words Converting 'ordered-words.bac'... done, 29 lines were processed in 0.004 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 accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
BBC BASIC
An optimisation is that the word isn't checked for being ordered unless it is at least as long as the current maximum.
dict% = OPENIN("unixdict.txt")
IF dict%=0 ERROR 100, "Failed to open dictionary file"
max% = 2
REPEAT
A$ = GET$#dict%
IF LENA$ >= max% THEN
i% = 0
REPEAT i% += 1
UNTIL ASCMID$(A$,i%) > ASCMID$(A$,i%+1)
IF i% = LENA$ THEN
IF i% > max% max% = i% : list$ = ""
list$ += A$ + CHR$13 + CHR$10
ENDIF
ENDIF
UNTIL EOF#dict%
CLOSE #dict%
PRINT list$
END
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Befunge
The word list is read from stdin, so ideally requires an interpreter that can accept input redirected from a file. It's still possible to enter the test data manually, though - you would just need to mark the end of the list with a blank line.
Memory usage isn't very efficient - the matching list is stored one word per line - so on a standard Befunge-93 interpreter there is an upper limit of 22 matches. This is not a problem for the unix dictionary, which only requires 16, but it's theoretically possible that other data sets could run out of space.
00p30p>_010p120p0>#v0~>>\$::48*\`\"~"`+!>>#v_$:#v_>30g:!#v_1-30p55+0>:30g3+g\1v
>0#v _$^#::\p04:<^+>#1^#\p01:p02*g02!`\g01:<@$ _ ,#!>#:<$<^<!:g03$<_^#!`\g00:+<
^<o>\30g2+p40g1+^0p00p03+1*g03!-g00 < < < < < <:>#$:#$00g#<\#<`#<!#<2#$0g#<*#<_
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
BQN
(↕∘≠≡⍋)
checks for an ordered word.
¯1⊑(≠¨⊔⊢)
groups by length, and takes the last (longest length) group.
words←•FLines "unixdict.txt"
¯1⊑(≠¨⊔⊢)(↕∘≠≡⍋)¨⊸/words
⟨ "abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" "biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty" ⟩
Bracmat
( orderedWords
= bow result longestLength word character
. 0:?bow
& :?result
& 0:?longestLength
& @( get$(!arg,STR)
: ?
( [!bow %?word \n [?bow ?
& @( !word
: ( ? %@?character <%@!character ?
| ?
( [!longestLength
& !word !result:?result
| [>!longestLength:[?longestLength
& !word:?result
|
)
)
)
& ~`
)
)
| !result
)
& orderedWords$"unixdict.txt"
knotty glossy floppy effort choppy choosy chilly biopsy billow bellow almost accost access accept accent abbott
Burlesque
ln{so}f[^^{L[}>mL[bx(==)[+(L[)+]f[uN
- Output:
$ ./burlesque --file ordered_words.brl < unixdict.txt abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
C
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#define MAXLEN 100
typedef char TWord[MAXLEN];
typedef struct Node {
TWord word;
struct Node *next;
} Node;
int is_ordered_word(const TWord word) {
assert(word != NULL);
int i;
for (i = 0; word[i] != '\0'; i++)
if (word[i] > word[i + 1] && word[i + 1] != '\0')
return 0;
return 1;
}
Node* list_prepend(Node* words_list, const TWord new_word) {
assert(new_word != NULL);
Node *new_node = malloc(sizeof(Node));
if (new_node == NULL)
exit(EXIT_FAILURE);
strcpy(new_node->word, new_word);
new_node->next = words_list;
return new_node;
}
Node* list_destroy(Node *words_list) {
while (words_list != NULL) {
Node *temp = words_list;
words_list = words_list->next;
free(temp);
}
return words_list;
}
void list_print(Node *words_list) {
while (words_list != NULL) {
printf("\n%s", words_list->word);
words_list = words_list->next;
}
}
int main() {
FILE *fp = fopen("unixdict.txt", "r");
if (fp == NULL)
return EXIT_FAILURE;
Node *words = NULL;
TWord line;
unsigned int max_len = 0;
while (fscanf(fp, "%99s\n", line) != EOF) {
if (strlen(line) > max_len && is_ordered_word(line)) {
max_len = strlen(line);
words = list_destroy(words);
words = list_prepend(words, line);
} else if (strlen(line) == max_len && is_ordered_word(line)) {
words = list_prepend(words, line);
}
}
fclose(fp);
list_print(words);
return EXIT_SUCCESS;
}
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Alternative version with dynamic array:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#define MAXLEN 100
typedef char TWord[MAXLEN];
typedef struct WordsArray {
TWord *words;
size_t len;
} WordsArray;
int is_ordered_word(const TWord word) {
assert(word != NULL);
int i;
for (i = 0; word[i] != '\0'; i++)
if (word[i] > word[i + 1] && word[i + 1] != '\0')
return 0;
return 1;
}
void array_append(WordsArray *words_array, const TWord new_word) {
assert(words_array != NULL);
assert(new_word != NULL);
assert((words_array->len == 0) == (words_array->words == NULL));
words_array->len++;
words_array->words = realloc(words_array->words,
words_array->len * sizeof(words_array->words[0]));
if (words_array->words == NULL)
exit(EXIT_FAILURE);
strcpy(words_array->words[words_array->len-1], new_word);
}
void array_free(WordsArray *words_array) {
assert(words_array != NULL);
free(words_array->words);
words_array->words = NULL;
words_array->len = 0;
}
void list_print(WordsArray *words_array) {
assert(words_array != NULL);
size_t i;
for (i = 0; i < words_array->len; i++)
printf("\n%s", words_array->words[i]);
}
int main() {
FILE *fp = fopen("unixdict.txt", "r");
if (fp == NULL)
return EXIT_FAILURE;
WordsArray words;
words.len = 0;
words.words = NULL;
TWord line;
line[0] = '\0';
unsigned int max_len = 0;
while (fscanf(fp, "%99s\n", line) != EOF) { // 99 = MAXLEN - 1
if (strlen(line) > max_len && is_ordered_word(line)) {
max_len = strlen(line);
array_free(&words);
array_append(&words, line);
} else if (strlen(line) == max_len && is_ordered_word(line)) {
array_append(&words, line);
}
}
fclose(fp);
list_print(&words);
array_free(&words);
return EXIT_SUCCESS;
}
Mmap
Shorter and potentially much faster version with mmap (2)
. No stinky malloc
or scanf
calls.
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <string.h>
int ordered(char *s, char **end)
{
int r = 1;
while (*++s != '\n' && *s != '\r' && *s != '\0')
if (s[0] < s[-1]) r = 0;
*end = s;
return r;
}
int main()
{
char *buf, *word, *end, *tail;
struct stat st;
int longest = 0, len, fd;
if ((fd = open("unixdict.txt", O_RDONLY)) == -1) err(1, "read error");
fstat(fd, &st);
if (!(buf = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)))
err(1, "mmap");
for (word = end = buf; end < buf + st.st_size; word = end) {
while (*word == '\r' || *word == '\n') word++;
if (!ordered(word, &end)) continue;
if ((len = end - word + 1) < longest) continue;
if (len > longest) {
tail = buf; /* longer words found; reset out buffer */
longest = len;
}
/* use the same mmap'd region to store output. because of MAP_PRIVATE,
* change will not go back to file. mmap is copy on write, and we are using
* only the head space to store output, so kernel doesn't need to copy more
* than the words we saved--in this case, one page tops.
*/
memcpy(tail, word, len);
tail += len;
*tail = '\0';
}
printf(buf);
munmap(buf, st.st_size);
close(fd);
return 0;
}
C#
using System;
using System.Linq;
using System.Net;
static class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
string text = client.DownloadString("http://www.puzzlers.org/pub/wordlists/unixdict.txt");
string[] words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
var query = from w in words
where IsOrderedWord(w)
group w by w.Length into ows
orderby ows.Key descending
select ows;
Console.WriteLine(string.Join(", ", query.First().ToArray()));
}
private static bool IsOrderedWord(string w)
{
for (int i = 1; i < w.Length; i++)
if (w[i] < w[i - 1])
return false;
return true;
}
}
Output:
abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty
C++
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
bool ordered(const std::string &word)
{
return std::is_sorted(word.begin(), word.end()); // C++11
}
int main()
{
std::ifstream infile("unixdict.txt");
if (!infile) {
std::cerr << "Can't open word file\n";
return -1;
}
std::vector<std::string> words;
std::string word;
int longest = 0;
while (std::getline(infile, word)) {
int length = word.length();
if (length < longest) continue; // don't test short words
if (ordered(word)) {
if (longest < length) {
longest = length; // set new minimum length
words.clear(); // reset the container
}
words.push_back(word);
}
}
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Clojure
(defn is-sorted? [coll]
(not-any? pos? (map compare coll (next coll))))
(defn take-while-eqcount [coll]
(let [n (count (first coll))]
(take-while #(== n (count %)) coll)))
(with-open [rdr (clojure.java.io/reader "unixdict.txt")]
(->> rdr
line-seq
(filter is-sorted?)
(sort-by count >)
take-while-eqcount
(clojure.string/join ", ")
println))
Output
abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty
CLU
is_ordered = proc (s: string) returns (bool)
last: char := '\000'
for c: char in string$chars(s) do
if last > c then return(false) end
last := c
end
return(true)
end is_ordered
lines = iter (s: stream) yields (string)
while true do
yield(stream$getl(s))
except when end_of_file: break end
end
end lines
ordered_words = proc (s: stream) returns (array[string])
words: array[string]
max_len: int := 0
for word: string in lines(s) do
if is_ordered(word) then
len: int := string$size(word)
if len > max_len then
max_len := len
words := array[string]$[]
elseif len = max_len then
array[string]$addh(words,word)
end
end
end
return(words)
end ordered_words
start_up = proc ()
dict: stream := stream$open(file_name$parse("unixdict.txt"), "read")
words: array[string] := ordered_words(dict)
stream$close(dict)
po: stream := stream$primary_output()
for word: string in array[string]$elements(words) do
stream$putl(po, word)
end
end start_up
- Output:
accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. ABC-WORDS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DICT ASSIGN TO DISK
ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD DICT
LABEL RECORD STANDARD
VALUE OF FILE-ID IS "unixdict.txt".
01 ENTRY.
03 WORD PIC X(32).
03 LETTERS PIC X OCCURS 32 TIMES, REDEFINES WORD.
WORKING-STORAGE SECTION.
01 LEN PIC 99.
01 MAXLEN PIC 99 VALUE 0.
01 I PIC 99.
01 OK-FLAG PIC X.
88 OK VALUE '*'.
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT DICT.
FIND-LONGEST-WORD.
READ DICT, AT END CLOSE DICT, GO TO PRINT-LONGEST-WORDS.
PERFORM CHECK-WORD.
GO TO FIND-LONGEST-WORD.
PRINT-LONGEST-WORDS.
ALTER VALID-WORD TO PROCEED TO SHOW-WORD.
OPEN INPUT DICT.
READ-WORDS.
READ DICT, AT END CLOSE DICT, STOP RUN.
PERFORM CHECK-WORD.
GO TO READ-WORDS.
CHECK-WORD.
MOVE ZERO TO LEN.
INSPECT WORD TALLYING LEN
FOR CHARACTERS BEFORE INITIAL SPACE.
MOVE '*' TO OK-FLAG.
PERFORM CHECK-CHAR-PAIR VARYING I FROM 2 BY 1
UNTIL NOT OK OR I IS GREATER THAN LEN.
IF OK, PERFORM DO-WORD.
CHECK-CHAR-PAIR.
IF LETTERS(I - 1) IS GREATER THAN LETTERS(I),
MOVE SPACE TO OK-FLAG.
DO-WORD SECTION.
VALID-WORD.
GO TO CHECK-LENGTH.
CHECK-LENGTH.
IF LEN IS GREATER THAN MAXLEN, MOVE LEN TO MAXLEN.
GO TO DONE.
SHOW-WORD.
IF LEN IS EQUAL TO MAXLEN, DISPLAY WORD.
DONE.
EXIT.
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
CoffeeScript
ordered_word = (word) ->
for i in [0...word.length - 1]
return false unless word[i] <= word[i+1]
true
show_longest_ordered_words = (candidates, dict_file_name) ->
words = ['']
for word in candidates
continue if word.length < words[0].length
if ordered_word word
words = [] if word.length > words[0].length
words.push word
return if words[0] == '' # we came up empty
console.log "Longest Ordered Words (source=#{dict_file_name}):"
for word in words
console.log word
dict_file_name = 'unixdict.txt'
file_content = require('fs').readFileSync dict_file_name
dict_words = file_content.toString().split '\n'
show_longest_ordered_words dict_words, dict_file_name
output
> coffee ordered_words.coffee
Longest Ordered Words (source=unixdict.txt):
abbott
accent
accept
access
accost
almost
bellow
billow
biopsy
chilly
choosy
choppy
effort
floppy
glossy
knotty
Common Lisp
(defun orderedp (word)
(reduce (lambda (prev curr)
(when (char> prev curr) (return-from orderedp nil))
curr)
word)
t)
(defun longest-ordered-words (filename)
(let ((result nil))
(with-open-file (s filename)
(loop
with greatest-length = 0
for word = (read-line s nil)
until (null word)
do (let ((length (length word)))
(when (and (>= length greatest-length)
(orderedp word))
(when (> length greatest-length)
(setf greatest-length length
result nil))
(push word result)))))
(nreverse result)))
CL-USER> (longest-ordered-words "unixdict.txt")
("abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow"
"biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty")
Or in a more functional way:
(defun orderedp (word)
(apply #'char<= (coerce word 'list)))
(let* ((words (uiop:read-file-lines "unixdict.txt"))
(ordered (delete-if-not #'orderedp words))
(maxlen (apply #'max (mapcar #'length ordered)))
(result (delete-if-not (lambda (l) (= l maxlen)) ordered :key #'length)))
(format t "~{~A~^, ~}" result))
- Output:
abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty
Cowgol
Because Cowgol is meant to run on small systems, this program does not store the words in memory. Instead, it scans through the file twice; once to find the maximum length of the ordered words it finds, and once to actually print the words.
include "cowgol.coh";
include "strings.coh";
include "file.coh";
var filename: [uint8] := "unixdict.txt";
# Call a subroutine for every line in a file
interface LineCb(line: [uint8]);
sub ForEachLine(fcb: [FCB], fn: LineCb) is
var linebuf: uint8[256];
var bufptr := &linebuf[0];
var len := FCBExt(fcb); # get length of file
FCBSeek(fcb, 0); # start at beginning of file
while len > 0 loop
var ch := FCBGetChar(fcb);
if ch == '\n' then
# end of line, terminate string
[bufptr] := 0;
fn(&linebuf[0]);
bufptr := &linebuf[0];
else
# add char to buffer
[bufptr] := ch;
bufptr := @next bufptr;
end if;
len := len - 1;
end loop;
# If the file doesn't cleanly end on a line terminator,
# also call for last incomplete line
if ch != '\n' then
[bufptr] := 0;
fn(&linebuf[0]);
end if;
end sub;
# Check if the letters in a word appear in alphabetical order
sub isOrdered(word: [uint8]): (r: uint8) is
var cr := [word];
word := @next word;
loop
var cl := cr;
cr := [word];
word := @next word;
if cr < 32 then
r := 1;
return;
elseif (cl | 32) > (cr | 32) then
r := 0;
return;
end if;
end loop;
end sub;
# Find maximum length of ordered words
var maxLen: uint8 := 0;
sub MaxOrderedLength implements LineCb is
var len := StrLen(line) as uint8;
if maxLen < len and isOrdered(line) != 0 then
maxLen := len;
end if;
end sub;
# Print all ordered words matching maximum length
sub PrintMaxLenWord implements LineCb is
if maxLen == StrLen(line) as uint8 and isOrdered(line) != 0 then
print(line);
print_nl();
end if;
end sub;
var fcb: FCB;
if FCBOpenIn(&fcb, filename) != 0 then
print("cannot open unixdict.txt\n");
ExitWithError();
end if;
ForEachLine(&fcb, MaxOrderedLength);
ForEachLine(&fcb, PrintMaxLenWord);
var foo := FCBClose(&fcb);
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
D
Simple Procedural Version
void main() {
import std.stdio, std.algorithm, std.range, std.string;
string[] result;
size_t maxLen;
foreach (string word; "unixdict.txt".File.lines) {
word = word.chomp;
immutable len = word.walkLength;
if (len < maxLen || !word.isSorted)
continue;
if (len > maxLen) {
result = [word];
maxLen = len;
} else
result ~= word;
}
writefln("%-(%s\n%)", result);
}
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Faster Procedural Version
Faster, same output.
void main() {
import std.stdio, std.algorithm, std.file, std.range;
string[] result;
size_t maxWalkLen;
foreach (word; "unixdict.txt".readText.splitter) {
if (word.length >= maxWalkLen && word.isSorted) {
immutable wlen = word.walkLength;
if (wlen > maxWalkLen) {
result.length = 0;
maxWalkLen = wlen;
}
result ~= word.idup;
}
}
writefln("%-(%s\n%)", result);
}
Functional Version
Shorter, same output.
void main() {
import std.stdio, std.algorithm, std.range, std.file, std.string;
auto words = "unixdict.txt".readText.split.filter!isSorted;
immutable maxLen = words.map!q{a.length}.reduce!max;
writefln("%-(%s\n%)", words.filter!(w => w.length == maxLen));
}
Fastest Memory Mapped Version
Lower level, much faster with large input files (about as fast as the memory mapped C version). The output is the same. It works only with ASCII texts, but unlike the C entry this is portable.
import std.stdio, core.stdc.string, std.mmfile, std.algorithm;
const(char)[] findWord(const char[] s) pure nothrow @safe @nogc {
size_t wordEnd = 0;
while (wordEnd < s.length && s[wordEnd] != '\n' && s[wordEnd] != '\r')
wordEnd++;
return s[0 .. wordEnd];
}
void main() {
auto mmf = new MmFile("unixdict.txt", MmFile.Mode.readCopyOnWrite, 0, null);
auto txt = cast(char[])(mmf[]);
size_t maxLen = 0, outStart = 0;
for (size_t wordStart = 0; wordStart < txt.length; ) {
while (wordStart < txt.length &&
(txt[wordStart] == '\r' || txt[wordStart] == '\n'))
wordStart++;
const word = findWord(txt[wordStart .. $]);
wordStart += word.length;
if (word.length < maxLen || !word.isSorted)
continue;
if (word.length > maxLen) {
// Longer ordered word found, reset the out buffer.
outStart = 0;
maxLen = word.length;
}
// Use the same mmap'd region to store output. Because of
// Mode.readCopyOnWrite, change will not go back to file.
// We are using only the head space to store output, so
// kernel doesn't need to copy more than the words we saved,
// in this case, one page tops.
memcpy(&txt[outStart], word.ptr, word.length);
outStart += word.length;
txt[outStart++] = '\n'; // Words separator in out buffer.
}
txt[0 .. outStart].write;
}
Delphi
program POrderedWords;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes, IdHTTP;
function IsOrdered(const s:string): Boolean;
var
I: Integer;
begin
Result := Length(s)<2; // empty or 1 char strings are ordered
for I := 2 to Length(s) do
if s[I]<s[I-1] then // can improve using case/localization to order...
Exit;
Result := True;
end;
function ProcessDictionary(const AUrl: string): string;
var
slInput: TStringList;
I, WordSize: Integer;
begin
slInput := TStringList.Create;
try
with TIdHTTP.Create(nil) do try
slInput.Text := Get(AUrl);
finally
Free;
end;
// or use slInput.LoadFromFile('yourfilename') to load from a local file
WordSize :=0;
for I := 0 to slInput.Count-1 do begin
if IsOrdered(slInput[I]) then
if (Length(slInput[I]) = WordSize) then
Result := Result + slInput[I] + ' '
else if (Length(slInput[I]) > WordSize) then begin
Result := slInput[I] + ' ';
WordSize := Length(slInput[I]);
end;
end;
finally
slInput.Free;
end;
end;
begin
try
WriteLn(ProcessDictionary('http://www.puzzlers.org/pub/wordlists/unixdict.txt'));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Output: dictionary directly processed from the URL
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Draco
\util.g
proc nonrec is_ordered(*char str) bool:
while str* /= '\e' and str* <= (str+1)* do
str := str + 1
od;
str* = '\e' or (str+1)* = '\e'
corp
proc nonrec main() void:
[64]char buf;
*char str;
word length, max_length;
file(1024) dictfile;
channel input text dict;
str := &buf[0];
max_length := 0;
open(dict, dictfile, "unixdict.txt");
while readln(dict; str) do
if is_ordered(str) then
length := CharsLen(str);
if length > max_length then max_length := length fi
fi
od;
close(dict);
open(dict, dictfile, "unixdict.txt");
while readln(dict; str) do
if is_ordered(str) then
length := CharsLen(str);
if length = max_length then writeln(str) fi
fi
od;
close(dict)
corp
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
E
pragma.enable("accumulator")
def words := <http://www.puzzlers.org/pub/wordlists/unixdict.txt>.getText().split("\n")
def ordered := accum [] for word ? (word.sort() <=> word) in words { _.with(word) }
def maxLen := accum 0 for word in ordered { _.max(word.size()) }
def maxOrderedWords := accum [] for word ? (word.size() <=> maxLen) in ordered { _.with(word) }
println(" ".rjoin(maxOrderedWords))
One-pass procedural algorithm which avoids keeping the entire data set in memory:
def best := [].diverge()
for `@word$\n` ? (word.sort() <=> word) in <http://www.puzzlers.org/pub/wordlists/unixdict.txt> {
if (best.size() == 0) {
best.push(word)
} else if (word.size() > best[0].size()) {
best(0) := [word] # replace all
} else if (word.size() <=> best[0].size()) {
best.push(word)
}
}
println(" ".rjoin(best.snapshot()))
Output: abbott accent accept access accost almost bellow billow biopsy chilly choosy
choppy effort floppy glossy knotty
EchoLisp
(define (ordered? str)
(for/and ([i (in-range 1 (string-length str))])
(string-ci<=? (string-ref str (1- i)) (string-ref str i))))
(define (ordre words)
(define wl 0)
(define s 's)
(for/fold (len 0) ((w words))
(set! wl (string-length w))
#:continue (< wl len)
#:when (ordered? w)
#:continue (and (= len wl) (push s w))
(push (stack s) w) ;; start a new list of length wl
wl)
(stack->list s))
;; output
(load 'unixdict)
(ordre (text-parse unixdict))
→ (abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty)
;; with the dictionaries provided with EchoLisp
;; french
→ (accentué) ;; ordered, longest, and ... self-reference
;; english
→ (Adelops alloquy beefily begorry billowy egilops)
Elixir
File.read!("unixdict.txt")
|> String.split
|> Enum.filter(fn word -> String.codepoints(word) |> Enum.sort |> Enum.join == word end)
|> Enum.group_by(fn word -> String.length(word) end)
|> Enum.max_by(fn {length,_words} -> length end)
|> elem(1)
|> Enum.sort
|> Enum.each(fn word -> IO.puts word end)
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Erlang
-module( ordered_words ).
-export( [is_ordered/1, task/0] ).
is_ordered( Word ) -> lists:sort( Word ) =:= Word.
task() ->
ok = find_unimplemented_tasks:init_http(),
Ordered_words = [X || X <- words(), is_ordered(X)],
Sorted_longest_length_first = lists:reverse( sort_with_length( Ordered_words ) ),
[{Max_length, _Word1} | _T] = Sorted_longest_length_first,
Longest_length_first = lists:takewhile( fun({Length, _Word2}) -> Length =:= Max_length end, Sorted_longest_length_first ),
[X || {_Length, X} <- Longest_length_first].
sort_with_length( Words ) ->
Words_with_length_first = [{erlang:length(X), X} || X <- Words],
lists:sort( Words_with_length_first ).
words() -> anagrams_deranged:words_from_url( "http://www.puzzlers.org/pub/wordlists/unixdict.txt" ).
- Output:
2> ordered_words:task(). ["knotty","glossy","floppy","effort","choppy","choosy", "chilly","biopsy","billow","bellow","almost","accost", "access","accept","accent","abbott"]
Euphoria
include misc.e
type ordered(sequence s)
for i = 1 to length(s)-1 do
-- assume all items in the sequence are atoms
if s[i]>s[i+1] then
return 0
end if
end for
return 1
end type
integer maxlen
sequence words
object word
constant fn = open("unixdict.txt","r")
maxlen = -1
while 1 do
word = gets(fn)
if atom(word) then
exit
end if
word = word[1..$-1] -- truncate new-line
if length(word) >= maxlen and ordered(word) then
if length(word) > maxlen then
maxlen = length(word)
words = {}
end if
words = append(words,word)
end if
end while
close(fn)
pretty_print(1,words,{2})
Output:
{ "abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty" }
F#
open System
open System.IO
let longestOrderedWords() =
let isOrdered = Seq.pairwise >> Seq.forall (fun (a,b) -> a <= b)
File.ReadLines("unixdict.txt")
|> Seq.filter isOrdered
|> Seq.groupBy (fun s -> s.Length)
|> Seq.sortBy (fst >> (~-))
|> Seq.head |> snd
longestOrderedWords() |> Seq.iter (printfn "%s")
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Factor
USING: grouping http.client io io.encodings.utf8 io.files
io.files.temp kernel math memoize sequences sequences.extras
unicode.case urls ;
IN: rosetta-code.ordered-words
MEMO: word-list ( -- seq )
"unixdict.txt" temp-file dup exists? [
URL" http://puzzlers.org/pub/wordlists/unixdict.txt"
over download-to
] unless utf8 file-lines ;
: ordered-word? ( word -- ? )
>lower [ <= ] monotonic? ;
: ordered-words-main ( -- )
word-list [ ordered-word? ] filter
all-longest [ print ] each ;
Output:
( scratchpad ) USING: ordered-words-main ; ( scratchpad ) main abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Fantom
class Main
{
public static Bool ordered (Str word)
{
word.chars.all |Int c, Int i -> Bool|
{
(i == (word.size-1) || c <= word.chars[i+1])
}
}
public static Void main ()
{
Str[] words := [,]
File(`unixdict.txt`).eachLine |Str word|
{
if (ordered(word))
{
if (words.isEmpty || words.first.size < word.size)
{ // reset the list
words = [word]
}
else if (words.size >= 1 && words.first.size == word.size)
{ // add word to existing ones
words.add (word)
}
}
}
echo (words.join (" "))
}
}
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
FBSL
Downloads the list from puzzlers.org.
#APPTYPE CONSOLE
FUNCTION RESTfulGET(url)
DIM %HTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
CALLMETHOD(HTTP, ".open %s, %s, %d", "GET", url, FALSE)
CALLMETHOD(HTTP, ".send")
RETURN GETVALUE("%s", HTTP, ".ResponseText")
END FUNCTION
DIM $TEXT = RESTfulGET("http://www.puzzlers.org/pub/wordlists/unixdict.txt")
DIM dict[] = Split(TEXT, CHR(10))
DIM max AS INTEGER = UBOUND(dict)
DIM theword AS STRING
DIM words[]
FOR DIM i = 0 TO max
theWord = dict[i]
IF isOrdered(theWord) THEN
words[LEN(theWord)] = words[LEN(theWord)] & " " & theWord
END IF
NEXT
PRINT words[UBOUND(words)]
PAUSE
FUNCTION isOrdered(s)
FOR DIM i = 1 TO LEN(s) - 1
IF s{i} > s{i + 1} THEN
RETURN FALSE
END IF
NEXT
RETURN TRUE
END FUNCTION
Output
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty Press any key to continue...
Forth
This program uses a string stack, which means that all matching words are stored on a stack. The longest word ends up on the top of the stack.
include lib/stmstack.4th \ include string stack library
: check-word ( a n -- a n f)
2dup bl >r \ start off with a space
begin
dup \ when not end of word
while
over c@ r@ >= \ check character
while
r> drop over c@ >r chop \ chop character off
repeat r> drop nip 0= \ cleanup and set flag
;
: open-file ( -- h)
1 dup argn = abort" Usage: ordered infile"
args input open error? abort" Cannot open file"
dup use \ return and use the handle
;
: read-file ( --)
0 >r \ begin with zero length
begin
refill \ EOF detected?
while
0 parse dup r@ >= \ equal or longer string length?
if \ check the word and adjust length
check-word if r> drop dup >r >s else 2drop then
else \ if it checks out, put on the stack
2drop \ otherwise drop the word
then
repeat r> drop \ clean it up
;
: read-back ( --)
s> dup >r type cr \ longest string is on top of stack
begin s> dup r@ >= while type cr repeat
2drop r> drop \ keep printing until shorter word
; \ has been found
: ordered ( --)
open-file s.clear read-file read-back close
; \ open file, clear the stack, read file
\ read it back and close the file
ordered
Since the longest word is on top of the stack, the only thing to be done is to pop all words from the stack until a shorter word is encountered. Consequently, all words are listed in reverse order:
knotty glossy floppy effort choppy choosy chilly biopsy billow bellow almost accost access accept accent abbott
Fortran
!***************************************************************************************
module ordered_module
!***************************************************************************************
implicit none
!the dictionary file:
integer,parameter :: file_unit = 1000
character(len=*),parameter :: filename = 'unixdict.txt'
!maximum number of characters in a word:
integer,parameter :: max_chars = 50
type word
character(len=max_chars) :: str !the word from the dictionary
integer :: n = 0 !length of this word
logical :: ordered = .false. !if it is an ordered word
end type word
!the dictionary structure:
type(word),dimension(:),allocatable :: dict
contains
!***************************************************************************************
!******************************************************************************
function count_lines_in_file(fid) result(n_lines)
!******************************************************************************
implicit none
integer :: n_lines
integer,intent(in) :: fid
character(len=1) :: tmp
integer :: i
integer :: ios
!the file is assumed to be open already.
rewind(fid) !rewind to beginning of the file
n_lines = 0
do !read each line until the end of the file.
read(fid,'(A1)',iostat=ios) tmp
if (ios < 0) exit !End of file
n_lines = n_lines + 1 !row counter
end do
rewind(fid) !rewind to beginning of the file
!******************************************************************************
end function count_lines_in_file
!******************************************************************************
!******************************************************************************
pure elemental function ordered_word(word) result(yn)
!******************************************************************************
! turns true if word is an ordered word, false if it is not.
!******************************************************************************
implicit none
character(len=*),intent(in) :: word
logical :: yn
integer :: i
yn = .true.
do i=1,len_trim(word)-1
if (ichar(word(i+1:i+1))<ichar(word(i:i))) then
yn = .false.
exit
end if
end do
!******************************************************************************
end function ordered_word
!******************************************************************************
!***************************************************************************************
end module ordered_module
!***************************************************************************************
!****************************************************
program main
!****************************************************
use ordered_module
implicit none
integer :: i,n,n_max
!open the dictionary and read in all the words:
open(unit=file_unit,file=filename) !open the file
n = count_lines_in_file(file_unit) !count lines in the file
allocate(dict(n)) !allocate dictionary structure
do i=1,n !
read(file_unit,'(A)') dict(i)%str !each line is a word in the dictionary
dict(i)%n = len_trim(dict(i)%str) !save word length
end do
close(file_unit) !close the file
!use elemental procedure to get ordered words:
dict%ordered = ordered_word(dict%str)
!max length of an ordered word:
n_max = maxval(dict%n, mask=dict%ordered)
!write the output:
do i=1,n
if (dict(i)%ordered .and. dict(i)%n==n_max) write(*,'(A,A)',advance='NO') trim(dict(i)%str),' '
end do
write(*,*) ''
!****************************************************
end program main
!****************************************************
Output
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
FreeBASIC
' FB 1.05.0 Win64
Function isOrdered(s As Const String) As Boolean
If Len(s) <= 1 Then Return True
For i As Integer = 1 To Len(s) - 1
If s[i] < s[i - 1] Then Return False
Next
Return True
End Function
Dim words() As String
Dim word As String
Dim maxLength As Integer = 0
Dim count As Integer = 0
Open "undict.txt" For Input As #1
While Not Eof(1)
Line Input #1, word
If isOrdered(word) Then
If Len(word) = maxLength Then
Redim Preserve words(0 To count)
words(count) = word
count += 1
ElseIf Len(word) > maxLength Then
Erase words
maxLength = Len(word)
Redim words(0 To 0)
words(0) = word
count = 1
End If
End If
Wend
Close #1
Print "The ordered words with the longest length ("; Str(maxLength); ") in undict.txt are :"
Print
For i As Integer = 0 To UBound(words)
Print words(i)
Next
Print
Print "Press any key to quit"
Sleep
- Output:
The ordered words with the longest length (6) in undict.txt are : abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Frink
url="https://web.archive.org/web/20180611003215if_/http://www.puzzlers.org:80/pub/wordlists/unixdict.txt"
a = sort[select[lines[url], {|w| charList[w] == sort[charList[w]] } ], {|a,b| length[a] <=> length[b]}]
println[sort[select[a, {|w, longest| length[w] == longest}, length[last[a]]]]]
- Output:
[abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty]
FutureBasic
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
local fn Words as CFArrayRef
CFURLRef url = fn URLWithString( @"https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt" )
CFStringRef string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
end fn = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )
local fn IsOrderedWord( string as CFStringRef ) as BOOL
BOOL flag = YES
long i
unichar chr, prevChr = 0
for i = 0 to len(string) - 1
chr = fn StringCharacterAtIndex( string, i )
if ( chr < prevChr )
flag = NO : break
end if
prevChr = chr
next
end fn = flag
void local fn DoIt
CFStringRef string
CFArrayRef words = fn Words
long length, maxLen = 0
CFMutableStringRef orderedWords = fn MutableStringWithCapacity(0)
for string in words
length = len(string)
if ( length < maxLen ) then continue
if ( fn IsOrderedWord( string ) )
if ( length > maxLen )
MutableStringSetString( orderedWords, @"" )
maxLen = length
end if
MutableStringAppendFormat( orderedWords, @"%@\n", string )
end if
next
print orderedWords
end fn
fn DoIt
HandleEvents
Gambas
Public Sub Main()
Dim sDict As String = File.Load(User.Home &/ "unixdict.txt") 'Store the 'Dictionary'
Dim sOrdered As New String[] 'To store ordered words
Dim sHold As New String[] 'General store
Dim sTemp As String 'Temp variable
Dim siCount As Short 'Counter
For Each sTemp In Split(sDict, gb.NewLine) 'For each word in the Dictionary
For siCount = 1 To Len(sTemp) 'Loop for each letter in the word
sHold.Add(Mid(sTemp, siCount, 1)) 'Put each letter in sHold array
Next
sHold.Sort() 'Sort sHold (abbott = abboot, zoom = mooz)
If sTemp = sHold.Join("") Then sOrdered.Add(sTemp) 'If they are the same (abbott(OK) mooz(not OK)) then add to sOrdered
sHold.Clear 'Empty sHold
Next
siCount = 0 'Reset siCount
For Each sTemp In sOrdered 'For each of the Ordered words
If Len(sTemp) > siCount Then siCount = Len(sTemp) 'Count the length of the word and keep a record of the longest length
Next
For Each sTemp In sOrdered 'For each of the Ordered words
If Len(sTemp) = siCount Then sHold.Add(sTemp) 'If it is one of the longest add it to sHold
Next
sHold.Sort() 'Sort sHold
Print sHold.Join(gb.NewLine) 'Display the result
End
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Go
Go has strings and Unicode and stuff, but with the dictionary all ASCII and lower case, strings and Unicode seem overkill. I just worked with byte slices here, only converting the final result to strings for easy output.
package main
import (
"bytes"
"fmt"
"io/ioutil"
)
func main() {
// read into memory in one chunk
b, err := ioutil.ReadFile("unixdict.txt")
if err != nil {
fmt.Println(err)
return
}
// split at line ends
bss := bytes.Split(b, []byte{'\n'})
// accumulate result
var longest int
var list [][]byte
for _, bs := range bss {
// don't bother with words shorter than
// our current longest ordered word
if len(bs) < longest {
continue
}
// check for ordered property
var lastLetter byte
for i := 0; ; i++ {
if i == len(bs) {
// end of word. it's an ordered word.
// save it and break from loop
if len(bs) > longest {
longest = len(bs)
list = list[:0]
}
list = append(list, bs)
break
}
// check next letter
b := bs[i]
if b < 'a' || b > 'z' {
continue // not a letter. ignore.
}
if b < lastLetter {
break // word not ordered.
}
// letter passes test
lastLetter = b
}
}
// print result
for _, bs := range list {
fmt.Println(string(bs))
}
}
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Groovy
Solution:
def isOrdered = { word -> def letters = word as List; letters == ([] + letters).sort() }
assert isOrdered('abbey')
assert !isOrdered('cat')
def dictUrl = new URL('http://www.puzzlers.org/pub/wordlists/unixdict.txt')
def orderedWords = dictUrl.readLines().findAll { isOrdered(it) }
def owMax = orderedWords*.size().max()
orderedWords.findAll { it.size() == owMax }.each { println it }
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Haskell
-- Words are read from the standard input. We keep in memory only the current
-- set of longest, ordered words.
--
-- Limitation: the locale's collation order is not take into consideration.
isOrdered wws@(_:ws) = and $ zipWith (<=) wws ws
longestOrderedWords = reverse . snd . foldl f (0,[]) . filter isOrdered
where f (max, acc) w =
let len = length w in
case compare len max of
LT -> (max, acc)
EQ -> (max, w:acc)
GT -> (len, [w])
main = do
str <- getContents
let ws = longestOrderedWords $ words str
mapM_ putStrLn ws
Output:
abbott
accent
accept
access
accost
almost
bellow
billow
biopsy
chilly
choosy
choppy
effort
floppy
glossy
knotty
Alternative version:
import Control.Monad (liftM)
isSorted wws@(_ : ws) = and $ zipWith (<=) wws ws
getLines = liftM lines . readFile
main = do
ls <- getLines "unixdict.txt"
let ow = filter isSorted ls
let maxl = foldr max 0 (map length ow)
print $ filter (\w -> (length w) == maxl) ow
Huginn
import Algorithms as algo;
import Mathematics as math;
import Network as net;
import Text as text;
main( argv_ ) {
url = size( argv_ ) > 1
? argv_[1]
: "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt";
words = algo.materialize( algo.map( net.get( url ).stream, string.strip ), list );
ordered = algo.materialize(
algo.filter(
words,
@( word ){ word == ∑( algo.map( algo.sorted( word ), string ) ); }
),
list
);
maxLen = algo.reduce( ordered, @( x, y ){ math.max( x, size( y ) ); }, 0 );
maxOrderedWords = algo.materialize(
algo.filter( ordered, @[maxLen]( word ){ size( word ) == maxLen; } ),
list
);
print( "{}\n".format( text.join( algo.sorted( maxOrderedWords ), " " ) ) );
return ( 0 );
}
Icon and Unicon
link strings
procedure main(A)
f := open(\A[1]) | stop("Give dictionary file name on command line")
every (maxLen := 0, maxLen <= *(w := !f), w == csort(w)) do {
if maxLen <:= *w then maxList := [] #discard any shorter sorted words
put(maxList, w)
}
every write(!\maxList)
end
strings provides csort which sorts the letters within a string
Output:
->ordered_words unixdict.txt abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty ->
Io
file := File clone openForReading("./unixdict.txt")
words := file readLines
file close
maxLen := 0
orderedWords := list()
words foreach(word,
if( (word size >= maxLen) and (word == (word asMutable sort)),
if( word size > maxLen,
maxLen = word size
orderedWords empty
)
orderedWords append(word)
)
)
orderedWords join(" ") println
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
J
NB. from http://wiki.puzzlers.org/pub/wordlists/unixdict.txt
oWords=: (#~ ] = /:~L:0) cutLF fread 'unixdict.txt'
;:inv (#~ (= >./)@:(#@>)) oWords
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Recap:
- read dictionary (
fread 'unixdict.txt'
) - break into words, one per line (
cutLF
) - find ordered words (
(#~ ] = /:~L:0)
) - select the longest ordered words (
(#~ (= >./)@:(#@>))oWords
) - format for display (using ;:inv)
Java
This example assumes there is a local copy of the dictionary whose path is given as the first argument to the program.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Ordered {
private static boolean isOrderedWord(String word){
char[] sortedWord = word.toCharArray();
Arrays.sort(sortedWord);
return word.equals(new String(sortedWord));
}
public static void main(String[] args) throws IOException{
List<String> orderedWords = new LinkedList<String>();
BufferedReader in = new BufferedReader(new FileReader(args[0]));
while(in.ready()){
String word = in.readLine();
if(isOrderedWord(word)) orderedWords.add(word);
}
in.close();
Collections.<String>sort(orderedWords, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return new Integer(o2.length()).compareTo(o1.length());
}
});
int maxLen = orderedWords.get(0).length();
for(String word: orderedWords){
if(word.length() == maxLen){
System.out.println(word);
}else{
break;
}
}
}
}
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Using Java 16
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public final class OrderedWords {
public static void main(String[] aArgs) throws IOException {
List<String> ordered = Files.lines(Path.of("unixdict.txt"))
.filter( word -> isOrdered(word) ).toList();
final int maxLength = ordered.stream().map( word -> word.length() ).max(Integer::compare).get();
ordered.stream().filter( word -> word.length() == maxLength ).forEach(System.out::println);
}
private static boolean isOrdered(String aWord) {
return aWord.chars()
.mapToObj( i -> (char) i )
.sorted()
.map(String::valueOf)
.reduce("", String::concat)
.equals(aWord);
}
}
The same as the Java example above.
JavaScript
Using node.js:
var fs = require('fs'), print = require('sys').print;
fs.readFile('./unixdict.txt', 'ascii', function (err, data) {
var is_ordered = function(word){return word.split('').sort().join('') === word;},
ordered_words = data.split('\n').filter(is_ordered).sort(function(a, b){return a.length - b.length}).reverse(),
longest = [], curr = len = ordered_words[0].length, lcv = 0;
while (curr === len){
longest.push(ordered_words[lcv]);
curr = ordered_words[++lcv].length;
};
print(longest.sort().join(', ') + '\n');
});
Output:
abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty
Alternative version (also using Node.js):
var http = require('http');
http.get({
host: 'www.puzzlers.org',
path: '/pub/wordlists/unixdict.txt'
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var words = data.split('\n');
var max = 0;
var ordered = [];
words.forEach(function(word) {
if (word.split('').sort().join('') != word) return;
if (word.length == max) {
ordered.push(word);
} else if (word.length > max) {
ordered = [word];
max = word.length;
}
});
console.log(ordered.join(', '));
});
});
jq
def is_sorted:
if length <= 1 then true
else .[0] <= .[1] and (.[1:] | is_sorted)
end;
def longest_ordered_words:
# avoid string manipulation:
def is_ordered: explode | is_sorted;
map(select(is_ordered))
| (map(length)|max) as $max
| map( select(length == $max) );
split("\n") | longest_ordered_words
- Output:
["abbott","accent","accept","access","accost","almost","bellow","billow","biopsy","chilly","choosy","choppy","effort","floppy","glossy","knotty"]
Julia
Built-in function:
issorted("abc") # true
Main:
lst = readlines("data/unixdict.txt")
filter!(issorted, lst)
filter!(x -> length(x) == maximum(length, lst), lst)
println.(lst)
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
K
w@&d=|/d:#:'w:d@&&/'{~x<y}':'d:0:"unixdict.txt"
("abbott"
"accent"
"accept"
"access"
"accost"
"almost"
"bellow"
"billow"
"biopsy"
"chilly"
"choosy"
"choppy"
"effort"
"floppy"
"glossy"
"knotty")
Kotlin
import java.net.URI
fun main() {
val url = URI(
"https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt"
).toURL()
val words = url.openStream().bufferedReader().use {
var max = 0
it.lineSequence()
.filter { it.asSequence().windowed(2).all { it[0] <= it[1] } }
.sortedByDescending(String::length)
.takeWhile { word -> word.length >= max.also { max = word.length } }
.toList()
}
words.forEach(::println)
}
Lambdatalk
{def maxOrderedWords
{def isOrdered
{lambda {:w}
{W.equal? :w {W.sort before :w}}}}
{def getOrdered
{lambda {:w}
{if {isOrdered :w} then :w else}}}
{def pushOrdered
{lambda {:m :w}
{if {= {W.length :w} :m} then {br}:w else}}}
{def maxOrderedWords.i
{lambda {:sortedWords}
{let { {:orderedWords {S.map getOrdered :sortedWords}} }
{S.map {{lambda {:m :w} {pushOrdered :m :w}}
{max {S.map {lambda {:w} {W.length :w}}
:orderedWords}}}
:orderedWords}}}}
{lambda {:s}
{maxOrderedWords.i {S.replace else by el_se in :s}}}}
-> maxOrderedWords
{maxOrderedWords UNIX.DICT}
->
abbott
accent
accept
access
accost
almost
bellow
billow
biopsy
chilly
choosy
choppy
effort
floppy
glossy
knotty
Lang5
: >string-index
"" split
"&'0123456789abcdefghijklmnopqrstuvwxyz" "" split
swap index collapse ;
: chars "" split length swap drop ;
: cr "\n" . ;
: nip swap drop ;
: ordered?
dup grade subscript != '+ reduce if 0 else -1 then ;
: filtering
[] '_ set
0 do read
2dup chars
<=
if dup >string-index ordered?
if 2dup chars
<
if nip dup chars swap
[] '_ set
then
_ swap append '_ set
'. . # progress dot
else drop
then
else drop
then
eof if break then loop
cr _ . cr
;
: ordered-words
'< 'unixdict.txt open 'fh set
fh fin filtering fh close ;
ordered-words
- Output:
[ abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty ]
Lasso
local(f = file('unixdict.txt'), words = array, ordered = array, maxleng = 0)
#f->dowithclose => {
#f->foreachLine => {
#words->insert(#1)
}
}
with w in #words
do => {
local(tosort = #w->asString->values)
#tosort->sort
if(#w->asString == #tosort->join('')) => {
#ordered->insert(#w->asString)
#w->asString->size > #maxleng ? #maxleng = #w->asString->size
}
}
with w in #ordered
where #w->size == #maxleng
do => {^ #w + '\r' ^}
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Liberty BASIC
'Ordered wordsFrom Rosetta Code
open "unixdict.txt" for input as #1
'this is not normal DOS/Windows file.
'It LF delimited, not CR LF
'So Line input would not work.
lf$=chr$(10)
curLen=0
wordList$=""
while not(eof(#1))
a$=inputto$(#1, lf$)
'now, check word
flag = 1
c$ = left$(a$,1)
for i = 2 to len(a$)
d$ = mid$(a$,i,1)
if c$>d$ then flag=0: exit for
c$=d$
next
'ckecked, proceed if ordered word
if flag then
if curLen=len(a$) then
wordList$=wordList$+" "+a$
else
if curLen<len(a$) then
curLen=len(a$)
wordList$=a$
end if
end if
end if
wend
close #1
print wordList$
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Lingo
Code ported from Lua solution.
-- Contents of unixdict.txt passed as string
on printLongestOrderedWords (words)
res = []
maxlen = 0
_player.itemDelimiter = numtochar(10)
cnt = words.item.count
repeat with i = 1 to cnt
w = words.item[i]
len = w.length
ordered = TRUE
repeat with j = 2 to len
if chartonum(w.char[j-1])>chartonum(w.char[j]) then
ordered = FALSE
exit repeat
end if
end repeat
if ordered then
if len > maxlen then
res = [w]
maxlen = len
else if len = maxlen then
res.add(w)
end if
end if
end repeat
put res
end
- Output:
-- ["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]
Lua
fp = io.open( "dictionary.txt" )
maxlen = 0
list = {}
for w in fp:lines() do
ordered = true
for l = 2, string.len(w) do
if string.byte( w, l-1 ) > string.byte( w, l ) then
ordered = false
break
end
end
if ordered then
if string.len(w) > maxlen then
list = {}
list[1] = w
maxlen = string.len(w)
elseif string.len(w) == maxlen then
list[#list+1] = w
end
end
end
for _, w in pairs(list) do
print( w )
end
fp:close()
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Maple
lst := StringTools:-Split(Import("http://www.puzzlers.org/pub/wordlists/unixdict.txt"), "\n"):
longest := 0:
words := Array():
i := 1:
for word in lst do
if StringTools:-IsSorted(word) then
len := StringTools:-Length(word):
if len > longest then
longest := len:
words := Array():
words(1) := word:
i := 2:
elif len = longest then
words(i) := word:
i++:
end if;
end if;
end do;
for word in words do print(word); end do;
- Output:
"abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" "biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty"
Mathematica/Wolfram Language
Module[{max,
data = Select[Import["http://www.puzzlers.org/pub/wordlists/unixdict.txt", "List"],
OrderedQ[Characters[#]] &]},
max = Max[StringLength /@ data];
Select[data, StringLength[#] == max &]]
However Mathematica has built in dictionaries for many languages, so here is a more general version...
maxWords[language_String] := Module[{max,data = Select[DictionaryLookup[{language, "*"}],OrderedQ[Characters[#]] &]},
max = Max[StringLength /@ data];
Select[data, StringLength[#] == max &]]
findLargestOrderedWord["English"] {"billowy"} findLargestOrderedWord["French"] {"accent", "afflux", "bijoux", "billot", "dehors", "effort"} findLargestOrderedWord["Spanish"] {"abenuz", "chillo"}
MATLAB / Octave
maxlen = 0;
listlen= 0;
fid = fopen('unixdict.txt','r');
while ~feof(fid)
str = fgetl(fid);
if any(diff(abs(str))<0) continue; end;
if length(str)>maxlen,
list = {str};
maxlen = length(str);
elseif length(str)==maxlen,
list{end+1} = str;
end;
end
fclose(fid);
printf('%s\n',list{:});
Returns:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Nanoquery
import Nanoquery.IO
def is_ordered(word)
word = str(word)
if len(word) < 2
return true
end
for i in range(1, len(word) - 1)
if str(word[i]) < str(word[i - 1])
return false
end
end
return true
end
longest = 0
words = {}
for word in new(File, "unixdict.txt").read()
if is_ordered(word)
if len(word) > longest
longest = len(word)
words.clear()
words.append(word)
else if len(word) = longest
words.append(word)
end
end
end
println words
- Output:
[abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty]
NetRexx
/* NetRexx */
options replace format comments java crossref savelog symbols binary
unixdict = 'unixdict.txt'
do
wmax = Integer.MIN_VALUE
dwords = ArrayList()
inrdr = BufferedReader(FileReader(File(unixdict)))
loop label ln while inrdr.ready
dword = Rexx(inrdr.readLine).strip
if isOrdered(dword) then do
dwords.add(dword)
if dword.length > wmax then
wmax = dword.length
end
end ln
inrdr.close
witerator = dwords.listIterator
loop label wd while witerator.hasNext
dword = Rexx witerator.next
if dword.length < wmax then do
witerator.remove
end
end wd
dwords.trimToSize
say dwords.toString
catch ex = IOException
ex.printStackTrace
end
return
method isOrdered(dword = String) inheritable static binary returns boolean
wchars = dword.toCharArray
Arrays.sort(wchars)
return dword.equalsIgnoreCase(String(wchars))
- Output
[abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty]
Nim
import strutils
const DictFile = "unixdict.txt"
func isSorted(s: string): bool =
var last = char.low
for c in s:
if c < last: return false
last = c
result = true
var
mx = 0
words: seq[string]
for word in DictFile.lines:
if word.len >= mx and word.isSorted:
if word.len > mx:
words.setLen(0)
mx = word.len
words.add word
echo words.join(" ")
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
OCaml
let input_line_opt ic =
try Some(input_line ic)
with End_of_file -> None
(* load each line in a list *)
let read_lines ic =
let rec aux acc =
match input_line_opt ic with
| Some line -> aux (line :: acc)
| None -> (List.rev acc)
in
aux []
let char_list_of_string str =
let lst = ref [] in
String.iter (fun c -> lst := c :: !lst) str;
(List.rev !lst)
let is_ordered word =
let rec aux = function
| c1::c2::tl ->
if c1 <= c2
then aux (c2::tl)
else false
| c::[] -> true
| [] -> true (* should only occur with an empty string *)
in
aux (char_list_of_string word)
let longest_words words =
let res, _ =
List.fold_left
(fun (lst, n) word ->
let len = String.length word in
let comp = compare len n in
match lst, comp with
| lst, 0 -> ((word::lst), n) (* len = n *)
| lst, -1 -> (lst, n) (* len < n *)
| _, 1 -> ([word], len) (* len > n *)
| _ -> assert false
)
([""], 0) words
in
(List.rev res)
let () =
let ic = open_in "unixdict.txt" in
let words = read_lines ic in
let lower_words = List.map String.lowercase words in
let ordered_words = List.filter is_ordered lower_words in
let longest_ordered_words = longest_words ordered_words in
List.iter print_endline longest_ordered_words
Output:
$ ocaml ordered_words.ml abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Oforth
: longWords
| w longest l s |
0 ->longest
File new("unixdict.txt") forEach: w [
w size dup ->s longest < ifTrue: [ continue ]
w sort w == ifFalse: [ continue ]
s longest > ifTrue: [ s ->longest ListBuffer new ->l ]
l add(w)
] l ;
- Output:
>longWords . [abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty]
ooRexx
Adapted for ooRexx
/*REXX list (the longest) ordered word(s) from a supplied dictionary. */
iFID= 'UNIXDICT.TXT'
w.=''
mL=0
Do j=1 While lines(iFID)\==0
x=linein(iFID)
w=length(x)
If w>=mL Then Do
Parse Upper Var x xU 1 z 2
Do k=2 To w
_=substr(xU, k, 1)
If \datatype(_, 'U') Then Iterate
If _<z Then Iterate j
z=_
End
mL=w
w.w=w.w x
End
End
nn=words(w.mL)
Say nn 'word's(nn) "found (of length" mL')'
Say ''
Do n=1 To nn
Say word(w.mL, n)
End
Exit
s: Return left('s',arg(1)>1)
- Output:
Same as REXX'
PARI/GP
ordered(s)=my(v=Vecsmall(s),t=97);for(i=1,#v,if(v[i]>64&&v[i]<91,v[i]+=32);if(v[i]<97||v[i]>122||v[i]<t,return(0),t=v[i]));1
v=select(ordered,readstr("~/unixdict.txt"));
N=vecmax(apply(length,v));
select(s->#s==N, v)
- Output:
%1 = ["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]
Perl
#!/usr/bin/perl
use strict;
use warnings;
open(FH, "<", "unixdict.txt") or die "Can't open file!\n";
my @words;
while (<FH>) {
chomp;
push @{$words[length]}, $_ if $_ eq join("", sort split(//));
}
close FH;
print "@{$words[-1]}\n";
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Phix
with javascript_semantics sequence words = unix_dict() integer maxlen = -1, found = 0 for i=1 to length(words) do string word = words[i] integer l = length(word) if l>=maxlen and word=sort(word) then if l>maxlen then {found,maxlen} = {0,l} end if found += 1 words[found] = word end if end for printf(1,"The %d longest ordered words:\n %s\n",{found,join_by(words[1..found],1,4," ","\n ")})
- Output:
The 16 longest ordered words: abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Phixmonti
include ..\Utilitys.pmt
0 var maxlen
( ) var words
0 var f
def getword f fgets dup -1 == if drop false else -1 del endif enddef
def ordered? dup dup sort == enddef
def greater? len maxlen > enddef
"unixdict.txt" "r" fopen var f
f -1 !=
while
getword dup if
ordered? if
greater? if
len var maxlen
( ) var words
endif
len maxlen == if
words over 0 put var words
endif
endif
endif
endwhile
f fclose
words print
Another solution
include ..\Utilitys.pmt
0 var f
def getword f fgets dup -1 == if drop false else -1 del endif enddef
def ordered? dup dup sort == enddef
def greater? len rot 1 get len nip rot swap over over > enddef
def append over 0 put enddef
( " " )
"unixdict.txt" "r" fopen var f
f -1 !=
while
getword dup if
ordered? if
greater? if
drop drop flush append
else
== if append endif
endif
swap
endif
endif
endwhile
f fclose
print
- Output:
["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]
Picat
go =>
Dict = "unixdict.txt",
Words = new_map([Word=Word.length : Word in read_file_lines(Dict), Word == Word.sort()]),
MaxLen = max([Len : _Word=Len in Words]),
println([Word : Word=Len in Words, Len=MaxLen].sort).
- Output:
[abbott,accent,accept,access,accost,almost,bellow,billow,biopsy,chilly,choosy,choppy,effort,floppy,glossy,knotty]
PicoLisp
(in "unixdict.txt"
(mapc prinl
(maxi '((L) (length (car L)))
(by length group
(filter '((S) (apply <= S))
(make (while (line) (link @))) ) ) ) ) )
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
PL/I
order: procedure options (main); /* 24/11/2011 */
declare word character (20) varying;
declare word_list character (20) varying controlled;
declare max_length fixed binary;
declare input file;
open file (input) title ('/ORDER.DAT,TYPE(TEXT),RECSIZE(100)');
on endfile (input) go to completed_search;
max_length = 0;
do forever;
get file (input) edit (word) (L);
if length(word) > max_length then
do;
if in_order(word) then
do;
/* Get rid of any stockpiled shorter words. */
do while (allocation(word_list) > 0);
free word_list;
end;
/* Add the eligible word to the stockpile. */
allocate word_list;
word_list = word;
max_length = length(word);
end;
end;
else if max_length = length(word) then
do; /* we have an eligle word of the same (i.e., maximum) length. */
if in_order(word) then
do; /* Add it to the stockpile. */
allocate word_list;
word_list = word;
end;
end;
end;
completed_search:
put skip list ('There are ' || trim(allocation(word_list)) ||
' eligible words of length ' || trim(length(word)) || ':');
do while (allocation(word_list) > 0);
put skip list (word_list);
free word_list;
end;
/* Check that the letters of the word are in non-decreasing order of rank. */
in_order: procedure (word) returns (bit(1));
declare word character (*) varying;
declare i fixed binary;
do i = 1 to length(word)-1;
if substr(word, i, 1) > substr(word, i+1, 1) then return ('0'b);
end;
return ('1'b);
end in_order;
end order;
OUTPUT:
There are 16 eligible words of length 6: knotty glossy floppy effort choppy choosy chilly biopsy billow bellow almost accost access accept accent abbott
PowerShell
$url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
(New-Object System.Net.WebClient).DownloadFile($url, "$env:TEMP\unixdict.txt")
$ordered = Get-Content -Path "$env:TEMP\unixdict.txt" |
ForEach-Object {if (($_.ToCharArray() | Sort-Object) -join '' -eq $_) {$_}} |
Group-Object -Property Length |
Sort-Object -Property Name |
Select-Object -Property @{Name="WordCount" ; Expression={$_.Count}},
@{Name="WordLength"; Expression={[int]$_.Name}},
@{Name="Words" ; Expression={$_.Group}} -Last 1
"There are {0} ordered words of the longest word length ({1} characters):`n`n{2}" -f $ordered.WordCount,
$ordered.WordLength,
($ordered.Words -join ", ")
Remove-Item -Path "$env:TEMP\unixdict.txt" -Force -ErrorAction SilentlyContinue
- Output:
There are 16 ordered words of the longest word length (6 characters): abbott, accent, accept, access, accost, almost, bellow, billow, biopsy, chilly, choosy, choppy, effort, floppy, glossy, knotty
Prolog
Works with SWI-Prolog
:- use_module(library( http/http_open )).
ordered_words :-
% we read the URL of the words
http_open('http://www.puzzlers.org/pub/wordlists/unixdict.txt', In, []),
read_file(In, [], Out),
close(In),
% we get a list of pairs key-value where key = Length and value = <list-of-its-codes>
% this list must be sorted
msort(Out, MOut),
group_pairs_by_key(MOut, POut),
% we sorted this list in decreasing order of the length of values
predsort(my_compare, POut, [_N-V | _OutSort]),
maplist(mwritef, V).
mwritef(V) :-
writef('%s\n', [V]).
read_file(In, L, L1) :-
read_line_to_codes(In, W),
( W == end_of_file ->
% the file is read
L1 = L
;
% we sort the list of codes of the line
% and keep only the "goods word"
( msort(W, W) ->
length(W, N), L2 = [N-W | L], (len = 6 -> writef('%s\n', [W]); true)
;
L2 = L
),
% and we have the pair Key-Value in the result list
read_file(In, L2, L1)).
% predicate for sorting list of pairs Key-Values
% if the lentgh of values is the same
% we sort the keys in alhabetic order
my_compare(R, K1-_V1, K2-_V2) :-
( K1 < K2 -> R = >; K1 > K2 -> R = <; =).
Output :
?- ordered_words. abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty true.
PureBasic
Procedure.s sortLetters(*word.Character, wordLength) ;returns a string with the letters of a word sorted
Protected Dim letters.c(wordLength)
Protected *letAdr = @letters()
CopyMemoryString(*word, @*letAdr)
SortArray(letters(), #PB_Sort_Ascending, 0, wordLength - 1)
ProcedureReturn PeekS(@letters(), wordLength)
EndProcedure
Structure orderedWord
word.s
length.i
EndStructure
Define filename.s = "unixdict.txt", fileNum = 0, word.s
If OpenConsole()
NewList orderedWords.orderedWord()
If ReadFile(fileNum, filename)
While Not Eof(fileNum)
word = ReadString(fileNum)
If word = sortLetters(@word, Len(word))
AddElement(orderedWords())
orderedWords()\word = word
orderedWords()\length = Len(word)
EndIf
Wend
Else
MessageRequester("Error", "Unable to find dictionary '" + filename + "'")
End
EndIf
SortStructuredList(orderedWords(), #PB_Sort_Ascending, OffsetOf(orderedWord\word), #PB_String)
SortStructuredList(orderedWords(), #PB_Sort_Descending, OffsetOf(orderedWord\length), #PB_Integer)
Define maxLength
FirstElement(orderedWords())
maxLength = orderedWords()\length
ForEach orderedWords()
If orderedWords()\length = maxLength
Print(orderedWords()\word + " ")
EndIf
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
Sample output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Python
Python: First Solution
import urllib.request
url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
words = urllib.request.urlopen(url).read().decode("utf-8").split()
ordered = [word for word in words if word==''.join(sorted(word))]
maxlen = len(max(ordered, key=len))
maxorderedwords = [word for word in ordered if len(word) == maxlen]
print(' '.join(maxorderedwords))
Python: Alternate Solution using one explicit loop
import urllib.request
mx, url = 0, 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
for word in urllib.request.urlopen(url).read().decode("utf-8").split():
lenword = len(word)
if lenword >= mx and word==''.join(sorted(word)):
if lenword > mx:
words, mx = [], lenword
words.append(word)
print(' '.join(words))
Sample Output
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Python: As a fold
'''The longest ordered words in a list'''
from functools import reduce
from operator import le
import urllib.request
# longestOrds :: [String] -> [String]
def longestOrds(ws):
'''The longest ordered words in a given list.
'''
return reduce(triage, ws, (0, []))[1]
# triage :: (Int, [String]) -> String -> (Int, [String])
def triage(nxs, w):
'''The maximum length seen for an ordered word,
and the ordered words of this length seen so far.
'''
n, xs = nxs
lng = len(w)
return (
(lng, ([w] if n != lng else xs + [w])) if (
ordered(w)
) else nxs
) if lng >= n else nxs
# ordered :: String -> Bool
def ordered(w):
'''True if the word w is ordered.'''
return all(map(le, w, w[1:]))
# ------------------------- TEST -------------------------
if __name__ == '__main__':
print(
'\n'.join(longestOrds(
urllib.request.urlopen(
'http://wiki.puzzlers.org/pub/wordlists/unixdict.txt'
).read().decode("utf-8").split()
))
)
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Quackery
[ - -1 1 clamp 1+ ]'[ swap peek do ] is <=> ( n n --> )
[ true swap
behead swap witheach
[ tuck > if
[ dip not conclude ] ]
drop ] is ordered ( [ --> b )
[ stack ] is largest ( [ --> s )
[ 1 largest put
[] swap witheach
[ dup size
largest share <=>
[ drop
[ dup ordered iff
[ nested join ]
else drop ]
[ dup ordered iff
[ dup size largest replace
nip nested ]
else drop ] ] ]
largest release ] is task ( [ --> [ )
$ 'unixdict.txt' sharefile drop nest$
task
witheach [ echo$ sp ]
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
R
words = scan("https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt",what = "character")
ordered = logical()
for(i in 1:length(words)){
first = strsplit(words[i],"")[[1]][1:nchar(words[i])-1]
second = strsplit(words[i],"")[[1]][2:nchar(words[i])]
ordered[i] = all(first<=second)
}
cat(words[ordered][which(nchar(words[ordered])==max(nchar(words[ordered])))],sep="\n")
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Racket
#lang racket
(require net/url)
(define dict "http://www.puzzlers.org/pub/wordlists/unixdict.txt")
(define (ordered? str)
(define lower (string-downcase str))
(for/and ([i (in-range 1 (string-length str))])
(char<=? (string-ref lower (sub1 i)) (string-ref lower i))))
(define words (port->lines (get-pure-port (string->url dict))))
(let loop ([len 0] [longs '()] [words words])
(if (null? words)
(for-each displayln (reverse longs))
(let* ([word (car words)] [words (cdr words)]
[wlen (string-length word)])
(if (or (< wlen len) (not (ordered? word)))
(loop len longs words)
(loop wlen (cons word (if (> wlen len) '() longs)) words)))))
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Raku
(formerly Perl 6)
Here we assume the dictionary is provided on standard input.
say lines.grep({ [le] .comb }).classify(*.chars).max(*.key).value
- Output:
[abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty]
Red
Red []
;; code to read url and save to local file:
;;data: read/binary http://www.puzzlers.org/pub/wordlists/unixdict.txt
;;write %unixdict.txt data
max: [ "" ] ;; init array with one empty string (length 0 )
foreach word read/lines %unixdict.txt [ ;; read local file
len: either word = sort copy word [ length? word ] [ -1 ] ;; check if ordered and get length
case [
len > length? first max [ max: reduce [ word ]] ;; init new block
len = length? first max [ append max word ]
]
]
probe max
Sample Output
["abbott" "accent" "accept" "access" "accost" "almost" "bellow" "billow" "biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty"]
REXX
This problem assumes (or implies) an order of letter case, but fortunately, there're no uppercase letters
in the (default) dictionary to see which programs would fail when not recognizing this distinction.
In ASCII
, A is less than a, while in EBCDIC
, it's the other way around.
The problem can be avoided by first converting the word to a specific case, and then test for ordered
letters in that word (as indeed, this REXX entry does).
Output could be improved by sorting the words in the dictionary, but adding a sort would bulk up the program;
but since the word list (in this case) is already in alphabetical order, this would-be improvement is
mostly moot.
/*REXX program lists (the longest) ordered word(s) from a supplied dictionary. */
iFID= 'UNIXDICT.TXT' /*the filename of the word dictionary. */
m= 1 /*maximum length of an ordered word(s).*/
call linein iFID, 1, 0 /*point to the first word in dictionary*/
@.= /*placeholder array for list of words. */
do j=1 while lines(iFID)\==0; x=linein(iFID) /*keep reading until file is exhausted.*/
w= length(x); if w<m then iterate /*Word not long enough? Then ignore it.*/
if \datatype(x, 'M') then iterate /*Is it not a letter? Then ignore it. */
parse upper var x xU 1 z 2 /*get uppercase version of X & 1st char*/
do k=2 for w-1; _= substr(xU, k, 1) /*process each letter in uppercase word*/
if _<z then iterate j /*is letter < than the previous letter?*/
z= _ /*we have a newer current letter. */
end /*k*/ /* [↑] logic includes ≥ order. */
m= w /*maybe define a new maximum length. */
@.w= @.w x /*add the original word to a word list.*/
end /*j*/ /*the 1st DO needs an index for ITERATE*/
#= words(@.m) /*just a handy─dandy variable to have. */
say # 'word's(#) "found (of length" m')'; say /*show the number of words and length. */
do n=1 for #; say word(@.m, n); end /*display all the words, one to a line.*/
exit /*stick a fork in it, we're all done. */
ghijk
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer (merely adds "S")*/
- output when using the default supplied word dictionary:
16 words found (of length 6) abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Ring
load "stdlib.ring"
cStr = read("unixdict.txt")
wordList = str2list(cStr)
sum = 0
sortList = []
see "working..." + nl + nl
for n = 1 to len(wordList)
num = 0
len = len(wordList[n])-1
for m = 1 to len
asc1 = ascii(wordList[n][m])
asc2 = ascii(wordList[n][m+1])
if asc1 <= asc2
num = num + 1
ok
next
if num = len
sum = sum + 1
add(sortList,[wordList[n],len])
ok
next
sortList = sort(sortList,2)
sortList = reverse(sortList)
endList = []
len = sortList[1][2]
for n = 1 to len(sortList)
if sortList[n][2] = len
add(endList,sortList[n][1])
else
exit
ok
next
endList = sort(endList)
see endList
see nl + "done..." + nl
Output:
working... abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty done...
RPL
The only way to use unixdict.txt
as input is to download it locally and to convert it into a list of 25104 strings. Fortunately, emulators can handle such a big data structure in RAM.
≪ "a" DUP 1 4 PICK SIZE FOR j SWAP DROP OVER j DUP SUB IF DUP2 > THEN 99 'j' STO END NEXT ≤ SWAP DROP ≫ 'ORDWORD?' STO ≪ 0 → words sizemax ≪ { } 1 words EVAL SIZE FOR j words j GET DUP SIZE IF DUP sizemax ≥ THEN IF OVER ORDWORD? THEN @ 2-step test, rather than one with an AND, to save execution time IF DUP sizemax > THEN 'sizemax' STO SWAP DROP { } SWAP ELSE DROP END + ELSE DROP2 END ELSE DROP2 END NEXT ≫ ≫ 'ORDWORDS' STO
'Unixdict' ORDWORDS
- Output:
1: { abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty }
Ruby
require 'open-uri'
ordered_words = open('http://www.puzzlers.org/pub/wordlists/unixdict.txt', 'r').select do |word|
word.strip!
word.chars.sort.join == word
end
grouped = ordered_words.group_by &:size
puts grouped[grouped.keys.max]
Sample Output
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Local version:
words = IO.foreach('unixdict.txt').map(&:chomp).select {|word| word.chars.sort.join == word}
puts words.group_by(&:size).sort_by(&:first).last.last
Run BASIC
a$ = httpget$("http://www.puzzlers.org/pub/wordlists/unixdict.txt")
j = 1
i = instr(a$,chr$(10),j)
while i <> 0
a1$ = mid$(a$,j,i-j)
for k = 1 to len(a1$) - 1
if mid$(a1$,k,1) > mid$(a1$,k+1,1) then goto [noWay]
next k
maxL = max(maxL,len(a1$))
if len(a1$) >= maxL then a2$ = a2$ + a1$ + "||"
[noWay]
j = i + 1
i = instr(a$,chr$(10),j)
wend
n = 1
while word$(a2$,n,"||") <> ""
a3$ = word$(a2$,n,"||")
if len(a3$) = maxL then print a3$
n = n + 1
wend
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Rust
const FILE: &'static str = include_str!("./unixdict.txt");
fn is_ordered(s: &str) -> bool {
let mut prev = '\x00';
for c in s.to_lowercase().chars() {
if c < prev {
return false;
}
prev = c;
}
return true;
}
fn find_longest_ordered_words(dict: Vec<&str>) -> Vec<&str> {
let mut result = Vec::new();
let mut longest_length = 0;
for s in dict.into_iter() {
if is_ordered(&s) {
let n = s.len();
if n > longest_length {
longest_length = n;
result.truncate(0);
}
if n == longest_length {
result.push(s);
}
}
}
return result;
}
fn main() {
let lines = FILE.lines().collect();
let longest_ordered = find_longest_ordered_words(lines);
for s in longest_ordered.iter() {
println!("{}", s.to_string());
}
}
Scala
val wordsAll = scala.io.Source.fromURL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").getLines.toSeq
/**
* Given a sequence of words return a sub-sequence of the
* words that have characters in sorted order.
*/
def orderedWords( words:Seq[String] ) : Seq[(String)] = {
def isOrdered( s:String ) : Boolean =
(s.foldLeft( (true,'@') ){
case ((false,_),_) => return false
case ((true,prev),c) => ((prev <= c),c)
})._1
wordsAll.filter( isOrdered(_) ).toSeq
}
val ww = orderedWords( wordsAll ).sortBy( -_.length )
println( ww.takeWhile( _.length == ww.head.length ).mkString("\n") )
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Scheme
The following implementation uses a char>=? procedure that accepts an arbitrary number of arguments. This is allowed, but not required, by R5RS, and is provided by many Scheme implementations. It has been tested under GNU Guile 1.8.8.
(define sorted-words
(let ((port (open-input-file "unixdict.txt")))
(let loop ((char (read-char port)) (word '()) (result '(())))
(cond
((eof-object? char)
(reverse (map (lambda (word) (apply string word)) result)))
((eq? #\newline char)
(loop (read-char port) '()
(let ((best-length (length (car result))) (word-length (length word)))
(cond
((or (< word-length best-length) (not (apply char>=? word))) result)
((> word-length best-length) (list (reverse word)))
(else (cons (reverse word) result))))))
(else (loop (read-char port) (cons char word) result))))))
(map (lambda (x)
(begin
(display x)
(newline)))
sorted-words)
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Seed7
$ include "seed7_05.s7i";
const func boolean: isOrdered (in string: word) is func
result
var boolean: ordered is TRUE;
local
var integer: index is 0;
begin
for index range 1 to pred(length(word)) do
if word[index] > word[succ(index)] then
ordered := FALSE;
end if;
end for;
end func;
const proc: write (in array string: wordList) is func
local
var string: word is "";
begin
for word range wordList do
writeln(word);
end for;
end func;
const proc: main is func
local
var file: dictionary is STD_NULL;
var string: word is "";
var integer: length is 0;
var array string: wordList is 0 times "";
begin
dictionary := open("unixdict.txt", "r");
if dictionary <> STD_NULL then
readln(dictionary, word);
while not eof(dictionary) do
if isOrdered(lower(word)) then
if length(word) > length then
length := length(word);
wordList := [] (word);
elsif length(word) = length then
wordList &:= word;
end if;
end if;
readln(dictionary, word);
end while;
close(dictionary);
end if;
write(wordList);
end func;
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Sidef
var words = [[]]
var file = %f'unixdict.txt'
file.open_r(\var fh, \var err) ->
|| die "Can't open file #{file}: $#{err}"
fh.each { |line|
line.trim!
if (line == line.sort) {
words[line.length] := [] << line
}
}
say words[-1].join(' ')
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Simula
BEGIN
BOOLEAN PROCEDURE ISORDERED(W); TEXT W;
BEGIN
BOOLEAN B;
B := TRUE;
W.SETPOS(1);
IF W.MORE THEN
BEGIN
CHARACTER CURR, LAST;
CURR := W.GETCHAR;
WHILE W.MORE AND B DO
BEGIN
LAST := CURR;
CURR := W.GETCHAR;
B := LAST <= CURR;
END;
END;
ISORDERED := B;
END;
REF (INFILE) INF;
INTEGER LONGEST;
TEXT W;
COMMENT FIND LONGEST LENGTH;
INF :- NEW INFILE("unixdict.txt");
INF.OPEN(BLANKS(132));
WHILE NOT INF.LASTITEM DO
BEGIN
W :- COPY(INF.IMAGE).STRIP;
IF ISORDERED(W) THEN
IF W.LENGTH > LONGEST THEN
LONGEST := W.LENGTH;
INF.INIMAGE;
END;
INF.CLOSE;
COMMENT OUTPUT ORDRERED WORDS OF LONGEST LENGTH;
INF :- NEW INFILE("unixdict.txt");
INF.OPEN(BLANKS(132));
WHILE NOT INF.LASTITEM DO
BEGIN
W :- COPY(INF.IMAGE).STRIP;
IF W.LENGTH = LONGEST AND THEN ISORDERED(W) THEN
BEGIN
OUTTEXT(W);
OUTIMAGE;
END;
INF.INIMAGE;
END;
INF.CLOSE;
END.
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty 14 garbage collection(s) in 0.0 seconds.
Smalltalk
|file dict r t|
file := FileStream open: 'unixdict.txt' mode: FileStream read.
dict := Set new.
"load the whole dict into the set before, 'filter' later"
[ file atEnd ] whileFalse: [
dict add: (file upTo: Character nl) ].
"find those with the sorted letters, and sort them by length"
r := ((dict
select: [ :w | (w asOrderedCollection sort) = (w asOrderedCollection) ] )
asSortedCollection: [:a :b| (a size) > (b size) ] ).
"get those that have length = to the max length, and sort alphabetically"
r := (r select: [:w| (w size) = ((r at: 1) size)]) asSortedCollection.
r do: [:e| e displayNl].
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
SPL
words = #.split(#.readtext("unixdict.txt","ascii"),#.lf)
max = 0
> i, 1..#.size(words,1)
word = words[i]
wordb = #.array(word)
wordbc = #.size(wordb,1)
> j, 3..wordbc,2
<< wordb[j]<wordb[j-2]
<
>> j!>wordbc|wordbc<max
? wordbc>max, result = ""
? wordbc>max, max = wordbc
result += word+#.crlf
<
#.output(result)
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Standard ML
fun isOrdered s =
let
fun loop (i, c) =
let
val c' = String.sub (s, i)
in
c <= c' andalso loop (i + 1, c')
end
handle Subscript => true
in
loop (0, #"\^@")
end
fun longestOrdereds (s, prev as (len, lst)) =
let
val len' = size s
in
if len' >= len andalso isOrdered s then
if len' = len then (len, s :: lst) else (len', [s])
else
prev
end
val () = print ((String.concatWith " "
o #2
o foldr longestOrdereds (0, [])
o String.tokens Char.isSpace
o TextIO.inputAll) TextIO.stdIn ^ "\n")
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Swift
import Foundation
guard
let url = NSURL(string: "http://www.puzzlers.org/pub/wordlists/unixdict.txt"),
let input = try? NSString(contentsOfURL: url,encoding: NSUTF8StringEncoding) as String
else { exit(EXIT_FAILURE) }
let words = input.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
let group: ([Int: [String]], String) -> [Int: [String]] = {
var d = $0; let g = d[$1.characters.count] ?? []
d[$1.characters.count] = g + [$1]
return d
}
let ordered: ([String], String) -> [String] = {
guard String($1.characters.sort()) == $1 else { return $0 }
return $0 + [$1]
}
let groups = words
.reduce([String](), combine: ordered)
.reduce([Int: [String]](), combine: group)
guard
let maxLength = groups.keys.maxElement(),
let maxLengthGroup = groups[maxLength]
else { exit(EXIT_FAILURE) }
maxLengthGroup.forEach { print($0) }
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Tcl
package require http
# Pick the ordered words (of maximal length) from a list
proc chooseOrderedWords list {
set len 0
foreach word $list {
# Condition to determine whether a word is ordered; are its characters
# in sorted order?
if {$word eq [join [lsort [split $word ""]] ""]} {
if {[string length $word] > $len} {
set len [string length $word]
set orderedOfMaxLen {}
}
if {[string length $word] == $len} {
lappend orderedOfMaxLen $word
}
}
}
return $orderedOfMaxLen
}
# Get the dictionary and print the ordered words from it
set t [http::geturl "http://www.puzzlers.org/pub/wordlists/unixdict.txt"]
puts [chooseOrderedWords [http::data $t]]
http::cleanup $t
Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Transd
#lang transd
MainModule: {
_start: (lambda
(with fs FileStream() len 0 maxlen 0 words Vector<String>()
(open-r fs "/mnt/vault/tmp/unixdict.txt") )
(for w in (read-lines fs) do
(= len (size w))
(if (< len maxlen) continue)
(if (is-sorted w)
(if (< maxlen len)
(clear words) (= maxlen len))
(append words w)
))
(lout words)
))
}
- Output:
["abbott", "accent", "accept", "access", "accost", "almost", "bellow", "billow", "biopsy", "chilly", "choosy", "choppy", "effort", "floppy", "glossy", "knotty"]
TUSCRIPT
$$ MODE TUSCRIPT
SET data = REQUEST ("http://www.puzzlers.org/pub/wordlists/unixdict.txt")
DICT orderdwords CREATE 99999
COMPILE
LOOP word=data
- "<%" = any token
SET letters=STRINGS (word,":<%:")
SET wordsignatur= ALPHA_SORT (letters)
IF (wordsignatur==letters) THEN
SET wordlength=LENGTH (word)
DICT orderdwords ADD/COUNT word,num,cnt,wordlength
ENDIF
ENDLOOP
DICT orderdwords UNLOAD words,num,cnt,wordlength
SET maxlength=MAX_LENGTH (words)
SET rtable=QUOTES (maxlength)
BUILD R_TABLE maxlength = rtable
SET index=FILTER_INDEX (wordlength,maxlength,-)
SET longestwords=SELECT (words,#index)
PRINT num," ordered words - max length is ",maxlength,":"
LOOP n,w=longestwords
SET n=CONCAT (n,"."), n=CENTER(n,4)
PRINT n,w
ENDLOOP
ENDCOMPILE
Output:
422 ordered words - max length is 6: 1. abbott 2. accent 3. accept 4. access 5. accost 6. almost 7. bellow 8. billow 9. biopsy 10. chilly 11. choosy 12. choppy 13. effort 14. floppy 15. glossy 16. knotty
Ursala
#import std
#show+
main = leql@bh$^ eql|= (ordered lleq)*~ unixdict_dot_txt
leql
is a binary predicate testing a pair of lists for less or equal lengtheql
tests for equal lengthlleq
tests for lexically less or equal relatedness between characters or stringsordered
takes a binary predicate to a predicate operating on a list, which tests whether every pair of consecutive items is related by the binary predicate- postfix operator
*~
turns a predicate into a list filtering function, deleting any item not satisfying it - postfix operator
|=
turns an equivalance relational predicate into function that partitions a list into a list of equivalence classes - postfix operator
$^
turns a predicate into a function that searches a list for the maximum member with respect to it - postfix operator
@bh
makes its operand function apply to the pair of heads of a pair of lists - loading a file
unixdict.txt
into a list of strings is implied by using its name as an identifier and supplying it to the compiler in a command line parameter
output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
VBA
Public Sub orderedwords(fname As String)
' find ordered words in dict file that have the longest word length
' fname is the name of the input file
' the words are printed in the immediate window
' this subroutine uses boolean function IsOrdered
Dim word As String 'word to be tested
Dim l As Integer 'length of word
Dim wordlength As Integer 'current longest word length
Dim orderedword() As String 'dynamic array holding the ordered words with the current longest word length
Dim wordsfound As Integer 'length of the array orderedword()
On Error GoTo NotFound 'catch incorrect/missing file name
Open fname For Input As #1
On Error GoTo 0
'initialize
wordsfound = 0
wordlength = 0
'process file line per line
While Not EOF(1)
Line Input #1, word
If IsOrdered(word) Then 'found one, is it equal to or longer than current word length?
l = Len(word)
If l >= wordlength Then 'yes, so add to list or start a new list
If l > wordlength Then 'it's longer, we must start a new list
wordsfound = 1
wordlength = l
Else 'equal length, increase the list size
wordsfound = wordsfound + 1
End If
'add the word to the list
ReDim Preserve orderedword(wordsfound)
orderedword(wordsfound) = word
End If
End If
Wend
Close #1
'print the list
Debug.Print "Found"; wordsfound; "ordered words of length"; wordlength
For i = 1 To wordsfound
Debug.Print orderedword(i)
Next
Exit Sub
NotFound:
debug.print "Error: Cannot find or open file """ & fname & """!"
End Sub
Public Function IsOrdered(someWord As String) As Boolean
'true if letters in word are in ascending (ascii) sequence
Dim l As Integer 'length of someWord
Dim wordLcase As String 'the word in lower case
Dim ascStart As Integer 'ascii code of first char
Dim asc2 As Integer 'ascii code of next char
wordLcase = LCase(someWord) 'convert to lower case
l = Len(someWord)
IsOrdered = True
If l > 0 Then 'this skips empty string - it is considered ordered...
ascStart = Asc(Left$(wordLcase, 1))
For i = 2 To l
asc2 = Asc(Mid$(wordLcase, i, 1))
If asc2 < ascStart Then 'failure!
IsOrdered = False
Exit Function
End If
ascStart = asc2
Next i
End If
End Function
Results:
OrderedWords("unixdict.txt") Found 16 ordered words of length 6 abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
VBScript
Note: The input file is in the same folder as the script.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set infile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\" &_
"unixdict.txt",1)
list = ""
length = 0
Do Until inFile.AtEndOfStream
line = infile.ReadLine
If IsOrdered(line) Then
If Len(line) > length Then
length = Len(line)
list = line & vbCrLf
ElseIf Len(line) = length Then
list = list & line & vbCrLf
End If
End If
Loop
WScript.StdOut.Write list
Function IsOrdered(word)
IsOrdered = True
prev_val = 0
For i = 1 To Len(word)
If i = 1 Then
prev_val = Asc(Mid(word,i,1))
ElseIf Asc(Mid(word,i,1)) >= prev_val Then
prev_val = Asc(Mid(word,i,1))
Else
IsOrdered = False
Exit For
End If
Next
End Function
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
Vedit macro language
File_Open("unixdict.txt", BROWSE)
#1 = 2 // length of longest word found
Repeat (ALL) {
#2 = EOL_Pos-Cur_Pos // length of this word
if (#2 >= #1) {
#3 = 1 // flag: is ordered word
Char(1)
While (!At_EOL) {
if (Cur_Char < Cur_Char(-1)) {
#3 = 0 // not an ordered word
break
}
Char(1)
}
if (#3) { // ordered word found
if (#2 > #1) { // new longer word found
#1 = #2
Reg_Empty(10) // clear list
}
BOL Reg_Copy(10,1,APPEND) // add word to list
}
}
Line(1,ERRBREAK) // next word
}
Buf_Quit(OK) // close file
Reg_Type(10) // display results
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
V (Vlang)
fn main() {
words := os.read_file("./unixdict.txt") or {println(err) exit(-1)}.split_into_lines()
mut longest_len := 0
mut longest := []string{}
mut u_word := []u8{}
for word in words {
for chars in word {u_word << chars}
u_word.sort()
if word.len > longest_len {
if word == u_word.bytestr() {
longest_len = word.len
longest.clear()
longest << word
}
}
else if word.len == longest_len {
if word == u_word.bytestr() {longest << word}
}
u_word.clear()
}
println("The ${longest.len} ordered words with the longest length (${longest_len}) are:")
print(longest.join("\n"))
}
- Output:
The 16 ordered words with the longest length (6) are: abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knott
Wren
import "io" for File
import "./sort" for Sort
var words = File.read("unixdict.txt").split("\n")
var longestLen = 0
var longest = []
for (word in words) {
if (word.count > longestLen) {
if (Sort.isSorted(word.toList)) {
longestLen = word.count
longest.clear()
longest.add(word)
}
} else if (word.count == longestLen) {
if (Sort.isSorted(word.toList)) longest.add(word)
}
}
System.print("The %(longest.count) ordered words with the longest length (%(longestLen)) are:")
System.print(longest.join("\n"))
- Output:
The 16 ordered words with the longest length (6) are: abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
XPL0
string 0; \use zero-terminated strings
int MaxLen, Pass, I, Ch, Ch0;
char Word(25);
def LF=$0A, CR=$0D, EOF=$1A;
[FSet(FOpen("unixdict.txt", 0), ^I);
MaxLen:= 0;
for Pass:= 1 to 2 do
[OpenI(3);
repeat I:= 0; Ch0:= 0;
loop [repeat Ch:= ChIn(3) until Ch # CR; \remove possible CR
if Ch=LF or Ch=EOF then
[if I > MaxLen then MaxLen:= I;
if I=MaxLen & Pass=2 then
[Word(I):= 0; Text(0, Word); CrLf(0)];
quit;
];
Word(I):= Ch;
if Ch < Ch0 then
[repeat Ch:= ChIn(3) until Ch=LF or Ch=EOF;
quit;
];
Ch0:= Ch;
I:= I+1;
];
until Ch = EOF;
];
]
- Output:
abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
zkl
One pass, dictionary stays on disk:
var words=L(), sz=0; // some state
fcn isLex(word){ word.reduce(fcn(p,c){ (p<=c) and c or T(Void.Stop,False) }) }
File("dict.txt").pump(Void,fcn(w){
w=w.strip(); // get rid of newline
if(isLex(w)){ n:=w.len();
if(n>sz){ words.clear(w); sz=n }
else if(n==sz) words.append(w)
}
})
println("Num words: %d, all size %d\n".fmt(words.len(),sz));
words.pump(Console.println);
Or, reading dictionary into memory:
fcn isLex(word){ word.reduce(fcn(p,c){ (p<=c) and c or T(Void.Stop,False) }) }
lwords:=File("dict.txt").readln(*).apply("strip").filter(isLex);
max:=lwords.reduce(fcn(n,w){ w.len()>n and w.len() or n },0);
lwords=lwords.filter(fcn(w,n){ w.len()==n },max);
println("Num words: %d, all size %d\n".fmt(lwords.len(),max));
words.pump(Console.println);
- Output:
Num words: 16, all size 6 abbott accent accept access accost almost bellow billow biopsy chilly choosy choppy effort floppy glossy knotty
- Programming Tasks
- Text processing
- 11l
- Acornsoft Lisp
- Action!
- Ada
- Aime
- ALGOL 68
- Amazing Hopper
- APL
- AppleScript
- Arturo
- AutoHotkey
- AWK
- BaCon
- BBC BASIC
- Befunge
- BQN
- Bracmat
- Burlesque
- C
- C sharp
- C++
- Clojure
- CLU
- COBOL
- CoffeeScript
- Common Lisp
- Cowgol
- D
- Delphi
- Draco
- E
- EchoLisp
- Elixir
- Erlang
- Euphoria
- F Sharp
- Factor
- Fantom
- FBSL
- Forth
- Fortran
- FreeBASIC
- Frink
- FutureBasic
- Gambas
- Go
- Groovy
- Haskell
- Huginn
- Icon
- Unicon
- Icon Programming Library
- Io
- J
- Java
- JavaScript
- Jq
- Julia
- K
- Kotlin
- Lambdatalk
- Lang5
- Lasso
- Liberty BASIC
- Lingo
- Lua
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Nanoquery
- NetRexx
- Nim
- OCaml
- Oforth
- OoRexx
- PARI/GP
- Perl
- Phix
- Phixmonti
- Picat
- PicoLisp
- PL/I
- PowerShell
- Prolog
- PureBasic
- Python
- Quackery
- R
- Racket
- Raku
- Red
- REXX
- Ring
- RPL
- Ruby
- Run BASIC
- Rust
- Scala
- Scheme
- Seed7
- Sidef
- Simula
- Smalltalk
- SPL
- Standard ML
- Swift
- Tcl
- Transd
- TUSCRIPT
- Ursala
- VBA
- VBScript
- Vedit macro language
- V (Vlang)
- Wren
- Wren-sort
- XPL0
- Zkl
- 6502 Assembly/Omit
- 8080 Assembly/Omit
- Z80 Assembly/Omit