Find sequence in integer value

Asked

Viewed 47 times

0

I came to the following algorithm to return true if there is an incremental numeric sequence pattern +1

 function sequenceSearch(vector) {
          let pos = 0;
          let found = 0;
          for (pos = 0; pos <= vector.length - 1; pos++) {
            if (vector[pos] + 1 === vector[pos + 1]) {
              found++;
            }
          }
          return found == 3 ? true : false;
        }

However, in the function call need to pass an int value and not an array, as required in the represented algorithm. I wonder if there is a way to transform an integer value, example: int value = 1234, in an array to be evaluated by the algorithm. ( Or some other more practical alternative to find the pattern)

  • 1

    Take the rest of the number division consecutively by 10, put it in your array and divide the number by 10 until it is <= 0. Your array will be with the digits in reverse order but you can easily treat this in your test.

2 answers

0

You can turn the number into a string and split it, creating an array... I created a function that takes an array and converts it to Int (it’s really important that they all be numbers or create a treatment for this), this function returns an array of integers that can be played inside its function.

I changed a little of its function too, now it will serve for any numerical difference. Problem in the case of the number '246810', because it separates the '10'. If you do not want this just use the one you already have.

Follow the code below:

function sequenceSearch(vector) {
    let pos = 0;
    let found = 0;
    let dif = vector[1] - vector[0]; // variavel de diferença
    for (pos = 0; pos <= vector.length - 1; pos++) {
      if (vector[pos] + dif === vector[pos + 1]) {
        found++;
      }
    }
    return found == vector.length -1 ? true : false; // verificando pelo tamanho do array menos 1, agora pode receber array de qualquer tamanho
  }

//a nova funcao que eu criei para converter o array
function transformaArray (array) {
    array.forEach((e, i, a) => {
        a[i] = parseInt(e, 10);
    });
    return array;
}

let value = 1234;

//um exemplo de como chamar as funcoes
console.log(sequenceSearch(transformaArray(value.toString().split('')))); //retornando true

Test was done with values '1234', '123456', '369' and '2468'. All returned 'true'

Test done with values '1258', '4789' and '2458'. All returned 'false'.

I hope I’ve helped.

  • 2

    But then, I would have an array of strings, which I might not be able to accomplish the sum in sequence, because the values would concatenate.

  • Good observation. I changed the answer. See if it will meet you.

0


If the vector parameter is not array it will be converted to array

function sequenceSearch(vector) {
    vector = Array.isArray(vector) ? vector : vector.toString().split('').map(Number);
    let pos = 0;
    let found = 0;
    for (pos = 0; pos <= vector.length - 1; pos++) {
        if (vector[pos] + 1 === vector[pos + 1]) {
            found++;
        }
    }
    return found == 3 ? true : false;
}

var value = 1234;

console.log(sequenceSearch(value));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.