Reply to Re: Flagging consecutive numbers in a data set

Your name:

Reply:


Posted by Chris Newey on 05/11/05 08:34

In article <1115773243.995016.304930@g44g2000cwa.googlegroups.com>,
dohertywa@hotmail.com says...
> Hi,
>
> I am trying to mark consective numbers in a data set and get a count as
> to how many consecutive numbers exist in the a given line of data.
>
> Here is an example line:
>
> 3, 5, 7, 9, 10, 13, 14
>
> The relevant part of the script which currently checks for a
> consecutive value works in this fashion:
>
> N.B.- $cc is the consective counter and $one thru $seven are my result
> numbers.
>
> if ($two == $one + 1) {
> $cc++;
> }
> if ($three == $two + 1) {
> $cc++;
> }
> if ($four == $three + 1) {
> $cc++;
> }
> if ($five == $four + 1) {
> $cc++;
> }
> if ($six == $five + 1) {
> $cc++;
> }
> if ($seven == $six + 1) {
> $cc++;
> }
>
> Now this is all find and dandy, ugly I know but it gets the job done,
> at least in the above example, it denotes the line as having two sets
> of consective numbers.
>
> My problem results when I have a line like the following:
>
> 3, 5, 7, 8, 9, 13, 15
>
> I would like the script to tell me that there are 3 numbers in a row,
> but it tells me there are two sets of consecutive numbers; 7,8 and 8,9.
> At the same time though, I want it to still report the number of sets
> of consective numbers.
>
> Like I said I know it's ugly but if someone can offer up some guiding
> light as to the error of my ways, I would appreciate the help.
>

Try this for a giggle

==================================================================
<?php


//$arr = array(3, 5, 7, 8, 9, 13, 15);
//var_dump($arr);



$arr = array(1);
chkArray($arr);
$arr = array(1,2);
chkArray($arr);
$arr = array(1,3,2);
chkArray($arr);
$arr = array(1,2,3);
chkArray($arr);
$arr = array(1,2,3,4);
chkArray($arr);
$arr = array(1,2,9,3,4,5);
chkArray($arr);

// ===============================================
function chkArray($arr) {

$consecutiveNumberPairCount = 0;
$longestConsecutiveRun = 0;
$currentLongestConsecutiveRun = 0;

$maxIndex = count($arr) - 1;
foreach($arr as $index => $value) {

//print "$index : $value\n";
if ($index < $maxIndex) {

//print '$arr[$index] ' . $arr[$index] . ' | $arr[$index+1]
' . $arr[$index+1] . "\n";
if (($arr[$index] + 1) == $arr[$index+1]) {
$consecutiveNumberPairCount =
$consecutiveNumberPairCount + 1;
}

}

if ($index > 0) {

//print '$arr[$index] ' . $arr[$index] . ' | $arr[$index+1]
' . $arr[$index+1] . "\n";
if (($arr[$index] -1 ) == $arr[$index-1]) {
if ($currentLongestConsecutiveRun == 0) {
$currentLongestConsecutiveRun = 2;

}
else {
$currentLongestConsecutiveRun =
$currentLongestConsecutiveRun + 1;
}
if ($currentLongestConsecutiveRun >
$longestConsecutiveRun) {
$longestConsecutiveRun =
$currentLongestConsecutiveRun;
}
}
else {
//print 'current longest ' .
$currentLongestConsecutiveRun . ' : longest ' . $longestConsecutiveRun .
"\n";
$currentLongestConsecutiveRun = 0;
}
}


}
print "pairs = $consecutiveNumberPairCount\n";
print "longest consecutive = $longestConsecutiveRun\n";

}

?>



=====================================================================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация