Ordered words: Difference between revisions

m
Fix functional CL dict path
mNo edit summary
imported>GoulashAmateur
m (Fix functional CL dict path)
 
(16 intermediate revisions by 8 users not shown)
Line 33:
<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>
 
Line 280 ⟶ 325:
glossy
knotty</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#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
 
</syntaxhighlight>
{{out}}
<pre>
abbott
accent
accept
access
accost
almost
bellow
billow
biopsy
chilly
choosy
choppy
effort
floppy
glossy
knotty
</pre>
 
=={{header|APL}}==
Line 1,250 ⟶ 1,352:
"biopsy" "chilly" "choosy" "choppy" "effort" "floppy" "glossy" "knotty")
</syntaxhighlight>
 
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}}==
Line 2,193 ⟶ 2,307:
=={{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" )
Line 2,617 ⟶ 2,733:
glossy
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}}==
Line 2,735 ⟶ 2,884:
=={{header|Kotlin}}==
 
<syntaxhighlight lang="scalakotlin">import java.ionet.FileURI
 
fun main(args: Array<String>) {
val fileurl = FileURI("unixdict.txt")
"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) {
result += 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)
 
}
result.sortByDescending { it.length }
</syntaxhighlight>
val max = result[0].length
 
for (word in result) {
if (word.length == max) {
println(word)
}
}
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
Line 4,068 ⟶ 4,213:
 
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>
 
Line 4,993 ⟶ 5,172:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./sort" for Sort
 
var words = File.read("unixdict.txt").split("\n")