check if there are consecutive numbers in list

amvukovic shared this question 4 years ago
Answered

I have a list like the following:

  1. nmbList={4,-2,15,3,5,56,100}


and I would like to loop through that list and check if there are consecutive numbers in the list (in above list, there are three consecutive numbers: 3,4 and 5) and, if are, to automatically count how many...

Is this possible with only GeoGebra scripting?


I've created the JS function for that:

  1. function pronadjiUzastopne(array) {
  2. var nmbOfSeq = 0;
  3. for(var i=0; i<array.length; i++) {
  4. for(var j=0; j<array.length; j++) {
  5. if(array[j]==array[i]+1) {
  6. nmbOfSeq+=1;
  7. }
  8. }
  9. }
  10. return nmbOfSeq;
  11. }

in which the returning value of the function (nmbOfSeq) is one less than the total number of consecutive numbers in the sequence because it's increased by one for each pair of consecutive numbers in the sequence...


Now, I'm working with students who are not as skilled in programming so I wonder if there are some commands by which I could come with the same result..?


Thanks in advance for your help,


Aleksandra-Maria Vuković

Best Answer
photo

Hi, try :


  1. nmbOfSeq=Sum[Join[Sequence[Sequence[nmbList(k) ≟ nmbList(l) + 1, k, 1, Length[nmbList]], l, 1, Length[nmbList]]]]

Comments (8)

photo
2

Hi, try :


  1. nmbOfSeq=Sum[Join[Sequence[Sequence[nmbList(k) ≟ nmbList(l) + 1, k, 1, Length[nmbList]], l, 1, Length[nmbList]]]]

photo
1

This is directly transferred JS code in GGB code! Fantastic! I knew for a two-dimensional array, but I did not really know how to get the number as an output value, and not a sequence ... I have never used the combination of commands Sum[Join[]] with the sequence - this solution is really very elegant one, and I learned a lot for the future applets! Thank you very much once again!

photo
1

I've only changed a little bit code because it also counts appearance of equal numbers as a part of consecutive sequence - for example, if there are numbers 8. 8 and 9 in array the output value is 2 as well as if there are 7, 8 and 9 in array... So, I've used Unique[] command in the code: I've first created new array with Unique[] command and than used that new array in the code... Once again I'm so happy - I'm working on Three in a row game on the coordinate plane and want to avoid JS as much as I can for later workshops with my students...

photo
photo
1

Works fantastic!

Thank you very much - now I have to study the GeoGebra syntax and commands you've used in the solution... I see the command Join[] that I've previously used only in the basic usage... Once again, I appreciate your help and quick response!

photo
2

Also CountIf and Zip are useful when dealing with lists


  1. Sum[Zip[CountIf[x==k+1,numList],k,numList]]

Assuming unique numbers this might work:

  1. Length[Intersection[numList,numList+1]]

photo
1

I just can't believe the simplicity of this solution!!! This is genius yet so simple and nice... Thanks!

photo
photo
1

I came upon another problem based on the error that I had in my original JS code... The var nmbOfSeq is increased by 1 each time the pair of consecutive numbers occur which means that if there are, for example, numbers 1,5,6,9,10,15 in array the output will also be 2 (the value of the nmbOfSeq) as well as if there are numbers 1,5,6,7,10,15 in the array!

As I'm creating the Three in a row game I need to find the exact sequence of three consecutive numbers, so I've came upon this solution based on the original Michel Iroir's solution:


  1. Sum[Sequence[numListUnique(k + 1) ≟ numListUnique(k) + 1 ∧ numListUnique(k + 2) ≟ numListUnique(k) + 2, k, 1, Length[numListUnique]]]

and it works perfect! The output value is 1 in this case (if there are three consecutive numbers) or, in general, the output value is 2 less than the number of consecutive numbers in array...

Now I'm going to try Zbynek's solution!

Once again thanks both of you - you're really helped me a lot!

photo
1

Zbynek, your solution really rocks except when having two pairs of consecutive numbers which don't form a sequence of consecutive numbers... This is due to my original error in JS code on which you based your solution...

Meaning, if there is numList = {2, 3, 6, 7, 12} the output will be 2 which is fine, but perhaps I didn't give enough detailed explanation at the beginning that I need to count only the sequences of 3 and more consecutive numbers... but never the pairs... Because I want to detect if three (in the game I'm developing now) points are collinear vertically, horizontally or diagonally and if their corresponding coordinates are consecutive (if so, I have three in a row...)

I need more complex procedure and I can not just use the AreCollinear[] command because I don't know in advance the indexes of the points, whose coordinates are stored in a list, which are potentially collinear nor their coordinates...

Anyway, once again - great and neat solution and a good idea which I will sooner or later use in some other code!

© 2020 International GeoGebra Institute