Anagrams/Deranged anagrams: Difference between revisions
m
→{{header|Perl}}: future-proof for 5.36, use new bitwise string operators
(→{{header|Haskell}}: Added a variant) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36, use new bitwise string operators) |
||
Line 3,848:
=={{header|Perl}}==
===String operations===
<syntaxhighlight lang="perl">sub deranged { # only anagrams ever get here▼
use warnings;
my @a = split('', shift); # split word into letters
my @b = split('', shift);
Line 3,882 ⟶ 3,886:
keys %letter_list )
{
# if we find a pair, they are the
last if find_deranged(@{ $letter_list{$_} });
}</syntaxhighlight>
{{out}}
<pre>length 10: excitation => intoxicate</pre>▼
===Bitwise operations===
▲length 10: excitation => intoxicate
<syntaxhighlight lang="perl">use strict;
▲<syntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use feature 'bitwise';
local (@ARGV, $/) = 'unixdict.txt';
Line 3,901 ⟶ 3,902:
{
my $key = join '', sort +split //, $word;
($_ ^. $word) =~ /\0/ or exit !print "$_ $word\n" for @{ $anagrams{$key} };
push @{ $anagrams{$key} }, $word;
}</syntaxhighlight>
{{out}}
<pre>excitation intoxicate</pre>▼
▲excitation intoxicate
=={{header|Phix}}==
|