From: Purl Gurl on
Feyruz wrote:

> So you say that Perl also uses a recursive function to find the element
> with the maximum numerical value in an array ?

I wrote previously,

"Perl's sort function is a default recursive function."

I can write that statement in a number of different ways
if doing so will help your understanding.


> I find this interesting. Because i was thinking that a recursive

"Because i[sic] was thinking...."

I would rather learn of what you are currently thinking.

Purl Gurl
From: Feyruz on
Hi Anno Siegel,
The question comes from a book. I wanted to see if someone had better
or more creative solutions than I. I see that you are absolutely good
in it. So that causes the run-time to fall to O(log n). i find this
impressive
Thanks for your interesting solution.
F.

From: Feyruz on
Wait a second, each half takes also O(n) doesnt it? i mean , are you
sure that it has any advantages?

From: Purl Gurl on
anno wrote:

> sub max {
> die "can't take max of no elements" unless @_;
> if ( @_ == 1 ) {
> return shift;
> }
> else {
> my $left = max( @_[ 0 .. @_/2 -1]);
> my $right = max( @_[ @_/2 .. $#_]);
> return $left > $right ? $left : $right;
> }
> }


Your code is very imaginative! I like your code.

I ran some tests. Your code works perfectly, including
returning a wrong result for an array containing negative
numbers and alphabet letters. Do not take that wrong;
it is not your code is broken but rather perl core's notion
of maximum numerical value for mixed array content, or
at least I think that is what is happening. It "appears"
perl core sorts and places negative numbers before
letters, for sorting order. However, this is not always
the case; test my example arrays.

Run these arrays for interesting results. Maybe you better
know what causes differences in returns. I am guessing
perl core has difficulties determining numerical value
for some circumstances.

@Array = qw (-10 -9 -8 -7 -6 0 -5 -4 -3 -2 -1 Z Y X W);
@Array = qw (Z Y X W -10 -9 -8 -7 -6 0 -5 -4 -3 -2 -1);

Very nice, Anno.

Purl Gurl
From: Feyruz on
Hi Perl Gurl,
The sub is supposed to recieve only integer values. I dont understand
why you run tests on it using letters.
F.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Perl - Get Two Decimals
Next: How to declare a variable???