Anadromes: Difference between revisions

m
syntax highlighting fixup automation
(Added Rust solution)
m (syntax highlighting fixup automation)
Line 25:
So you will need to use another compiler under Windows.<br>
As in the Wren sample, the words are quicksorted so binary searching can be used to find the reversed words.
<langsyntaxhighlight lang=algol68>BEGIN # find some anadromes: words that whwn reversed are also words #
# in-place quick sort an array of strings #
PROC s quicksort = ( REF[]STRING a, INT lb, ub )VOID:
Line 114:
OD;
print( ( newline, "Found ", whole( a count, 0 ), " anadromes", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 144:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang=applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
Line 189:
end Anadromes
 
return Anadromes(((path to desktop as text) & "www.rosettacode.org:words.txt") as «class furl», 7)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang=applescript>"amaroid <--> diorama
degener <--> reneged
deifier <--> reified
Line 208:
reliver <--> reviler
revotes <--> setover
sallets <--> stellas"</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f ANADROMES.AWK WORDS.TXT
#
Line 245:
return(rts)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 269:
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <algorithm>
#include <cstdlib>
#include <fstream>
Line 298:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 322:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// Anadromes. Nigel Galloway: June 26th., 2022
let words=seq{use n=System.IO.File.OpenText("words.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(fun n->6<(Seq.length n))|>Seq.map(fun n->n.ToCharArray())|>Set.ofSeq
Set.intersect words (words|>Set.map(Array.rev))|>Set.iter(fun n->if n<Array.rev n then printfn "%s" (System.String n))
</syntaxhighlight>
</lang>
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<langsyntaxhighlight lang=factor>USING: assocs grouping hash-sets io.encodings.ascii io.files
kernel math prettyprint sequences sets sets.extras ;
 
"words.txt" ascii file-lines [ length 6 > ] filter dup >hash-set '[ reverse _ in? ] filter
[ reverse ] zip-with [ all-equal? ] reject [ fast-set ] unique-by .</langsyntaxhighlight>
{{out}}
<pre>
Line 358:
 
=={{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 374:
main = TIO.interact (T.unlines . map anaShow . anadromes . longEnough . T.lines)
where longEnough = filter ((> 6) . T.length)
anaShow (x, y) = T.unwords [x, y]</langsyntaxhighlight>
 
{{out}}
Line 418:
Anyways, the basic approach here is to identify a canonical key for each word, look for paired keys and organize the words based on those keys:
 
<langsyntaxhighlight lang=J>words=: cutLF fread 'words.txt'
canon=: {.@/:~@(,:|.) each words
akeys=: (~. #~ 2 = #/.~) canon
tkeys=: (#~ 6 < #@>) akeys
order=: /: canon
pairs=: _2]\ (order{canon e. tkeys) # order { words</langsyntaxhighlight>
 
This gives us:
 
<langsyntaxhighlight lang=J> pairs
┌────────┬────────┐
│amaroid │diorama │
Line 462:
├────────┼────────┤
│sallets │stellas │
└────────┴────────┘</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang=ruby>function anadromes(minsize, csense = true, fname = "words.txt")
words = Set(filter(w -> length(w) >= minsize, split((csense ? identity : lowercase)(read(fname, String)), r"\s+")))
found = [(w, reverse(w)) for w in words if (r = reverse(w)) in words && w < r]
Line 474:
anadromes(7)
anadromes(7, false)
</langsyntaxhighlight>{{out}}
<pre>
Total 17 case sensitive anadrome pairs found.
Line 530:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>use strict;
use warnings;
 
Line 540:
}
 
print $A{$_} for sort keys %A;</langsyntaxhighlight>
{{out}}
<pre> amaroid ↔ diorama
Line 561:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">5</span><span style="color: #0000FF;">:</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
Line 572:
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%8s &lt;=&gt; %-8s"</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;">"Found %d anadromes:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #000000;">t</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
On the desktop:
Line 606:
 
=={{header|Raku}}==
<langsyntaxhighlight lang=perl6>my @words = 'words.txt'.IO.slurp.words.grep: *.chars > 6;
 
my %words = @words.pairs.invert;
 
put join "\n", @words.map: { %words{$_}:delete and sprintf "%10s ↔ %s", $_, .flip if ($_ ne .flip) && %words{.flip} }</langsyntaxhighlight>
{{out}}
<pre> amaroid ↔ diorama
Line 631:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>use std::collections::BTreeSet;
use std::fs::File;
use std::io::{self, BufRead};
Line 659:
Err(error) => eprintln!("{}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 683:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
 
const func boolean: binarySearch (in array string: haystack, in string: needle) is func
Line 726:
end if;
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 750:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>var S = Hash()
 
File('words.txt').open_r.each {|word|
Line 760:
S{word} = true
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 785:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "io" for File
import "./sort" for Sort, Find
import "./fmt" for Fmt
Line 805:
}
System.print("The anadrome pairs with more than 6 letters are:")
for (ana in anadromes) Fmt.print("$8s <-> $8s", ana, ana[-1..0])</langsyntaxhighlight>
 
{{out}}
10,327

edits