http://www.perlmonks.org?node_id=76206


in reply to Golf: Embedded In Order

35 chars:
sub seq{!grep{$_[0]!~/\G.*?$_/g}split//,pop}

update: And now what everyone's been waiting for:

The longest word(s) with all letters in alphabetical order:

solaris /usr/dict/words:  almost biopsy
linux    /usr/dict/words:  abhors almost begins biopsy chintz

The longest word(s) with all letters in reverse alphabetical order:

sol:  sponge
lun:  sponged wronged

The word(s) with the most letters in alphabetical order:

sol: condemnatory 8
lun: administratively antidisestablishmentarianism behavioristic
        demonstratively incomprehensibility 8
shortest: behavioristic

update oopsdate: Removed words w/ most letters in order. It was wrong, though noone remarked on it (if you were being polite, thank you). Will re-post when, if I get it right. Anyone else care to try?
update 3: maybe the above are right now. The code is:
sub seq{ my @x=split"",pop; my $s=pop; my $cc=0; for my $k (0..$#x-1) { $c = seq1($cc, $s, @x[$k..$#x]); $cc = ($c, $cc)[ $c < $cc ] } $cc } sub seq1 { my($cc, $s, @x) = @_; my $c=pos($s)=0; my $op=0; my $cy=0; for my $k (0..$#x) { $s =~ /$x[$k]/gc or next; if ($cc <= @x - $k) { # what if we skip this char? my $cx = $c + seq1( $cy, substr($s, $op), @x[$k+1 .. $#x]); $cy = ($cx, $cy)[$cx < $cy]; } $op = pos($s); $c++; } $c = ($cy, $c)[ $cy < $c ]; }
Update the last:  Actually, this one can be done in 27 chars:
sub seq{!grep$_[0]!~/$_/g,pop=~/./g}
  ...or 30 with provision for strange_chars/newlines:
sub seq{!grep$_[0]!~/\Q$_/g,pop=~/./gs}
But see my trimming of japhy's above.

  p