Skip to content
Advertisement

Grouping / clustering a list of numbers so that the min-max gap of each subset is always less than a cutoff in Python

Say I have a list of 50 random numbers. I want to group the numbers in a way that each subset has a min-max gap less than a cutoff 0.05. Below is my code.

JavaScript

Check if all subsets have min-max gaps less than the cutoff:

JavaScript

Output:

JavaScript

Obviously my code is not working. Any suggestions?

Advertisement

Answer

Following @j_random_hacker’s answer, I simply change my code to

JavaScript

Now it is working as expected

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement