I was going to create a nasty little test using length, tr///s, m//, and a couple of evals, but I just couldn't bring myself to complete it. Instead, I reached for CPAN and Math::Combinatorics.
use strict;
use warnings;
use Math::Combinatorics qw( permute );
use Test::More;
my @match_wanted = qw( abc bca cab cba bac acb );
my @match_unwanted = qw( aab abbc acc );
my @charset = qw( a b c );
my %valid = map { join( '', @$_ ) => 1 } permute( @charset );
sub match
{
my $s = shift;
return( exists $valid{$s} ? 1 : 0 );
}
plan 'tests' => ( scalar @match_wanted + scalar @match_unwanted );
ok( match( $_ ), "matches '$_'" ) for @match_wanted;
ok( ! match( $_ ), "does not match '$_'" ) for @match_unwanted;
Output:
1..9
ok 1 - matches 'abc'
ok 2 - matches 'bca'
ok 3 - matches 'cab'
ok 4 - matches 'cba'
ok 5 - matches 'bac'
ok 6 - matches 'acb'
ok 7 - does not match 'aab'
ok 8 - does not match 'abbc'
ok 9 - does not match 'acc'
Thanks and ++ to kyle for the framework.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|