Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Golf: Embedded In Order

by petral (Curate)
on Apr 27, 2001 at 22:25 UTC ( #76206=note: print w/replies, xml ) Need Help??

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://76206]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-04-20 11:33 GMT
Find Nodes?
    Voting Booth?