Alternade words: Difference between revisions

m
syntax highlighting fixup automation
(Initial Haskell version.)
m (syntax highlighting fixup automation)
Line 25:
{{trans|Nim}}
 
<langsyntaxhighlight lang=11l>F alternating_words(word)
V r = [‘’, ‘’]
L(c) word
Line 42:
L(line) result
V i = L.index
print((‘#2:’.format(i + 1))‘ ’line)</langsyntaxhighlight>
 
{{out}}
Line 107:
 
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;
 
Line 166:
end;
end loop;
end Alternade_Words;</langsyntaxhighlight>
{{out}}
<pre style="height: 45ex">accost acs cot
Line 229:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang=algol68># find alternade words in a list of words #
# use the associative array in the Associate array/iteration task #
PR read "aArray.a68" PR
Line 298:
OD
FI
</syntaxhighlight>
</lang>
{{out}}
Note - the alternade words as output by this program are not sorted, the output shown here has been sorted for ease of comparison with the output of the other samples.
Line 365:
{{works with|Dyalog APL}}
 
<langsyntaxhighlight lang=APL>alternade←{
⍺←6
parts←{(⊂k/⍵),⊂(~k←2|⍳≢⍵)/⍵}
Line 371:
long←(⍺≤≢¨words)/words
↑(⊂,parts)¨(∧/∘check∘parts¨long)/long
}</langsyntaxhighlight>
 
{{out}}
Line 437:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang=applescript>(*
Now takes the number of subwords per alternade as a parameter.
Extracted texts are validated against the words in the input list.
Line 539:
set wordList to words of (read wordFile as «class utf8»)
-- Return two-word alternades of 6 or more characters. Result in text form.
return alternades(wordList, 2, 6, text)</langsyntaxhighlight>
 
{{output}}
Line 603:
Another example call:
 
<langsyntaxhighlight lang=applescript>-- Return three-word alternades of 10 or more characters. Result as a list of records.
return alternades(wordList, 3, 10, list)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang=applescript>{{alternade:"benevolent", subwords:{"belt", "eve", "non"}}, {alternade:"rejuvenate", subwords:{"rune", "eva", "jet"}}}</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang=rebol>words: map read.lines relative "unixdict.txt" => strip
 
loop words 'word [
Line 624:
if and? [contains? words alt1][contains? words alt2] ->
print [word "=>" alt1 alt2]
]</langsyntaxhighlight>
 
{{out}}
Line 688:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>FileRead, db, % A_Desktop "\unixdict.txt"
words := [], sixWords := []
 
Line 709:
result .= w "`t-> " w1 " " w2 "`n"
}
MsgBox % result</langsyntaxhighlight>
{{out}}
<pre style="height: 45ex">accost -> acs cot
Line 771:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f ALTERNADE_WORDS.AWK unixdict.txt
#
Line 801:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 866:
=={{header|C}}==
 
<langsyntaxhighlight lang=C>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Line 999:
scan_trie(root, check_alternade);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,064:
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <cstdlib>
#include <fstream>
#include <iomanip>
Line 1,100:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 1,165:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// Alternade words: Nigel Galloway. June 10th., 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>2)|>List.ofSeq
Line 1,171:
let fG(n,g)=let fN g=(Array.ofList>>System.String) g in let n,g=fN n,fN g in if List.contains n dict && List.contains g dict then Some(n,g) else None
dict|>Seq.filter(fun n->n.Length>5)|>Seq.iter(fun n->match (fN>>fG) n with Some(g,l)->printfn "%s -> %s * %s" n g l |_->())
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,234:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: formatting io.encodings.ascii io.files kernel literals
math sequences sequences.extras sets strings ;
 
Line 1,251:
[ alternade? ] filter
[ length 5 > ] filter
[ dup subwords "%-8s %-4s %-4s\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre style="height: 45ex">
Line 1,315:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>#define DLEN 25104
 
function is_in(word() as string, s as string) as boolean
Line 1,341:
next j
if is_in(word(), alt(0)) and is_in(word(), alt(1)) then print word(i), alt(1), alt(0)
next i</langsyntaxhighlight>
{{out}}<pre>
accost acs cot
Line 1,404:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,449:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,516:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import qualified Data.Set as S
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
Line 1,535:
in T.unlines $ map alterShow $ alternades dict $ filter longEnough words'
where longEnough = (>= 6) . T.length
alterShow (w, (x, y)) = T.unwords [w, x, y]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,601:
=={{header|J}}==
 
<langsyntaxhighlight lang=J> words=: cutLF fread 'unixdict.txt'
alts=: <@deb"1@|:@(_2]\])@> ref=: (#~ 5<#@>) words
;:inv (ref,.alts) #~ alts */@e."1 words
Line 1,661:
troupe top rue
truant tun rat
twirly til wry</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang=java>import java.io.*;
import java.util.*;
 
Line 1,698:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,767:
 
Note: For gojq, change `keys_unsorted` to `keys`. The results are unaffected.
<syntaxhighlight lang=jq>
<lang jq>
# if the input is an alternade word relative to the keys of $dict,
# then emit [$even, $odd], else false.
Line 1,784:
| (select(.) // empty)
| "\($w) → \(.[0]) \(.[1])"
</syntaxhighlight>
</lang>
Invocation: jq -n -rR -f alternade-words.jq unixdict.txt
{{out}}
Line 1,849:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>function foreachword(wordfile::String, condition::Function; minlen = 0, colwidth = 15, numcols = 6, toshow = 0)
println("Word source: $wordfile\n")
words = split(read(wordfile, String), r"\s+")
Line 1,869:
foreachword("unixdict.txt", isalternade, minlen = 6, numcols=1)
 
</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
Line 1,934:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>dic = Once[Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt"]];
dic = StringSplit[dic, "\n"];
largerthansix = Select[dic, StringLength /* GreaterEqualThan[6]];
Line 1,946:
]
all = DeleteCases[Table[AlternadeQ[w, smallerthansix], {w, largerthansix}], {}];
all // Column</langsyntaxhighlight>
{{out}}
<pre>accost->{acs,cot}
Line 2,008:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=Nim>import algorithm, sets, strutils, sugar
 
proc alternatingWords(word: string): array[2, string] =
Line 2,024:
result.add word.align(8) & " → $1 $2" % altWords
result.sort()
for i, line in result: echo ($(i+1)).align(2), ": ", line</langsyntaxhighlight>
 
{{out}}
Line 2,087:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>#!/usr/bin/perl
 
use strict;
Line 2,099:
my $odd = s/.(.?)/$1/gr;
$words{$even} && $words{$odd} and print "$_ => [ $even $odd ]\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight 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>
Line 2,128:
<span style="color: #7060A8;">pp</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;">"alternade words found"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,163:
=={{header|Picat}}==
{{works with|Picat}}
<langsyntaxhighlight lang=Picat>
split_word(Word) = Words =>
N = len(Word),
Line 2,188:
end,
close(Reader).
</syntaxhighlight>
</lang>
{{out}}
<pre style="height: 45ex">
Line 2,253:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang=prolog>:- dynamic dictionary_word/1.
 
main:-
Line 2,293:
odd_even_chars([Ch], [Ch], []):-!.
odd_even_chars([Ch1, Ch2|Chars], [Ch1|Chars1], [Ch2|Chars2]):-
odd_even_chars(Chars, Chars1, Chars2).</langsyntaxhighlight>
 
{{out}}
Line 2,359:
=={{header|Python}}==
 
<langsyntaxhighlight lang=python>WORDFILE = 'unixdict.txt'
MINLEN = 6
 
Line 2,424:
odd = word[1::2]
if even in words and odd in words:
print(word, even, odd)</langsyntaxhighlight>
 
{{out}}
Line 2,489:
=={{header|Quackery}}==
 
<langsyntaxhighlight lang=Quackery> [ stack ] is sift.test ( --> s )
protect sift.test
 
Line 2,524:
else [ drop 2drop ] ]
dict release</langsyntaxhighlight>
 
{{out}}
Line 2,590:
A more straightforward approach:
 
<langsyntaxhighlight lang=Quackery> [ [] [] rot witheach [ join swap ] ] is unzip ( [ --> [ [ )
 
[ $ "rosetta/unixdict.txt" sharefile
Line 2,610:
echo$ cr ]
else drop ]
else 2drop ]</langsyntaxhighlight>
 
{{out}}
Line 2,675:
=={{header|Raku}}==
 
<langsyntaxhighlight lang=perl6>unit sub MAIN ($file = 'unixdict.txt', :$min = 6);
 
my %words = $file.IO.slurp.words.map: * => 1;
Line 2,694:
.say for @alternades > 10
?? (flat @alternades.head(5), '...', @alternades.tail(5))
!! @alternades;</langsyntaxhighlight>
{{out}}
<pre>58 alternades longer than 5 characters found in unixdict.txt:
Line 2,715:
 
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 alternade words (within an identified dictionary).*/
parse arg minL iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 6 /*Not specified? Then use the default.*/
Line 2,741:
end /*j*/
/*stick a fork in it, we're all done. */
say copies('─',30) finds ' alternade words found with a minimum length of ' minL</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,811:
 
Independently developed at the same time as version 1 :-)
<langsyntaxhighlight lang=rexx>/* REXX */
fid='d:\unix.txt'
cnt.=0 /* cnt.n -> words of length n */
Line 2,851:
s.v=s.v||vv
End
Return s.u s.v</langsyntaxhighlight>
Output:
<pre>D:\>rexx alternade
Line 2,920:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
load "stdlib.ring"
 
Line 2,948:
next
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,189:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>words = File.open("unixdict.txt").map(&:chomp)
 
res = words.filter_map do |word|
Line 3,199:
 
puts res
</syntaxhighlight>
</lang>
{{out}}
<pre style="height: 30ex">accost: acs, cot
Line 3,262:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>use std::collections::BTreeSet;
use std::fs::File;
use std::io::{self, BufRead};
Line 3,298:
Err(error) => eprintln!("{}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,363:
 
=={{header|Swift}}==
<langsyntaxhighlight lang=swift>import Foundation
 
func loadDictionary(_ path: String) throws -> Set<String> {
Line 3,401:
} catch {
print(error.localizedDescription)
}</langsyntaxhighlight>
 
{{out}}
Line 3,466:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang=tcl>set unixdict [dict create]
foreach word [read [open unixdict.txt]] {
dict set unixdict $word {}
Line 3,485:
puts "$word $oddeven"
}
}</langsyntaxhighlight>
{{out}}
<pre>$ tclsh alternate_words.tcl | column -t
Line 3,549:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang=vlang>import os
 
fn main() {
Line 3,580:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Same as Go entry</pre>
Line 3,587:
{{libheader|Wren-set}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "io" for File
import "/set" for Set
import "/fmt" for Fmt
Line 3,615:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,682:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=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 3,749:
DI:= DI+1;
until DI >= DictSize;
]</langsyntaxhighlight>
 
{{out}}
10,333

edits