Odd words: Difference between revisions
m
syntax highlighting fixup automation
(J: see talk page) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12:
{{trans|Python}}
<
Set[String] oddWordSet
Line 21:
L(i) sorted(Array(oddWordSet))
print(i)</
{{out}}
Line 41:
=={{header|Ada}}==
Using instances of generic procedure to filter odd/even words.
<
with Ada.Containers.Indefinite_Ordered_Maps;
Line 110:
Put_Line ("Even words:");
Put_Even_Words;
end Odd_Words;</
{{out}}
<pre>Odd words:
Line 138:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Based on (and almost identical to) the Alternade Words sample.
<
# use the associative array in the Associate array/iteration task #
PR read "aArray.a68" PR
Line 202:
e := NEXT words
OD
FI</
{{out}}
As with the Alternade Words Algol 68 sample, the output is not sorted, it has been sorted here for ease of comparison with the other samples' output.
Line 223:
=={{header|Arturo}}==
<
getOdd: function [w][
Line 237:
if and? [4 < size ow][contains? words ow] ->
print [word "=>" ow]
]</
{{out}}
Line 257:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f ODD_WORDS.AWK unixdict.txt
#
Line 286:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 314:
=={{header|C++}}==
<
#include <fstream>
#include <iomanip>
Line 376:
return EXIT_SUCCESS;
}</
{{out}}
Line 405:
=={{header|F_Sharp|F#}}==
<
// Odd words: Nigel Galloway. June 9th., 2021
let dict=seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(fun n->n.Length>4)|>List.ofSeq
let fN g=let n=g|>String.mapi(fun n g->if n%2=0 then g else ' ')|>String.filter((<>)' ') in match List.contains n dict with true->Some(n,g) |_->None
dict|>Seq.filter(fun n->n.Length>6)|>Seq.choose fN|>Seq.iter(fun(n,g)->printfn "%s -> %s" g n)
</syntaxhighlight>
{{out}}
<pre>
Line 431:
This is basically the same program as https://rosettacode.org/wiki/Alternade_words#Factor. <code><evens></code> is a virtual sequence representing the (zero-based) even indices of the input sequence, which this task calls the odd indices.
{{works with|Factor|0.99 2020-08-14}}
<
kernel literals math sequences sequences.extras sets strings ;
Line 444:
[ length 8 > ] filter
[ odd wordset in? ] filter
[ dup odd "%-15s %s\n" printf ] each</
{{out}}
<pre>
Line 465:
=={{header|FreeBASIC}}==
<
type node
Line 522:
nextword:
curr = curr->nxt
wend</
{{out}}
<pre>barbarian ---> brain
Line 547:
=={{header|Go}}==
<
import (
Line 586:
}
}
}</
{{out}}
Line 609:
=={{header|J}}==
In J, the index of the first character is 0, and the index of the second character is 1, thus:<
annal
motto
posey
scoop
train</
However, some languages have 1 for the index of the first character, which would have given us:<
brain
cider
Line 628:
slain
spree
trial</
=={{header|Java}}==
<
import java.util.*;
Line 697:
}
}
}</
{{out}}
Line 730:
Counting the letters from 1...
<
INDEX(inputs | select(length>4); .)
Line 738:
| (explode | [.[range(0;length;2)]] | implode) as $odd
| select( ($odd|length > 4) and $dict[$odd])
| "\(lpad(10)) : \($odd)"</
{{out}}
Illustrative invocation: jq -nrR -f program.jq unixdict.txt
Line 761:
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
<
foreachword("unixdict.txt", isoddword, minlen=9, numcols=1)
</
<pre>
Word source: unixdict.txt
Line 784:
=={{header|Lua}}==
<
minWordLength = minOddWordLength*2-1
Line 805:
end
end
end</
{{out}}
Line 826:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
dict //= StringSplit[#, "\n"] &;
ClearAll[OddLetters, OddWordQ]
Line 837:
]
]
{#, OddLetters[#]} & /@ Select[dict, OddWordQ] // Grid</
{{out}}
<pre>barbarian brain
Line 855:
=={{header|Nim}}==
<
const DictFile = "unixdict.txt"
Line 867:
if oddWord.len > 4 and oddWord in words:
inc count
echo &"{count:2}: {word:12} → {oddWord}"</
{{out}}
Line 886:
=={{header|Perl}}==
<
@ARGV = 'unixdict.txt';
Line 896:
my $oddword = s/(.).?/$1/gr;
exists $dict{$oddword} and print " $_ $oddword\n";
}</
{{out}}
<pre>
Line 916:
=={{header|Phix}}==
<!--<
<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>
Line 924:
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">oddch</span><span style="color: #0000FF;">),</span><span style="color: #000000;">over4</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</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;">"%d odd words found: %s\n"</span><span style="color: #0000FF;">,{</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: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 931:
=== Alternative ===
Slightly more traditional, same output.
<!--<
<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>
Line 946:
<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;">"%d odd words found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</
=={{header|Python}}==
Line 954:
Tested on Python 3+, the file download will work only if the link is still active. It is possible that you may be able to fetch the file in your browser but download via code may still fail. Check whether you are connected to a VPN, it works on open networks.
<syntaxhighlight lang="python">
#Aamrun, 4th October 2021
Line 973:
[print(i) for i in sorted(oddWordSet)]
</syntaxhighlight>
{{out}}
<pre>brain
Line 990:
===Functional===
Defining a dictionary of oddWords, keyed by their sources:
<
from os.path import expanduser
Line 1,038:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>{
Line 1,059:
=={{header|Quackery}}==
<
protect sift.test
Line 1,094:
else drop ]
dict release</
{{out}}
Line 1,116:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my (@odds, @evens);
Line 1,130:
.put for flat 'Odd words > 4:', @odds.sort;
.put for flat "\nEven words > 4:", @evens.sort;</
{{out}}
<pre>Odd words > 4:
Line 1,158:
Any words longer than ten characters are truncated in the output.
=== version 1 ===
<
fid='d:\unix.txt'
ww.=0 /* ww.* the words to be analyzed */
Line 1,189:
wo=wo||substr(w,i,1)
End
Return wo</
{{out}}
<pre> 1 barbarian brain
Line 1,211:
It also allows the minimum length to be specified on the command line (CL) as well as the dictionary file identifier.
<
parse arg minL iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 5 /*Not specified? Then use the default.*/
Line 1,237:
end /*j*/
/*stick a fork in it, we're all done. */
say copies('─', 30) finds ' "odd words" found with a minimum length of ' minL</
{{out|output|text= when using the default input:}}
<pre>
Line 1,259:
=={{header|Ring}}==
<
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 1,278:
ok
next
</syntaxhighlight>
Output:
<pre>
Line 1,299:
=={{header|Rust}}==
<
use std::fs::File;
use std::io::{self, BufRead};
Line 1,354:
Err(error) => eprintln!("{}", error),
}
}</
{{out}}
Line 1,383:
=={{header|Swift}}==
<
let minLength = 5
Line 1,435:
} catch {
print(error.localizedDescription)
}</
{{out}}
Line 1,467:
{{libheader|Wren-sort}}
{{libheader|Wren-trait}}
<
import "/fmt" for Fmt
import "/sort" for Find
Line 1,486:
}
}
}</
{{out}}
Line 1,508:
=={{header|XPL0}}==
<
int Dict(26000); \pointers to words (enough for unixdict.txt)
int DictSize; \actual number of pointers in Dict
Line 1,574:
DI:= DI+1;
until DI >= DictSize;
]</
{{out}}
|