5

Calculate the number of elements to evaluate

 2 years ago
source link: https://www.codesd.com/item/calculate-the-number-of-elements-to-evaluate.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Calculate the number of elements to evaluate

advertisements

This is a logical Question which is not bound to any specific language.

Does someone know a way on how to achieve the following?

I do have an array of which I can always evaluate a max of 10 elements. So if the size of the array grows higher than 10, I need to determine which element I can evaluate - equally distributed.

Example:

[87, 34, 65, 23, 98, 45, 21, 54, 65, 235, 4, 32, 98, 42, 17]

The bold ones could be the ones considered.

And I do want to always evaluate the highest amount of possibles:

If the length of the array is smaller than 10 THEN evaluate every item. If the length is higher THEN evaluate 10 out of the array.

If length is 20 -> every 2nd item should be evaluated

If length is 100 -> every 10th item should be evaluated

But what if length is 13 or 27?


You can use a function which remaps your index 0..9 to an evenly distributed index form 0..n-1, e.g.

int map_index(int i,  // index: 0..9
              int n)  // no of elements in array
{
    return i * (n - 1) / (10 - 1);
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK