455,775 Members | 1,325 Online
Need help? Post your question and get tips & solutions from a community of 455,775 IT Pros & Developers. It's quick & easy.

# Flagging consecutive numbers in a data set

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a Adam wrote: 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

 P: n/a Adam wrote: 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

 P: n/a 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 ================================================== ================ \$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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.