Hans Dieter Pearcey on 15 Oct 2003 11:21:14 -0400


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

Re: Small puzzle


On Wed, Oct 15, 2003 at 11:15:55AM -0400, Hans Dieter Pearcey wrote:
> On Wed, Oct 15, 2003 at 01:34:15AM -0400, Mark Dominus wrote:
> > My function to do this is 18 lines long.  This is way too much code.
> > I am sure there must be a simpler solution.  Can anyone think of a
> > simpler solution?
> 
> I have one, but I realized after writing it that I was using the names
> of the nodes as strings ("A", "E", etc.).  Is that allowed?

Oops.  Of course, this doesn't matter.  I can just use the nodes
themselves instead of the labels thereof.

sub find_ancestor {
  my @nodes = @_;
  my %found;
  my $ancestor;
  do {
    $found{$_}++ for @nodes;
    @nodes = grep defined, map $_->parent, @nodes;
    ($ancestor) = grep { $found{$_} == @_ } keys %found;
  } until ($ancestor or not @nodes);
  return $ancestor;
}

hdp.
-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**