473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Flagging consecutive numbers in a data set

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.

TIA,
Adam

Jul 17 '05 #1
8 2997
Theres no logical way that 3, 5, 7, 9, 10, 13, 14 can return 2, while
3, 5, 7, 8, 9, 13, 15 returns 3. If the second one is said to have 3
consecutive numbers, then the first one has 4. If the first one is
said to have 2, then the second one also has 2. It depends on how you
look at it, but I can't see any logical way of having the first one
return 2 and the second one return 3.

Jul 17 '05 #2
Theres no logical way that 3, 5, 7, 9, 10, 13, 14 can return 2, while
3, 5, 7, 8, 9, 13, 15 returns 3. If the second one is said to have 3
consecutive numbers, then the first one has 4. If the first one is
said to have 2, then the second one also has 2. It depends on how you
look at it, but I can't see any logical way of having the first one
return 2 and the second one return 3.

Jul 17 '05 #3
DJ Craig wrote:
Theres no logical way that 3, 5, 7, 9, 10, 13, 14 can return 2, while
3, 5, 7, 8, 9, 13, 15 returns 3. If the second one is said to have 3
consecutive numbers, then the first one has 4. If the first one is
said to have 2, then the second one also has 2. It depends on how you
look at it, but I can't see any logical way of having the first one
return 2 and the second one return 3.


I can.

In the first one, 9 and 10 are consecutive. So are 13 and 14 - but they
are not consecutive with 9 and 10. So you have 2 sets of consecutive
numbers, the longer of which is 2.

In the second set, 7, 8 and 9 are consecutive, but there are no other
consecutive numbers. The only set is 3 elements long.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #4
DJ Craig wrote:
Theres no logical way that 3, 5, 7, 9, 10, 13, 14 can return 2, while
3, 5, 7, 8, 9, 13, 15 returns 3. If the second one is said to have 3
consecutive numbers, then the first one has 4. If the first one is
said to have 2, then the second one also has 2. It depends on how you
look at it, but I can't see any logical way of having the first one
return 2 and the second one return 3.


I can.

In the first one, 9 and 10 are consecutive. So are 13 and 14 - but they
are not consecutive with 9 and 10. So you have 2 sets of consecutive
numbers, the longer of which is 2.

In the second set, 7, 8 and 9 are consecutive, but there are no other
consecutive numbers. The only set is 3 elements long.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #5
Adam wrote:

<snip likely homework assignment>

Hmmm, now what business problem are you trying to solve here? None? This
is perhaps something more abstract? Perhaps a homework assignment?

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #6
Adam wrote:

<snip likely homework assignment>

Hmmm, now what business problem are you trying to solve here? None? This
is perhaps something more abstract? Perhaps a homework assignment?

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #7
In article <11**********************@g44g2000cwa.googlegroups .com>,
do*******@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";

}

?>

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

Jul 17 '05 #8
Here's a little function that does what you want. It returns an array
of lengths of consecutive sequences. For [3, 5, 7, 8, 9, 13, 15] it
yields [3]. For [3, 5, 7, 9, 10, 13, 14], you get [2, 2]. I'll leave it
to you to figure out how it works ;-)

function seq($a) {
$b = range($a[count($a) - 1], $a[1]);
$c = array_map(create_function('$a,$b', 'return $a + $b;'), $a, $b);
$d = array_count_values($c);
$e = array_filter($d, create_function('$n', 'return $n != 1;'));
return array_values($e);
}

print_r(seq(array(3, 5, 7, 8, 9, 13, 15)));
print_r(seq(array(3, 5, 7, 9, 10, 13, 14)));

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mr. Magoo | last post by:
I'm working with imaplib. I'm trying to flag (or move or copy - anything that takes a message_set as an argument) a bunch of messages with one command and am having trouble. === def...
10
by: ChrisD | last post by:
I'm trying extract a count of consecutive numbers, or "unbroken" years in this case, at any particular given time. For example (simplified): CREATE TABLE #Customers ( CustNo INT, YearNo...
0
by: Dennis Ruppert | last post by:
Greetings This should be easy, but I am stuck! I have a table that I import from another program. There are 25 fields, but I only need to use 3 of them for what I need to do. After I import...
3
by: jr | last post by:
A perplexing one this. I Am trying to design a query or series of queries which will firstly identify a condition. If column A value is less than column B value make column C value =1 , else...
1
by: Kosmos | last post by:
Hi, I really need some help here because I am not a programmer but I took on the task because I'm an intern and wanted to rise up to the challenge...and all that crap...anyways over the past few days...
9
by: boliches | last post by:
I have a seperate table to generate consecutive numbers. Using "Dmax" to find the largest number to increment . My problem is that I want the number to begin at 1000 at the start of each month,...
7
by: Sharkie | last post by:
I need a regular expression which will evaluate to false if number of consecutive characters (non-whitespace) exceeds certain number (10 in this example). For example, I have this function: ...
8
by: help2008 | last post by:
Hi I have been doing this working on an assignment for the last week and have stumbled across a part which I cant get my head around. I was hoping that someone could explain what I am missing. I...
2
bwesenberg
by: bwesenberg | last post by:
I am not sure how to explain this so I will try to the best of my ability. I need to be able to produce 1000 labels from access that are based on consecutive numbers that Access will produce. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.