find the lowest number?

2002-08-30 Thread David Samuelsson (PAC)


i get an array conataining a lot off numbers like this:

567
551
540
540
540
533
531
526
493
476
468
429
421
520

how do i find the lowest or the higest number in this series?

//Dave

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




AW: find the lowest number?

2002-08-30 Thread Angerstein

@numbers = (44, 55, 38, 10 , 5, 7);
@numbers = sort { $a=$b } @number;
print $numbers[0]; # the lowest

 -Ursprüngliche Nachricht-
 Von: David Samuelsson (PAC) [mailto:[EMAIL PROTECTED]]
 Gesendet am: Freitag, 30. August 2002 10:23
 An: '[EMAIL PROTECTED]'
 Betreff: find the lowest number?


 i get an array conataining a lot off numbers like this:

 567
 551
 540
 540
 540
 533
 531
 526
 493
 476
 468
 429
 421
 520

 how do i find the lowest or the higest number in this series?

 //Dave

 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




AW: find the lowest number?

2002-08-30 Thread Angerstein

@numbers[$#numbers]; # highest
@numbers[-1]; # highest
 -Ursprüngliche Nachricht-
 Von: Angerstein [mailto:[EMAIL PROTECTED]]
 Gesendet am: Freitag, 30. August 2002 10:34
 An: [EMAIL PROTECTED]
 Betreff: AW: find the lowest number?

 @numbers = (44, 55, 38, 10 , 5, 7);
 @numbers = sort { $a=$b } @number;
 print $numbers[0]; # the lowest

  -Ursprüngliche Nachricht-
  Von: David Samuelsson (PAC) [mailto:[EMAIL PROTECTED]]
  Gesendet am: Freitag, 30. August 2002 10:23
  An: '[EMAIL PROTECTED]'
  Betreff: find the lowest number?
 
 
  i get an array conataining a lot off numbers like this:
 
  567
  551
  540
  540
  540
  533
  531
  526
  493
  476
  468
  429
  421
  520
 
  how do i find the lowest or the higest number in this series?
 
  //Dave
 
  --
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
 
 


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




AW: find the lowest number?

2002-08-30 Thread Angerstein

sort will sort like windows does with filename if you do this that way.
example
sort do:
1, 16, 20, 3, 35, 4, 400
not:
1,3,4,16,20,35,400

 -Ursprüngliche Nachricht-
 Von: Ramprasad A Padmanabhan [mailto:[EMAIL PROTECTED]]
 Gesendet am: Freitag, 30. August 2002 10:39
 An: [EMAIL PROTECTED]
 Betreff: Re: find the lowest number?

 David Samuelsson wrote:
  i get an array conataining a lot off numbers like this:
 
  567
  551
  540
  540
  540
  533
  531
  526
  493
  476
  468
  429
  421
  520
 
  how do i find the lowest or the higest number in this series?
 
  //Dave

 @a = qw(121 23 31 4 11);

 print LOWEST  . (reverse sort @a )[0];



 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On Fri, 30 Aug 2002, Ramprasad A Padmanabhan wrote:

 
 @a = qw(121 23 31 4 11);
 
 print LOWEST  . (reverse sort @a )[0];

By default sort does a lexical sort, this will not work
Correct way to do this

my @list = qw(121 23 31 4 11);
print LOWEST: , (sort {$a = $b} @list)[0];



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Felix Geerinckx

on Fri, 30 Aug 2002 08:22:56 GMT, [EMAIL PROTECTED] 
(David Samuelsson) wrote:

 i get an array conataining a lot off numbers like this:
 
 567

[snipped]

 520
 
 how do i find the lowest or the higest number in this series?

There is no need to sort the full array (as others suggested) to just 
find the lowest and highest number when an O(n) operation will 
suffice:

my ($min, $max);
$min = $max = $array[0];
for (1..@array-1) {
$min = $array[$_] if $array[$_]  $min;
$max = $array[$_] if $array[$_]  $max;
}
print min = $min, max = $max\n;

-- 
felix
  

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On 30 Aug 2002, Felix Geerinckx wrote:

 There is no need to sort the full array (as others suggested) to just 
 find the lowest and highest number when an O(n) operation will 
 suffice:

Precisely the comment that I had a few months back for a similar question.
Read through this thread
http://archive.develooper.com/beginners%40perl.org/msg22716.html

 
 my ($min, $max);
 $min = $max = $array[0];
 for (1..@array-1) {
 $min = $array[$_] if $array[$_]  $min;
 $max = $array[$_] if $array[$_]  $max;
 }
 print min = $min, max = $max\n;

For further proof here is a benchmarking result
#!/usr/local/bin/perl -w
use strict;
use Benchmark;

my @arr = qw(34 12 23 45 11 91 32);
sub using_sort {
my $min = (sort {$a = $b} @arr)[0];
my $max = (reverse sort {$a = $b} @arr)[0];
}

sub using_for {
my ($min, $max);
$min = $max = $arr[0];
for (1..@arr-1) {
$min = $arr[$_] if $arr[$_]  $min;
$max = $arr[$_] if $arr[$_]  $max;
}
}

timethese (100, { using_sort = \using_sort,
using_for = \using_for });

=cut
Benchmark: timing 100 iterations of using_for, using_sort...
 using_for: 11 wallclock secs (11.60 usr +  0.04 sys = 11.64 CPU) @ 
85910.65/s (n=100)
using_sort:  8 wallclock secs ( 7.63 usr +  0.06 sys =  7.69 CPU) @ 
130039.01/s (n=100)

Note that even with two sorts and a reverse the using_sort subroutine 
is faster in terms of speed.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




AW: find the lowest number?

2002-08-30 Thread Angerstein

fake! you dont need to sort the array to times... because u know where the
lowest hand the higest is.
Less of the half of 11 wallclocks would be what i expect.

 -Ursprüngliche Nachricht-
 Von: Sudarshan Raghavan [mailto:[EMAIL PROTECTED]]
 Gesendet am: Samstag, 31. August 2002 04:11
 An: Perl beginners
 Betreff: Re: find the lowest number?

 On 30 Aug 2002, Felix Geerinckx wrote:

  There is no need to sort the full array (as others suggested) to just
  find the lowest and highest number when an O(n) operation will
  suffice:

 Precisely the comment that I had a few months back for a similar question.
 Read through this thread
 http://archive.develooper.com/beginners%40perl.org/msg22716.html

 
  my ($min, $max);
  $min = $max = $array[0];
  for (1..@array-1) {
  $min = $array[$_] if $array[$_]  $min;
  $max = $array[$_] if $array[$_]  $max;
  }
  print min = $min, max = $max\n;

 For further proof here is a benchmarking result
 #!/usr/local/bin/perl -w
 use strict;
 use Benchmark;

 my @arr = qw(34 12 23 45 11 91 32);
 sub using_sort {
 my $min = (sort {$a = $b} @arr)[0];
 my $max = (reverse sort {$a = $b} @arr)[0];
 }

 sub using_for {
 my ($min, $max);
 $min = $max = $arr[0];
 for (1..@arr-1) {
   $min = $arr[$_] if $arr[$_]  $min;
   $max = $arr[$_] if $arr[$_]  $max;
   }
 }

 timethese (100, { using_sort = \using_sort,
 using_for = \using_for });

 =cut
 Benchmark: timing 100 iterations of using_for, using_sort...
  using_for: 11 wallclock secs (11.60 usr +  0.04 sys = 11.64 CPU) @
 85910.65/s (n=100)
 using_sort:  8 wallclock secs ( 7.63 usr +  0.06 sys =  7.69 CPU) @
 130039.01/s (n=100)

 Note that even with two sorts and a reverse the using_sort subroutine
 is faster in terms of speed.


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: AW: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On Fri, 30 Aug 2002, Angerstein wrote:

 fake! you dont need to sort the array to times... because u know where the
 lowest hand the higest is.

Yes I do know that, you don't have to sort the array two times. 
 Less of the half of 11 wallclocks would be what i expect.

I think you should read through the mail properly before crying fake. The 
11 seconds is for the subroutine using the foreach loop.

 
  Benchmark: timing 100 iterations of using_for, using_sort...
   using_for: 11 wallclock secs (11.60 usr +  0.04 sys = 11.64 CPU) @
  85910.65/s (n=100)
  using_sort:  8 wallclock secs ( 7.63 usr +  0.06 sys =  7.69 CPU) @
  130039.01/s (n=100)


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




AW: AW: find the lowest number?

2002-08-30 Thread Angerstein

ok, i see.
That´s what i thought.

 -Ursprüngliche Nachricht-
 Von: Sudarshan Raghavan [mailto:[EMAIL PROTECTED]]
 Gesendet am: Samstag, 31. August 2002 04:33
 An: Perl beginners
 Betreff: Re: AW: find the lowest number?

 On Fri, 30 Aug 2002, Angerstein wrote:

  fake! you dont need to sort the array to times... because u
 know where the
  lowest hand the higest is.

 Yes I do know that, you don't have to sort the array two times.
  Less of the half of 11 wallclocks would be what i expect.

 I think you should read through the mail properly before crying fake. The
 11 seconds is for the subroutine using the foreach loop.

 
   Benchmark: timing 100 iterations of using_for, using_sort...
using_for: 11 wallclock secs (11.60 usr +  0.04 sys = 11.64 CPU) @
   85910.65/s (n=100)
   using_sort:  8 wallclock secs ( 7.63 usr +  0.06 sys =  7.69 CPU) @
   130039.01/s (n=100)


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: AW: find the lowest number?

2002-08-30 Thread Dharmendra Rai


my $ret_val;

my $i;

if ($#your_array  0){

## do appropriate thing

}

elsif ($#your_array == 0){ ## return the only value it has}

## following is the ELSE part

for (my $i = 0; i  $#your_array; ++i)

{

$ret_val = your_array[i+1];

$ret_val = your_array[i] if (your_array[i]  your_array[i+1]);

}

## $ret_val contains the max number. 




-
Get a bigger mailbox -- choose a size that fits your needs.



Re: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On Sat, 31 Aug 2002, Sudarshan Raghavan wrote:

 For further proof here is a benchmarking result
 #!/usr/local/bin/perl -w
 use strict;
 use Benchmark;
 
 my @arr = qw(34 12 23 45 11 91 32);
 sub using_sort {
   my $min = (sort {$a = $b} @arr)[0];
   my $max = (reverse sort {$a = $b} @arr)[0];

My bad!! this should be
my ($min, $max) = (sort {$a = $b} @arr)[0, -1];

 }
 
 sub using_for {
 my ($min, $max);
 $min = $max = $arr[0];
 for (1..@arr-1) {
   $min = $arr[$_] if $arr[$_]  $min;
   $max = $arr[$_] if $arr[$_]  $max;
   }
 }
 
 timethese (100, { using_sort = \using_sort,
 using_for = \using_for });

With these changes the benchmarking gets even better
Benchmark: timing 100 iterations of using_for, using_sort...
 using_for: 14 wallclock secs (11.89 usr +  0.06 sys = 11.95 CPU) @ 
83682.01/s (n=100)
using_sort:  4 wallclock secs ( 4.13 usr +  0.02 sys =  4.15 CPU) @ 
240963.86/s (n=100)
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: AW: find the lowest number?

2002-08-30 Thread Dharmendra Rai

why use sorting and other algos which have non-linear time-complexity. traverse the 
array once and get the max and min



-
Get a bigger mailbox -- choose a size that fits your needs.



Re: AW: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On Fri, 30 Aug 2002, Dharmendra Rai wrote:

The list FAQ requests folks to snip uneccessary lines before replying to 
mails. But surely the entire mail cannot be uneccessary. Please leave some 
parts of it so that other's in this list know what you are replying to.

 
 my $ret_val;
 
 my $i;
 
 if ($#your_array  0){
 
 ## do appropriate thing
 
 }
 
 elsif ($#your_array == 0){ ## return the only value it has}
 
 ## following is the ELSE part
 
 for (my $i = 0; i  $#your_array; ++i)
 
 {
 
 $ret_val = your_array[i+1];

Do you test your code, this is a syntax error
$your_array[i+1];

 
 $ret_val = your_array[i] if (your_array[i]  your_array[i+1]);

Same here. Use the ternary operator for this
$ret_val = ($your_array[i]  $your_array[i+1]) ? $your_array[i] : $your_array[i+1]; 
 
 }
 
 ## $ret_val contains the max number. 

This is C style coding, Felix had already posted a soln on similar lines.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Felix Geerinckx

on Sat, 31 Aug 2002 02:23:20 GMT, [EMAIL PROTECTED] (Sudarshan
Raghavan) wrote: 

 On 30 Aug 2002, Felix Geerinckx wrote:
 
 There is no need to sort the full array (as others suggested) to
 just find the lowest and highest number when an O(n) operation
 will suffice:
 
 Precisely the comment that I had a few months back for a similar
 question. Read through this thread
 http://archive.develooper.com/beginners%40perl.org/msg22716.html

Thanks for this reference (from a time before I discovered the perl
beginners list). 

 Benchmark: timing 100 iterations of using_for, using_sort...
  using_for: 11 wallclock secs (11.60 usr +  0.04 sys = 11.64 CPU)
  @ 85910.65/s (n=100)
  using_sort:  8 wallclock secs ( 7.63 usr +  0.06 sys =  7.69 CPU)
  @ 130039.01/s (n=100)

This if for a seven-element array. 

I did some testing myself for larger array sizes (times are in 
CPU seconds):

  Size   Loop  Sort
  1000   0.00  0.01
 1   0.02  0.08
10   0.13  1.03
20   0.24  2.18
30   0.37  3.35

So if you have more than 1000 numbers, the loop method will already
be faster. In addition, it has the advantage that you don't need all
the numbers to be present in memory, since you can calculate min/max
while you read the numbers from file. 

For small array sizes, it doesn't matter which method you use, since 
the cpu time will be near zero anyway. For larger sizes it does 
matter, and the loop method is a clear winner.
Therefor, imho, your suggestion in March (and mine now :-) to use a 
loop, is *always* preferable.

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On 30 Aug 2002, Felix Geerinckx wrote:

 This if for a seven-element array. 
 
 I did some testing myself for larger array sizes (times are in 
 CPU seconds):
 
   Size   Loop  Sort
   1000   0.00  0.01
  1   0.02  0.08
 10   0.13  1.03
 20   0.24  2.18
 30   0.37  3.35
 
 So if you have more than 1000 numbers, the loop method will already
 be faster. In addition, it has the advantage that you don't need all
 the numbers to be present in memory, since you can calculate min/max
 while you read the numbers from file. 
 
 For small array sizes, it doesn't matter which method you use, since 
 the cpu time will be near zero anyway. For larger sizes it does 
 matter, and the loop method is a clear winner.
 Therefor, imho, your suggestion in March (and mine now :-) to use a 
 loop, is *always* preferable.

I thought on similar lines too when that thread was on, but I convinced 
myself saying that an unsorted array of 1000 or more elements in your 
code is a poor design to start with :-)





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Felix Geerinckx

on Sat, 31 Aug 2002 03:51:39 GMT, [EMAIL PROTECTED] (Sudarshan
Raghavan) wrote: 

 I thought on similar lines too when that thread was on, but I
 convinced myself saying that an unsorted array of 1000 or more
 elements in your code is a poor design to start with :-)

I beg to differ. From perldoc perldata (my emphasis):

Normal arrays are *ordered* lists of scalars
indexed by number, starting with 0 and with negative
subscripts counting from the end.

One of the main features of arrays (compared to hashes) is that you 
have full control over the order in which the elements are inserted. 
Many algorithms rely on this order (look e.g. at the GD::Graph module).

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Sudarshan Raghavan

On 30 Aug 2002, Felix Geerinckx wrote:

 on Sat, 31 Aug 2002 03:51:39 GMT, [EMAIL PROTECTED] (Sudarshan
 Raghavan) wrote: 
 
  I thought on similar lines too when that thread was on, but I
  convinced myself saying that an unsorted array of 1000 or more
  elements in your code is a poor design to start with :-)
 
 I beg to differ. From perldoc perldata (my emphasis):
 
 Normal arrays are *ordered* lists of scalars
 indexed by number, starting with 0 and with negative
 subscripts counting from the end.
 
 One of the main features of arrays (compared to hashes) is that you 
 have full control over the order in which the elements are inserted. 
 Many algorithms rely on this order (look e.g. at the GD::Graph module).

My statement was far too general, what I should have said was to have a 
unsorted array of 1000 or more elements when your problem requires finding 
the max or min of that list is not a good design. 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Felix Geerinckx

on Sat, 31 Aug 2002 04:30:34 GMT, [EMAIL PROTECTED] (Sudarshan
Raghavan) wrote: 

 My statement was far too general, what I should have said was to
 have a unsorted array of 1000 or more elements when your problem
 requires finding the max or min of that list is not a good design.

Again, I beg to differ. I'll use the same example (GD::Graph) to 
explain myself.

Say you want to graph a mathematical function

y = f(x)

for 0 = x = 1000.

GD::Graph wants two arrayrefs, so you do:

my ($x, $y);
for (0..1000) {
$x-[$_] = $_;
$y-[$_] = f($_);
}

To have a nice looking graph, you want to have the top/bottom 
coincide with the max/min value of your function, so you need to 
calculate ymax and ymin, which, of course, you do in the same for 
loop that generates the (x,y) pairs.

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Janek Schleicher

David Samuelsson wrote at Fri, 30 Aug 2002 10:22:56 +0200:

 
 i get an array conataining a lot off numbers like this:
 
 567
 551
 540
 540
 540
 533
 531
 526
 493
 476
 468
 429
 421
 520
 
 how do i find the lowest or the higest number in this series?

There's also a module doing such a job for you:

use List::Util qw/min max/;
my $min = min @array;
my $max = max @array;


(Of course, that's not the quickest solution,
 as it is an 2 * O(n) algorithm,
 and perhaps also a sort is quicker for little lists,
 as it is implemented directly in Perl
- But it's a very simple and readable solution).


Greetings,
Janek


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: find the lowest number?

2002-08-30 Thread Tagore Smith

Felix Geerinckx wrote:
snip
 For small array sizes, it doesn't matter which method you use, since
 the cpu time will be near zero anyway. For larger sizes it does
 matter, and the loop method is a clear winner.
 Therefor, imho, your suggestion in March (and mine now :-) to use a
 loop, is *always* preferable.

*Always* is a very strong word :). For small array sizes the method may make
a difference if you're going to be performing the operation many times.
Sudarshan's banchmark called sort twice and reverse once when it only needed
to call sort once, and it was still much faster than the linear scan for an
array with seven elements. If you had an application that spent most of its
time finding the minimum and maximum element of small collections the sort
method would be preferable. It's also idiomatic (didn't Randall Schwartz
post about this once saying it was an example in the Llama book?) and imho
quite readable.

There are other situations in which you might prefer a log n to a linear
algorithm for this kind of thing. If you need to repeatedly find minimum and
maximum elements of a collection over a period in which insertions and
deletions are being performed you might want to keep the collection sorted.
Likewise if you collect the elements over time and want to amortize the cost
of the operation. If your data is too large to fit in memory a log n
algorithm may beat a linear algorithm badly for all reasonable values of n
if the linear algorithm uses a lot of disk seeks. Without knowing the
details of the application it's not possible to know what algorithm is best.

I suspect that a lot of the people who ask this question are dealing with
small arrays, in which case the sort solution makes sense. It's also kind of
cool :). But the caveat about large arrays _is_ important- don't want people
using it on an array with fifty-million elements! (Of course if you're
trying to maximize performance working with large datasets you might be best
off using C or Lisp anyway, or using an RDBMS to do your heavy lifting- and
hopefully you're not dealing with these kinds of datasets if you're at the
stage where you're first learning how to find the maximum element of an
array).

This (how do I find the greatest/lowest element?) comes up every few months.
Maybe there should be a list faq that covers this and other
questions that get asked a lot and aren't in the Perl faq :).

Tagore Smith



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]