Odd words: Difference between revisions

m
syntax highlighting fixup automation
(J: see talk page)
m (syntax highlighting fixup automation)
Line 12:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V wordSet = Set(File(‘unixdict.txt’).read().split("\n"))
 
Set[String] oddWordSet
Line 21:
 
L(i) sorted(Array(oddWordSet))
print(i)</langsyntaxhighlight>
 
{{out}}
Line 41:
=={{header|Ada}}==
Using instances of generic procedure to filter odd/even words.
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;
 
Line 110:
Put_Line ("Even words:");
Put_Even_Words;
end Odd_Words;</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="algol68"># find words where the odd letters also form a word #
# use the associative array in the Associate array/iteration task #
PR read "aArray.a68" PR
Line 202:
e := NEXT words
OD
FI</langsyntaxhighlight>
{{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}}==
 
<langsyntaxhighlight lang="rebol">words: read.lines relative "unixdict.txt"
 
getOdd: function [w][
Line 237:
if and? [4 < size ow][contains? words ow] ->
print [word "=>" ow]
]</langsyntaxhighlight>
 
{{out}}
Line 257:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ODD_WORDS.AWK unixdict.txt
#
Line 286:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 314:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <fstream>
#include <iomanip>
Line 376:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 405:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{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}}
<langsyntaxhighlight lang="factor">USING: formatting hash-sets io io.encodings.ascii io.files
kernel literals math sequences sequences.extras sets strings ;
 
Line 444:
[ length 8 > ] filter
[ odd wordset in? ] filter
[ dup odd "%-15s %s\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 465:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define NULL 0
 
type node
Line 522:
nextword:
curr = curr->nxt
wend</langsyntaxhighlight>
{{out}}
<pre>barbarian ---> brain
Line 547:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 586:
}
}
}</langsyntaxhighlight>
 
{{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:<langsyntaxhighlight Jlang="j"> >(([-.-.) (#~ $&0 1@#)&.>) (#~ 4<#@>)cutLF fread'unixdict.txt'
annal
motto
posey
scoop
train</langsyntaxhighlight>
 
However, some languages have 1 for the index of the first character, which would have given us:<langsyntaxhighlight Jlang="j"> >(([-.-.) (#~ $&1 0@#)&.>) (#~ 4<#@>)cutLF fread'unixdict.txt'
brain
cider
Line 628:
slain
spree
trial</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.io.*;
import java.util.*;
 
Line 697:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 730:
 
Counting the letters from 1...
<langsyntaxhighlight lang="jq"> def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
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)"</langsyntaxhighlight>
{{out}}
Illustrative invocation: jq -nrR -f program.jq unixdict.txt
Line 761:
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
<langsyntaxhighlight lang="julia">isoddword(w, d) = (o = mapreduce(i -> w[i], *, 1:2:length(w)); haskey(d, o) ? rpad(w, 16) * ": " * o : "")
foreachword("unixdict.txt", isoddword, minlen=9, numcols=1)
</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
Line 784:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">minOddWordLength = 5
minWordLength = minOddWordLength*2-1
 
Line 805:
end
end
end</langsyntaxhighlight>
 
{{out}}
Line 826:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">dict = Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt"];
dict //= StringSplit[#, "\n"] &;
ClearAll[OddLetters, OddWordQ]
Line 837:
]
]
{#, OddLetters[#]} & /@ Select[dict, OddWordQ] // Grid</langsyntaxhighlight>
{{out}}
<pre>barbarian brain
Line 855:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sets, strformat, sugar
 
const DictFile = "unixdict.txt"
Line 867:
if oddWord.len > 4 and oddWord in words:
inc count
echo &"{count:2}: {word:12} → {oddWord}"</langsyntaxhighlight>
 
{{out}}
Line 886:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
@ARGV = 'unixdict.txt';
Line 896:
my $oddword = s/(.).?/$1/gr;
exists $dict{$oddword} and print " $_ $oddword\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 916:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 931:
=== Alternative ===
Slightly more traditional, same output.
<!--<langsyntaxhighlight Phixlang="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>
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>
<!--</langsyntaxhighlight>-->
 
=={{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">
<lang Python>
#Aamrun, 4th October 2021
 
Line 973:
 
[print(i) for i in sorted(oddWordSet)]
</syntaxhighlight>
</lang>
{{out}}
<pre>brain
Line 990:
===Functional===
Defining a dictionary of oddWords, keyed by their sources:
<langsyntaxhighlight lang="python">'''Odd words'''
 
from os.path import expanduser
Line 1,038:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>{
Line 1,059:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ stack ] is sift.test ( --> s )
protect sift.test
 
Line 1,094:
else drop ]
 
dict release</langsyntaxhighlight>
 
{{out}}
Line 1,116:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>my %words = 'unixdict.txt'.IO.slurp.words.map: * => 1;
 
my (@odds, @evens);
Line 1,130:
.put for flat 'Odd words > 4:', @odds.sort;
 
.put for flat "\nEven words > 4:", @evens.sort;</langsyntaxhighlight>
{{out}}
<pre>Odd words > 4:
Line 1,158:
Any words longer than ten characters are truncated in the output.
=== version 1 ===
<langsyntaxhighlight lang="rexx">/* REXX */
fid='d:\unix.txt'
ww.=0 /* ww.* the words to be analyzed */
Line 1,189:
wo=wo||substr(w,i,1)
End
Return wo</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="rexx">/*REXX program finds all the caseless "odd words" (within an identified dictionary). */
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,259:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 1,278:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,299:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::collections::BTreeSet;
use std::fs::File;
use std::io::{self, BufRead};
Line 1,354:
Err(error) => eprintln!("{}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,383:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
let minLength = 5
Line 1,435:
} catch {
print(error.localizedDescription)
}</langsyntaxhighlight>
 
{{out}}
Line 1,467:
{{libheader|Wren-sort}}
{{libheader|Wren-trait}}
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/fmt" for Fmt
import "/sort" for Find
Line 1,486:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,508:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
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;
]</langsyntaxhighlight>
 
{{out}}
10,327

edits