Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Consecutive number checking

by beable (Friar)
on Jul 04, 2004 at 06:15 UTC ( [id://371672]=note: print w/replies, xml ) Need Help??


in reply to Consecutive number checking

Use a hash.
#!/usr/bin/perl use strict; use warnings; my @array = (1,5,6,7,8,55,63,77,89,103,104,105,106,200,215,554); my %free_numbers = map {$_ => 1} @array; for my $number (@array) { my $ok = 1; for (my $i = 0; $i < 4; $i++) { if (! exists $free_numbers{$number + $i}) { $ok = 0; last; } } if ($ok) { print "found consecutive numbers: "; for (my $i = 0; $i < 4; $i++) { print $number + $i, " "; delete $free_numbers{$number + $i}; } print "\n"; } } __END__ found consecutive numbers: 5 6 7 8 found consecutive numbers: 103 104 105 106

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found