K
K
Klavikus2018-12-22 22:41:51
Python
Klavikus, 2018-12-22 22:41:51

How to sort a one-dimensional array of points, taking into account the distance between them?

There is a set of points of the following form:
source_list =
I want to get the following:
result_list =
Conditions:
max_D = 9
K = 4
If the distance between the elements is less than the max_D parameter, and such elements are in a row K times (and more), then these elements are kept, and the rest are discarded.
What is the best way to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-12-22
@Klavikus

var sourceList = [11, 14, 15, 17, 27, 28, 32, 52, 53, 54, 55, 56, 57, 75, 90, 97];
   sourceList.sort();

   var max_d = 9, K = 4;
   
   var result = [];
   var sequence = [sourceList[0] - max_d];

   sourceList.forEach(num => {
      if(num - sequence[sequence.length - 1] >= max_d) {
         if(sequence.length >= K) result = sequence;
         sequence = [];
      }
      sequence.push(num);
   });
   if(sequence.length >= K) result = sequence;

   console.log(result);

S
Stalker_RED, 2018-12-22
@Stalker_RED

1. Sort
2. Walk through the entire list, recording for each continuous piece the beginning, end, and number of elements.
3. Find the piece with the largest quantity, or the largest length, or whatever you need.
4. Throw away the excess.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question