[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] 4 Consecutive Letters



In message <20000814122348.C601060@linguist.dartmouth.edu>,
    Ronald J Kimball writes:

: On Mon, Aug 14, 2000 at 11:04:16AM -0500, Greg Bacon wrote:
:
: > I'll probably lose my tour card, but
: > 
: >     sub inarow {
: >         my $word = lc(shift || '');
: >         my $cnt  = shift || 0;
: > 
: >         return unless $word && $cnt > 0;
: >         return unless length($word) >= $cnt;
: > 
: >         return 1 if $cnt == 1;
: 
: Good, factor out the obvious cases first!

As you noted, I should have also done something like

    $word =~ tr/a-z/ /c;

: >         my $code = q<
: >     do {
: >         study $word;
: 
: You study $word, but your target string for the matches is $a.

Doh!  That should probably be C<study $a>, but I wonder whether it
would help.

: >         $code .= join ' || ',
: >                  map '$a =~ /' . $_ . '/',
: >                  map substr($word, $_, $cnt),
: >                  0 .. length($word)-$cnt;
: 
: Two maps are no better than one:
: 
: map {'$a =~ /' . substr($word, $_, $cnt) . '/'} (0 .. length($word)-$cnt);

Touche.  Rosler will nail you for using the slower map BLOCK form. :-)

: Using a constant string for the target and the input for the regex is a
: neat idea.

I got it from tchrist on #perl when someone asked how to determine
whether a number is a string of consecutive digits.

:             In this case it means a lot of evalling, though...

How so?

: I don't think you need the if/else; C<if (expr) then { 1 } else { 0 }> is
: equivalent to C<scalar(expr)>.

Like I said, I'd probably lose my tour card. :-/

How about this?

    sub inarow {
        my $word = lc(shift || '');
        my $cnt  = shift || 0;

        $word =~ tr/a-z/ /c;

        return unless $word && $cnt > 0;
        return unless length($word) >= $cnt;

        return 1 if $cnt == 1;

        my $code = q<
    do {
        my $a = "abcdefghijklmnopqrstuvwxyz";
        study $a;
        $a =~ />;

        $code .= join '|',
                 sort
                 map substr($word, $_, $cnt),
                 0 .. length($word)-$cnt;

        $code .= "/\n}";

        #print $code, "\n";
        my $result = eval $code;
        die $@ if $@;

        $result;
    }

Greg

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe