I'm trying to figure out how to handle a specific task, but I'm running into some obstacles.
When adding numbers to clusters, a number is considered to belong to a cluster if its distance to at least one existing number in the cluster is within a preconfigured spacing limit. If a new number does not fit any existing clusters, it forms a new cluster. In cases where a new number falls between two nearby clusters, it can be part of both, leading to their merge.
For example:
[100, 0, 7, -89, 80, 86, -100, 2, 81]
This input should result in 5 clusters.
I've managed to create a solution, but it only works when inputting numbers in ascending order:
accept = (value: number): void => {
if (this.previous == undefined || Math.abs((this.previous) - value) > this.spacing) {
this.counter++;
}
this.previous = value;
}
I have to enter data one by one because the tests are set up like this:
it("custom spacing test", () => {
detector.accept(4);
detector.accept(7);
expect(detector.clusterCount()).toEqual(2);
});