Anadromes: Difference between revisions

Content added Content deleted
(Added Rust solution)
m (syntax highlighting fixup automation)
Line 25: Line 25:
So you will need to use another compiler under Windows.<br>
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.
As in the Wren sample, the words are quicksorted so binary searching can be used to find the reversed words.
<lang algol68>BEGIN # find some anadromes: words that whwn reversed are also words #
<syntaxhighlight lang=algol68>BEGIN # find some anadromes: words that whwn reversed are also words #
# in-place quick sort an array of strings #
# in-place quick sort an array of strings #
PROC s quicksort = ( REF[]STRING a, INT lb, ub )VOID:
PROC s quicksort = ( REF[]STRING a, INT lb, ub )VOID:
Line 114: Line 114:
OD;
OD;
print( ( newline, "Found ", whole( a count, 0 ), " anadromes", newline ) )
print( ( newline, "Found ", whole( a count, 0 ), " anadromes", newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 144: Line 144:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
<syntaxhighlight lang=applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "Foundation"
use scripting additions
use scripting additions
Line 189: Line 189:
end Anadromes
end Anadromes


return Anadromes(((path to desktop as text) & "www.rosettacode.org:words.txt") as «class furl», 7)</lang>
return Anadromes(((path to desktop as text) & "www.rosettacode.org:words.txt") as «class furl», 7)</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"amaroid <--> diorama
<syntaxhighlight lang=applescript>"amaroid <--> diorama
degener <--> reneged
degener <--> reneged
deifier <--> reified
deifier <--> reified
Line 208: Line 208:
reliver <--> reviler
reliver <--> reviler
revotes <--> setover
revotes <--> setover
sallets <--> stellas"</lang>
sallets <--> stellas"</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<syntaxhighlight lang=AWK>
# syntax: GAWK -f ANADROMES.AWK WORDS.TXT
# syntax: GAWK -f ANADROMES.AWK WORDS.TXT
#
#
Line 245: Line 245:
return(rts)
return(rts)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 269: Line 269:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <algorithm>
<syntaxhighlight lang=cpp>#include <algorithm>
#include <cstdlib>
#include <cstdlib>
#include <fstream>
#include <fstream>
Line 298: Line 298:
}
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 322: Line 322:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang=fsharp>
// Anadromes. Nigel Galloway: June 26th., 2022
// 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
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))
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}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
{{works with|Factor|0.99 2022-04-03}}
<lang factor>USING: assocs grouping hash-sets io.encodings.ascii io.files
<syntaxhighlight lang=factor>USING: assocs grouping hash-sets io.encodings.ascii io.files
kernel math prettyprint sequences sets sets.extras ;
kernel math prettyprint sequences sets sets.extras ;


"words.txt" ascii file-lines [ length 6 > ] filter dup >hash-set '[ reverse _ in? ] filter
"words.txt" ascii file-lines [ length 6 > ] filter dup >hash-set '[ reverse _ in? ] filter
[ reverse ] zip-with [ all-equal? ] reject [ fast-set ] unique-by .</lang>
[ reverse ] zip-with [ all-equal? ] reject [ fast-set ] unique-by .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 358: Line 358:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import qualified Data.Set as S
<syntaxhighlight lang=haskell>import qualified Data.Set as S
import qualified Data.Text as T
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.Text.IO as TIO
Line 374: Line 374:
main = TIO.interact (T.unlines . map anaShow . anadromes . longEnough . T.lines)
main = TIO.interact (T.unlines . map anaShow . anadromes . longEnough . T.lines)
where longEnough = filter ((> 6) . T.length)
where longEnough = filter ((> 6) . T.length)
anaShow (x, y) = T.unwords [x, y]</lang>
anaShow (x, y) = T.unwords [x, y]</syntaxhighlight>


{{out}}
{{out}}
Line 418: 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:
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:


<lang J>words=: cutLF fread 'words.txt'
<syntaxhighlight lang=J>words=: cutLF fread 'words.txt'
canon=: {.@/:~@(,:|.) each words
canon=: {.@/:~@(,:|.) each words
akeys=: (~. #~ 2 = #/.~) canon
akeys=: (~. #~ 2 = #/.~) canon
tkeys=: (#~ 6 < #@>) akeys
tkeys=: (#~ 6 < #@>) akeys
order=: /: canon
order=: /: canon
pairs=: _2]\ (order{canon e. tkeys) # order { words</lang>
pairs=: _2]\ (order{canon e. tkeys) # order { words</syntaxhighlight>


This gives us:
This gives us:


<lang J> pairs
<syntaxhighlight lang=J> pairs
┌────────┬────────┐
┌────────┬────────┐
│amaroid │diorama │
│amaroid │diorama │
Line 462: Line 462:
├────────┼────────┤
├────────┼────────┤
│sallets │stellas │
│sallets │stellas │
└────────┴────────┘</lang>
└────────┴────────┘</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang ruby>function anadromes(minsize, csense = true, fname = "words.txt")
<syntaxhighlight 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+")))
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]
found = [(w, reverse(w)) for w in words if (r = reverse(w)) in words && w < r]
Line 474: Line 474:
anadromes(7)
anadromes(7)
anadromes(7, false)
anadromes(7, false)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Total 17 case sensitive anadrome pairs found.
Total 17 case sensitive anadrome pairs found.
Line 530: Line 530:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;


Line 540: Line 540:
}
}


print $A{$_} for sort keys %A;</lang>
print $A{$_} for sort keys %A;</syntaxhighlight>
{{out}}
{{out}}
<pre> amaroid ↔ diorama
<pre> amaroid ↔ diorama
Line 561: Line 561:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: 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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
On the desktop:
On the desktop:
Line 606: Line 606:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>my @words = 'words.txt'.IO.slurp.words.grep: *.chars > 6;
<syntaxhighlight lang=perl6>my @words = 'words.txt'.IO.slurp.words.grep: *.chars > 6;


my %words = @words.pairs.invert;
my %words = @words.pairs.invert;


put join "\n", @words.map: { %words{$_}:delete and sprintf "%10s ↔ %s", $_, .flip if ($_ ne .flip) && %words{.flip} }</lang>
put join "\n", @words.map: { %words{$_}:delete and sprintf "%10s ↔ %s", $_, .flip if ($_ ne .flip) && %words{.flip} }</syntaxhighlight>
{{out}}
{{out}}
<pre> amaroid ↔ diorama
<pre> amaroid ↔ diorama
Line 631: Line 631:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>use std::collections::BTreeSet;
<syntaxhighlight lang=rust>use std::collections::BTreeSet;
use std::fs::File;
use std::fs::File;
use std::io::{self, BufRead};
use std::io::{self, BufRead};
Line 659: Line 659:
Err(error) => eprintln!("{}", error),
Err(error) => eprintln!("{}", error),
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 683: Line 683:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";


const func boolean: binarySearch (in array string: haystack, in string: needle) is func
const func boolean: binarySearch (in array string: haystack, in string: needle) is func
Line 726: Line 726:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 750: Line 750:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var S = Hash()
<syntaxhighlight lang=ruby>var S = Hash()


File('words.txt').open_r.each {|word|
File('words.txt').open_r.each {|word|
Line 760: Line 760:
S{word} = true
S{word} = true
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 785: Line 785:
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "io" for File
<syntaxhighlight lang=ecmascript>import "io" for File
import "./sort" for Sort, Find
import "./sort" for Sort, Find
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 805: Line 805:
}
}
System.print("The anadrome pairs with more than 6 letters are:")
System.print("The anadrome pairs with more than 6 letters are:")
for (ana in anadromes) Fmt.print("$8s <-> $8s", ana, ana[-1..0])</lang>
for (ana in anadromes) Fmt.print("$8s <-> $8s", ana, ana[-1..0])</syntaxhighlight>


{{out}}
{{out}}