Home > Blockchain >  Find the 'step' value from an arbitrary array that is guaranteed to be in a certain '
Find the 'step' value from an arbitrary array that is guaranteed to be in a certain '

Time:06-11

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.

CodePudding user response:

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]));

CodePudding user response:

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.

CodePudding user response:

I think that your solution is the best for this problem

CodePudding user response:

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

CodePudding user response:

This function:

function arrayStep(arrayValue) {
  let step = arrayValue[1] - arrayValue[0];
  return step;
}

ps: if I understand correctly, the answer of the first example would be 0.1

  • Related