Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Find the 'step' value from an arbitrary array that is guaranteed to be in a certain 'step' amount

I've looked around and have not found any answers for this. The problem seems pretty simple at first.

Let's say we have an array like this. We can assume that the array will ALWAYS have some equal thing it counts up by (each element has the same difference between the elements adjacent to it).

[1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0]

The answer to above would be 0.1.

Another example might be an array like this.

[0.2,0.4,0.6,0.8,1.0,1.2,1.4]

This answer would be 0.2

One possible solution is I could read the first and second parameters of the array and capture the difference and that will be my 'step'. Is this the best way to go about doing this? Is there any downsides to this approach?

if the array was counting downward like this:

[0.8,0.6,0.4,0.2], then how could I extract the step successfully?

In this case I need the answer -0.2, my answer would not work, and taking the absolute value would give me 0.2 not -0.2.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You don't need any special-case logic for the case of counting downward. In your second example, you simply subtract the first term from the second, 0.6 - 0.8, and get the correct answer: -0.2. This is similar to how in your first example, you subtracted the first term from the second, 0.4 - 0.2 and got the correct answer.

To convince you that this works, consider that your array starts with a real number n, and increases by a real number k each step:

[n, n + k, n + k*2, n + k*3, ...]

Then you can subtract the first two terms:

(n + k) - n

to get

k

The sign of k doesn't matter.

about 4 years ago · Juan Pablo Isaza Relatório

0

This is the same as taking the common difference of an arithmetic progression in mathematics. You can just subtract a term from the previous term, and you'll get the difference. Here is how the array would look in general terms:

[a, a + d, a + 2d, a + 3d, ...]

So from this you can easily see that if you perform (a + d) - (a), you'll get d. Subtracting any element from the previous element will yield the same result. So the simplest way to do this would be:

const getStep = array => (array[1] - array[0]);

Demonstration using your three provided arrays:

const getStep = array => (array[1] - array[0]);

console.log(getStep([1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0]));
console.log(getStep([0.2,0.4,0.6,0.8,1.0,1.2,1.4]));
console.log(getStep([0.8,0.6,0.4,0.2]));

Note that the above values may be inaccurate as they're floating point numbers (Floating point math is broken) but you can just round it to the nearest x-th decimal place (here's a solution with a 1/10th rounding, for instance):

const getStep = array => Math.round((array[1] - array[0]) * 10) / 10;

console.log(getStep([1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0]));
console.log(getStep([0.2,0.4,0.6,0.8,1.0,1.2,1.4]));
console.log(getStep([0.8,0.6,0.4,0.2]));

about 4 years ago · Juan Pablo Isaza Relatório

0

This in math is called arithmetic progression. If you guarantee that the sequence have the same step, you surely can just take the difference between the 2 first numbers of the sequence.

when you count downwards your sequence is decrescent, so, your step is negative. There is no possible way to get this sequence downwards without a negative step

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda