A non-regex solution:
my @digits = /./sg; my @next = (1,2,3,4,5,6,7,8,9,0); my @prev = (9,0,1,2,3,4,5,6,7,8); my $longest_start = my $start = 0; my $longest_len = my $len = 1; my $slope = 0; for (1..$#digits) { my $new_slope = $digits[$_-0] == $next[$digits[$_-1]] ? +1 : $digits[$_-0] == $prev[$digits[$_-1]] ? -1 : 0; if ($new_slope) { if ($new_slope == $slope) { ++$len; } else { $start = $_ - 1; $len = 1; } } $slope = $new_slope; if ($len > $longest_len) { $longest_start = $start; $longest_len = $len; } } my $longest = substr($_, $longest_start, $longest_len+1);
Update: Fixed lack of wrapping.
In reply to Re: Find Length Of Longest Ascending/Descending Sequence
by ikegami
in thread Find Length Of Longest Ascending/Descending Sequence
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |