2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,17 @@
|
|||
Define an ordered word as a word in which the letters of the word appear in alphabetic order. Examples include 'abbey' and 'dirt'.
|
||||
An ''ordered word'' is a word in which the letters appear in alphabetic order.
|
||||
|
||||
The task is to find ''and display'' all the ordered words in this [http://www.puzzlers.org/pub/wordlists/unixdict.txt dictionary] 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.
|
||||
Examples include '''abbey''' and '''dirt'''.
|
||||
|
||||
{{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.
|
||||
|
||||
(Examples that access the dictionary file locally assume that you have downloaded this file yourself.)
|
||||
|
||||
The display needs to be shown on this page.
|
||||
|
||||
{{task heading|Related tasks}}
|
||||
|
||||
{{Related tasks/Word plays}}
|
||||
|
||||
<hr>
|
||||
|
|
|
|||
83
Task/Ordered-words/ALGOL-68/ordered-words.alg
Normal file
83
Task/Ordered-words/ALGOL-68/ordered-words.alg
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
# find the longrst words in a list that have all letters in order #
|
||||
# use the associative array in the Associate array/iteration task #
|
||||
PR read "aArray.a68" PR
|
||||
|
||||
# returns the length of word #
|
||||
PROC length = ( STRING word )INT: 1 + ( UPB word - LWB word );
|
||||
|
||||
# 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
|
||||
# failed to open the file #
|
||||
print( ( "Unable to open """ + file name + """", newline ) )
|
||||
ELSE
|
||||
# file opened OK #
|
||||
BOOL at eof := FALSE;
|
||||
# set the EOF handler for the file #
|
||||
on logical file end( input file, ( REF FILE f )BOOL:
|
||||
BEGIN
|
||||
# note that we reached EOF on the #
|
||||
# latest read #
|
||||
at eof := TRUE;
|
||||
# return TRUE so processing can continue #
|
||||
TRUE
|
||||
END
|
||||
);
|
||||
# store the ordered words and find the longest #
|
||||
INT max length := 0;
|
||||
REF AARRAY words := INIT LOC AARRAY;
|
||||
STRING word;
|
||||
WHILE NOT at eof
|
||||
DO
|
||||
STRING word;
|
||||
get( input file, ( word, newline ) );
|
||||
IF char sort( word ) = word
|
||||
THEN
|
||||
# have an ordered word #
|
||||
IF INT word length := length( word );
|
||||
word length > max length
|
||||
THEN
|
||||
max length := word length
|
||||
FI;
|
||||
# store the word #
|
||||
words // word := ""
|
||||
FI
|
||||
OD;
|
||||
# close the file #
|
||||
close( input file );
|
||||
|
||||
print( ( "Maximum length of ordered words: ", whole( max length, -4 ), newline ) );
|
||||
# show the ordered words with the maximum length #
|
||||
REF AAELEMENT e := FIRST words;
|
||||
WHILE e ISNT nil element DO
|
||||
IF max length = length( key OF e )
|
||||
THEN
|
||||
print( ( key OF e, newline ) )
|
||||
FI;
|
||||
e := NEXT words
|
||||
OD
|
||||
FI
|
||||
17
Task/Ordered-words/Io/ordered-words.io
Normal file
17
Task/Ordered-words/Io/ordered-words.io
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
21
Task/Ordered-words/Kotlin/ordered-words.kotlin
Normal file
21
Task/Ordered-words/Kotlin/ordered-words.kotlin
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import java.io.File
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val file = File("unixdict.txt")
|
||||
val result = mutableListOf<String>()
|
||||
|
||||
file.forEachLine {
|
||||
if (it.toCharArray().sorted().joinToString(separator = "") == it) {
|
||||
result += it
|
||||
}
|
||||
}
|
||||
|
||||
result.sortByDescending { it.length }
|
||||
val max = result[0].length
|
||||
|
||||
for (word in result) {
|
||||
if (word.length == max) {
|
||||
println(word)
|
||||
}
|
||||
}
|
||||
}
|
||||
4
Task/Ordered-words/PARI-GP/ordered-words.pari
Normal file
4
Task/Ordered-words/PARI-GP/ordered-words.pari
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
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)
|
||||
|
|
@ -1 +1 @@
|
|||
say .value given max :by(*.key), classify *.chars, grep { [le] .comb }, lines;
|
||||
say lines.grep({ [le] .comb }).classify(*.chars).max(*.key).value
|
||||
|
|
|
|||
16
Task/Ordered-words/PowerShell/ordered-words.psh
Normal file
16
Task/Ordered-words/PowerShell/ordered-words.psh
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
$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
|
||||
|
|
@ -25,10 +25,13 @@ If OpenConsole()
|
|||
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_Sort_String)
|
||||
SortStructuredList(orderedWords(), #PB_Sort_Descending, OffsetOf(orderedWord\length), #PB_Sort_integer)
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,27 +1,24 @@
|
|||
/*REXX program lists (the longest) ordered word(s) from a supplied dictionary.*/
|
||||
iFID = 'UNIXDICT.TXT' /*the filename of the word dictionary. */
|
||||
@.= /*placeholder array for list of words. */
|
||||
mL=0 /*maximum length of the ordered words. */
|
||||
call linein iFID, 1, 0 /*point to the first word in dictionary*/
|
||||
/* [↑] just in case the file is open. */
|
||||
do j=1 while lines(iFID)\==0 /*keep reading until file is exhausted.*/
|
||||
x=linein(iFID); w=length(x) /*obtain a word and also its length. */
|
||||
if w<mL then iterate /*Word not long enough? Then ignore it.*/
|
||||
xU=x; upper xU /*create uppercase version of word X. */
|
||||
z=left(xU,1) /*now, determine if the word is ordered*/
|
||||
/*handle words of mixed case. */
|
||||
do k=2 to w; _=substr(xU,k,1) /*process each letter in word. */
|
||||
if \datatype(_,'U') then iterate /*Not a letter? Then ignore it.*/
|
||||
if _<z then iterate j /*is letter < than previous ? */
|
||||
z=_ /*we have newer current letter. */
|
||||
end /*k*/ /* [↑] logic includes ≥ order. */
|
||||
mL=w /*maybe define a new maximum length. */
|
||||
@.w=@.w x /*add the original word to a word list.*/
|
||||
end /*j*/
|
||||
|
||||
#=words(@.mL) /*just a handy─dandy variable to have. */
|
||||
say # 'word's(#) "found (of length" mL')'; say /*show # words & length.*/
|
||||
do n=1 for #; say word(@.mL,n); end /*list all the words. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
s: if arg(1)==1 then return ''; return 's' /*a simple pluralizer.*/
|
||||
/*REXX program lists (the longest) ordered word(s) from a supplied dictionary. */
|
||||
iFID= 'UNIXDICT.TXT' /*the filename of the word dictionary. */
|
||||
@.= /*placeholder array for list of words. */
|
||||
mL=0 /*maximum length of the ordered words. */
|
||||
call linein iFID, 1, 0 /*point to the first word in dictionary*/
|
||||
/* [↑] just in case the file is open. */
|
||||
do j=1 while lines(iFID)\==0; x=linein(iFID) /*keep reading until file is exhausted.*/
|
||||
w=length(x); if w<mL then iterate /*Word not long enough? Then ignore it.*/
|
||||
parse upper var x xU 1 z 2 /*get uppercase version of X & 1st char*/
|
||||
/* [↓] handle words of mixed case. */
|
||||
do k=2 to w; _=substr(xU, k, 1) /*process each letter in uppercase word*/
|
||||
if \datatype(_, 'U') then iterate /*Is it not a letter? Then ignore it. */
|
||||
if _<z then iterate j /*is letter < than the previous letter?*/
|
||||
z=_ /*we have a newer current letter. */
|
||||
end /*k*/ /* [↑] logic includes ≥ order. */
|
||||
mL=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(@.mL) /*just a handy─dandy variable to have. */
|
||||
say # 'word's(#) "found (of length" mL')'; say /*show the number of words and length. */
|
||||
do n=1 for #; say word(@.mL, n); end /*display all the words, one to a line.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer (merely adds "S")*/
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader,BufRead};
|
||||
const FILE: &'static str = include_str!("./unixdict.txt");
|
||||
|
||||
fn is_ordered(s: &str) -> bool {
|
||||
let mut prev = '\x00';
|
||||
for c in s.chars() {
|
||||
for c in s.to_lowercase().chars() {
|
||||
if c < prev {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -13,7 +12,7 @@ fn is_ordered(s: &str) -> bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
fn find_longest_ordered_words(dict: Vec<String>) -> Vec<String> {
|
||||
fn find_longest_ordered_words(dict: Vec<&str>) -> Vec<&str> {
|
||||
let mut result = Vec::new();
|
||||
let mut longest_length = 0;
|
||||
|
||||
|
|
@ -25,7 +24,7 @@ fn find_longest_ordered_words(dict: Vec<String>) -> Vec<String> {
|
|||
result.truncate(0);
|
||||
}
|
||||
if n == longest_length {
|
||||
result.push(s.to_owned());
|
||||
result.push(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -34,7 +33,7 @@ fn find_longest_ordered_words(dict: Vec<String>) -> Vec<String> {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let lines = BufReader::new(File::open("unixdict.txt").unwrap()).lines().map(|l|l.unwrap()).collect();
|
||||
let lines = FILE.lines().collect();
|
||||
|
||||
let longest_ordered = find_longest_ordered_words(lines);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue