Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Find Length Of Longest Ascending/Descending Sequence

by moritz (Cardinal)
on May 10, 2011 at 08:41 UTC ( #903927=note: print w/replies, xml ) Need Help??


in reply to Find Length Of Longest Ascending/Descending Sequence

Not a working solution, just a thought experiment:

Perl 6 has a regex construct spelled <*abcd>. It is a shortcut for a|ab|abc|abcd (also note that in Perl 6 regexes, the longest pipe-delimited alternative wins, not the first one as in Perl 5).

Sadly no compiler implements the <*abcd> feature yet, so I can't demonstrate it, but I think something like this should work:

my token longest_asc_desc { # ascending | <*0123456789> ** <?after 9> | <*1234567890> ** <?after 0> | <*2345678901> ** <?after 1> ... # descending: | <*9876543210> ** <?after 0> | <*8765432109> ** <?after 9> | <*7654321098> ** <?after 8> ... }

Replies are listed 'Best First'.
Re^2: Find Length Of Longest Ascending/Descending Sequence
by Ratazong (Monsignor) on May 10, 2011 at 09:22 UTC

    nice feature :-)

    How will it match data like the following?

    11101234567890123411111 => expected result: 15
    Will you need to create the pattern in <*0123456789> dynamically based on the length of the data?

    Rata
      Notice the repetition:
      <*0123456789> ** <?after 9>

      is short for

      <*0123456789> [ <?after 9> <*0123456789>]*

      So if it matches the first one, and upto 9 (that's what the <?after 9> checks), it tries again to start with 0.

      To extract the longest match, you need to collect all, and overlapping (because the last digits of an ascending sequence can be the first of a descending sequence):

      my regex seq { ... } my $length = [max] $teststring.match(&seq, :g, :overlap)>>.chars;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903927]
help
Chatterbox?
[marto]: fortunately my bad jokes and puns don't fall into those categories :P
[Corion]: :-D
Corion discovers a new shiny toy to try out over the (longish) weekend. Since I've done some more with websockets, maybe I'll try writing a webserver that implements hot-reloading of HTML(+CSS, +Javascript) in the browser. Edit the local file and ...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-04-26 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?