Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Finding largest common subset in lists?

by anjiro (Beadle)
on Jun 06, 2003 at 04:34 UTC ( [id://263586]=note: print w/replies, xml ) Need Help??


in reply to Re: Finding largest common subset in lists?
in thread Finding largest common subset in lists?

So close! It works almost all the time, but this test case fails:
my @a = qw( a b c d e f g h i j k l m n o p q r s t u v w x y z ); my @b = qw( a b c X f g h X l m n X j k a b c d );
Running on this, I get a b c as the result instead of a b c d. I'm looking to see if I can figure out why now, but I figured I'd post in case someone's faster than me (likely).

Replies are listed 'Best First'.
Re: Re: Re: Finding largest common subset in lists?
by Jenda (Abbot) on Jun 06, 2003 at 09:32 UTC

    I'm stupid, stupid, stupid. There should be:

    .... if ($end > @result) { @result = @a[0..$end-1]; } ...
    instead of
    ... if (--$end > @result) { @result = @a[0..$end]; } ...
    The way I have it I'd only update the @result if the newly found list was longer by at least 2 items. The usual off-by-one error :-(

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://263586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found