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

Re: [FWP] Can it be funner?



Ed Perry wrote:
> Here's a fun bit of code to find the longest repeated substring.
>...
> Problem is, it seems there should be a funner way to do it.
> Not just a shorter way (I like named variables), but a fundamentally
> funner way.

Of course, certain values of "fun" =~ "shorter".

print longest_repeated( 'aabbaaabbbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyxyyy' );

sub longest_repeated {
  my $s = shift;
  my $i = 0;
  my %longer;

  for (
    sort { $b->[2] <=> $a->[2] }
    map { ($i^=1) ? [ substr($_,0,1), $_, length, ] : () }
    $s =~ /((.)\2*)/g
  ) {
    $longer{$_->[0]}++ and not $i and $i = $_->[1];
  }
  $i
}

Now, does anyone else want to argue about Perl being a HLL?  ;-)

John Porter


==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org