Jump to content

Anagrams/Deranged anagrams: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 666:
<pre>
longest derangement: intoxicate excitation
</pre>
 
=={{header|C sharp}}==
{{libheader|System}}
{{libheader|System.Collections.Generic}}
{{libheader|System.Linq}}
{{libheader|System.IO}}
{{works with|C sharp|6}}
<lang csharp>public static void Main()
{
var lookupTable = File.ReadLines("unixdict.txt").ToLookup(line => AnagramKey(line));
var query = from a in lookupTable
orderby a.Key.Length descending
let deranged = FindDeranged(a)
where deranged != null
select deranged[0] + " " + deranged[1];
Console.WriteLine(query.FirstOrDefault());
}
static string AnagramKey(string word) => new string(word.OrderBy(c => c).ToArray());
static string[] FindDeranged(IEnumerable<string> anagrams) => (
from first in anagrams
from second in anagrams
where !second.Equals(first)
&& Enumerable.Range(0, first.Length).All(i => first[i] != second[i])
select new [] { first, second })
.FirstOrDefault();
</lang>
{{out}}
<pre>
excitation intoxicate
</pre>
 
Line 719 ⟶ 751:
return EXIT_SUCCESS;
}</lang>
{{out}}
<pre>
excitation intoxicate
</pre>
 
=={{header|C sharp}}==
{{libheader|System}}
{{libheader|System.Collections.Generic}}
{{libheader|System.Linq}}
{{libheader|System.IO}}
{{works with|C sharp|6}}
<lang csharp>public static void Main()
{
var lookupTable = File.ReadLines("unixdict.txt").ToLookup(line => AnagramKey(line));
var query = from a in lookupTable
orderby a.Key.Length descending
let deranged = FindDeranged(a)
where deranged != null
select deranged[0] + " " + deranged[1];
Console.WriteLine(query.FirstOrDefault());
}
static string AnagramKey(string word) => new string(word.OrderBy(c => c).ToArray());
static string[] FindDeranged(IEnumerable<string> anagrams) => (
from first in anagrams
from second in anagrams
where !second.Equals(first)
&& Enumerable.Range(0, first.Length).All(i => first[i] != second[i])
select new [] { first, second })
.FirstOrDefault();
</lang>
{{out}}
<pre>
Line 2,304:
length 10: excitation => intoxicate
</pre>
 
=={{header|Perl 6}}==
 
{{works with|Rakudo|2016.08}}
 
<lang perl6>my @anagrams = 'unixdict.txt'.IO.words
.map(*.comb.cache) # explode words into lists of characters
.classify(*.sort.join).values # group words with the same characters
.grep(* > 1) # only take groups with more than one word
.sort(-*[0]) # sort by length of the first word
;
 
for @anagrams -> @group {
for @group.combinations(2) -> [@a, @b] {
if none @a Zeq @b {
say "{@a.join} {@b.join}";
exit;
}
}
}</lang>
 
{{out}}
<pre>excitation intoxicate</pre>
 
=={{header|Phix}}==
Line 2,931 ⟶ 2,908:
{{out}}
<pre>'(("intoxicate" "excitation"))</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
{{works with|Rakudo|2016.08}}
 
<lang perl6>my @anagrams = 'unixdict.txt'.IO.words
.map(*.comb.cache) # explode words into lists of characters
.classify(*.sort.join).values # group words with the same characters
.grep(* > 1) # only take groups with more than one word
.sort(-*[0]) # sort by length of the first word
;
 
for @anagrams -> @group {
for @group.combinations(2) -> [@a, @b] {
if none @a Zeq @b {
say "{@a.join} {@b.join}";
exit;
}
}
}</lang>
 
{{out}}
<pre>excitation intoxicate</pre>
 
=={{header|REXX}}==
Line 3,364 ⟶ 3,365:
{{out}}
<pre>length 10: excitation => intoxicate</pre>
 
=={{header|Simula}}==
<lang simula>! cim --memory-pool-size=512 deranged-anagrams.sim;
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.