in reply to Find Length Of Longest Ascending/Descending Sequence

How does 01234567890987654321 measure?

A single compliant sequence? Two that overlap? Other?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Find Length Of Longest Ascending/Descending Sequence
by Limbic~Region (Chancellor) on May 09, 2011 at 21:23 UTC
    BrowserUk,
    The sequence is defined as either ascending or descending - not both at the same time so two overlapping. In the end, I am only interested in the length of the longest so if there are ties for first place pick one.

    Cheers - L~R

      I am only interested in the length of the longest

      In that case, this should work. Nothing special, but just one pass of the string:

      #! perl -slw use strict; sub z { my $soFar = 1; my $lastC = substr $_[0], 0, 1; my $lastS = $lastC <=> substr $_[0], 1, 1; my $bestN = 0; for my $p ( 1 .. length( $_[0] )-1 ) { my $this = substr $_[0], $p, 1; my $d = abs( $this - $lastC ); my $s = $this <=> $lastC; $s *= -1 if $d == 9; if( $s != $lastS or $d != 1 and $d != 9 ) { $soFar = 1; $lastS = $s; } if( ++$soFar > $bestN ) { $bestN = $soFar; } $lastC = $this; } return $bestN; } while( <DATA> ) { chomp; my $l = z( $_ ); print $_, ' : ', $l; } __DATA__ 01234567890987654321 01234567890123456789 78901234567890123456 98765432109876543210 21098765432109876543 012345678890123456789 789012345677890123456 987654321099876543210 210987654322109876543 012345678900987654321

      That is one nasty, messy problem :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Find Length Of Longest Ascending/Descending Sequence
by wind (Priest) on May 09, 2011 at 20:04 UTC
    I'm going with two that overlap:
    qw(01234567890 0987654321);