in reply to Re: All array elements the same?
in thread All array elements the same?

Davorg: Out of curiosity, that *would* count as brute force, wouldn't it? Please explain why, or why not, if you have time. update: Would this be considered a tied hash? According to camel pg 182, perl does walk entire tied hash. (brute force)

Replies are listed 'Best First'.
RE: RE: Re: All array elements the same?
by Russ (Deacon) on Aug 01, 2000 at 21:33 UTC
    I don't think it does count as brute force. davorg may disagree, of course, but here are my two cents.

    This is brute force:
    (Note: this is deliberately not perl-idiom, just to be obtuse...)

    my @Unique; for (my $i = 0; $i != @Arr; $i++){ my $Val = $Arr[$i]; my $Found = 0; for (my $j = 0; $j != @Unique; $j++){ if ($Val == $Unique[$j]){ $Found = 1; last; } } if (not $Found){ push @Unique, $Val; } } print 'Unique values are: ', join ', ', @Unique;
    Now, in perl idiom, we do all that in one line using a hash. Of course, the specific problem we are solving here would be much simpler, even in 'C' mentality...

    I guess my definition of brute-force is "the opposite of perl idiom."

    Russ
    Brainbench 'Most Valuable Professional' for Perl

RE: RE: Re: All array elements the same?
by lhoward (Vicar) on Aug 01, 2000 at 22:17 UTC
    I would argue that this is still brute-force. Since every array element (at least till a mismatch is found) must still be examined. In this case "brute-force" = O(n) (time is proportional to the size of the list). The technique presented above has the performance characteristic that its speed is directly proportional to the list size so IMHO it is still brute-force (or at least performance-equivalent to brute-force).

    However, if you were to do something like populate your list elements into a hash as keys at the same time as you added them to a list then determining that they were all the same would be O(1), better than brute-force. However, this would slow-down your "add an element to the list" function.

      ...proving that brute force is not always a Bad Thing (tm), and that non-brute force methods can be a Bad Thing (tm), depending on your purpose and perspective.

      IMHO: Given all the above descriptions, I would consider the hash method brute force and in this case, the more efficient method.

        I agree, except in your assertion that the hash brute-force method is best.

        Performance wise the hash method will always take O(n) time, even if the first and second elements of the lists are different. An appriach like what I present below can bail-out if it finds a mismatch before examining the entire list. Therefore giving it a better average and best-case performance for lists that don't have homogeneous contents.

        sub is_list_homegeneous{ my @list=@_; my $val=shift @list; foreach(@list){ return 0 if($_ ne $val) } return 1; }
        This is not as short and cool, but does have better algorithmic performance. You could tweak-out the overhead I incurred by copying the list if you wanted the utmost in speed. My technique is probably slightly slower worse-case (a homogeneous list), because it does do slightly more work.
RE: RE: Re: All array elements the same?
by davorg (Chancellor) on Aug 01, 2000 at 22:10 UTC

    I suppose it depends what your definition of 'brute force' is. I'd say that a brute force solution would involve looping around the array checking each element and therefore this isn't brute force.

    This certainly isn't a tied hash. A tied hash is some random object whcih you can access via a hash interface. This is a real hash which just happens to contain some interesting information about an array.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
RE: RE: Re: All array elements the same?
by jjhorner (Hermit) on Aug 01, 2000 at 22:13 UTC

    Why is recursion always considered the same as "brute force".

    I'm pretty sure they aren't always the same.

    I've seen some beautiful recursive techniques that were both elegant and efficient.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/