Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Find Length Of Longest Ascending/Descending Sequence

by wind (Priest)
on May 09, 2011 at 19:50 UTC ( [id://903822]=note: print w/replies, xml ) Need Help??


in reply to Re: Find Length Of Longest Ascending/Descending Sequence
in thread Find Length Of Longest Ascending/Descending Sequence

As far as I can tell, when using a purely regex solution, you pretty much have to keep the scanning for ascending and descending sequences separated. Otherwise overlapping sequences will be missed:

123xxxx56543

Capturing in one go will return qw(123 56 543) instead of qw(123 56 6543);

my $asc = join '|', map {"$_(?=".(($_+1)%10).")"} (0..9); my $dsc = join '|', map {"$_(?=".(($_+9)%10).")"} (0..9); while (<DATA>) { my $longest = max map length, /((?:$asc)+)/g, /((?:$dsc)+)/g; print "$longest\n"; }

Update: This was in reference to your last solution, as previous ones did take this into account.

Replies are listed 'Best First'.
Re^3: Find Length Of Longest Ascending/Descending Sequence
by ikegami (Patriarch) on May 09, 2011 at 20:49 UTC

    Capturing in one go will return qw(123 56 543) instead of qw(123 56 6543);

    Which isn't a problem when you just want the length. The result is 1 more than the length of the longest capture. That's why I increment $longest at the end.

    ...or rather, that's why I was planning on incrementing $longest at the end. Fixed.

      The result is 1 more than the length of the longest capture.

      Why? If the full string is "123", then the result should be 3. If it's "21234", then the result should be 4. Still need to take overlaps into account even if you just want the length.

      Btw, still have the typo in your last bit of code where you use min instead of max.

      my $longest = min map length,

        You don't seen to have read my code closely enough. Specifically, you missed that I capture one short of the whole sequence.

        Why? If the full string is "123", then the result should be 3.

        For "123", "12" is captured and length("12") + 1 is 3.

        If it's "21234", then the result should be 4.

        For "21234", "123" is captured and length("123") + 1 is 4.

        Still need to take overlaps into account even if you just want the length.

        True, but I avoided needing to capture the same digit twice, and thus I avoided the need to visit the string twice.

        you use min instead of max.

        Thanks, fixed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://903822]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-18 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found