Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Validate array of numbers in sequence

by dtr (Scribe)
on Sep 08, 2003 at 20:00 UTC ( [id://289850]=note: print w/replies, xml ) Need Help??


in reply to Validate array of numbers in sequence

An even simpler way to do this would be to add all of the numbers up. This also has the performance benefit of avoiding the sort entirely.

For example:-

my $total = 0; my $expect = 0; for(int i=0;i<@array;i++) { $total += $array[$i]; $expect += $i; Correction: $expect += $i + 1; } return 1 if ($total == $expect);

Aaarrrggghhh - sorry, ignore this post. As has been pointed out, it doesn't work.

Replies are listed 'Best First'.
Re: Re: Validate array of numbers in sequence
by Roger (Parson) on Sep 08, 2003 at 22:50 UTC
    This method does not work!

    You can not validate the number sequence by simply adding it up. Consider the following lists:

    1, 1, 4, 4 and 1, 2, 3, 4. Both add up to 10, but the first list is certainly not in sequence!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found