I have a constant base list, like this:
[50, 100, 150, 200, 500, 1000]
The list defines ranges: 0 to 50, 50 to 100, and do on until 1000 to infinity.
I want to write a function for transforming any list of numbers into a list compatible with the above. By "compatible" I mean it has only numbers from that list in it, but the numbers are as close to the original value as possible. So for an example input of [111, 255, 950]
, I would get [100, 200, 1000]
. So far I have a naive code that works like this:
for each i in input
{
calculate proximity to each number in base list
get the closest number
remove that number from the base list
return the number
}
This works fine for most scenarios, but breaks down when the input scale goes way out of hand. When I have an input like [1000, 2000, 3000]
, the first number gets the last number from the base list, then 2000 and 3000 get respectively 500 and 200 (since 1000 and then 500 are already taken). This results in a backwards list [1000, 500, 200]
.
How would I guard against that?