We wish to train a machine learning algorithm on an array of floating-point numbers in the interval [0.0,
1.0). The data is not evenly distributed, and we wish to filter the dataset to obtain a subset containing an
equal number of values from each interval [0, 0.2), [0.2, 0.4), ... [0.8, 1.0), throwing away as little data as
possible.
Write a program which reads comma-separated floating-point numbers in a single line from stdin and prints
the filtered data to stdout in the same format
Note: Solve this in linear time.
Examples:
Example 1
Input: 0.1,0.3,0.5,0.7,0.9
Output: 0.1,0.3,0.5,0.7,0.9
Answers
Answer:
sorry for these says i don't no that answer
Answer:
Explanation:
import java.util.*;
import java.util.Map.Entry;
public class ClassifyFloats
{
public static void main(String[] args)
{
Map<Float, List<Float>> intervals = new LinkedHashMap<>();
Map<Float, List<Float>> result = new LinkedHashMap<>();
float[] inputsFromUser = {0.11F, 0.12F, 0.13F, 0.23F, 0.34F, 0.35F, 0.47F, 0.59F, 0.77F, 0.83F,0.85F,0.91F,0.95F};
Float baseFloat = 0.2F;
int minIntervalValuesCount = Integer.MAX_VALUE;
for (Float f:inputsFromUser)
{
Float intervalkey = (float) (baseFloat+((int) Math.floor(f/baseFloat))*baseFloat);
List<Float> intervalValues = intervals.get(intervalkey);
if (intervalValues !=null)
{
intervalValues.add(f);
intervals.put(intervalkey, intervalValues);
}
else
{
intervalValues = new ArrayList<>();
intervalValues.add(f);
intervals.put(intervalkey, intervalValues);
}
}
for(Entry<Float, List<Float>> e:intervals.entrySet())
{
if (e.getValue().size()<minIntervalValuesCount)
{
minIntervalValuesCount = e.getValue().size();
}
}
for(Entry<Float, List<Float>> e: intervals.entrySet())
{
result.put(e.getKey(), e.getValue().subList(0, minIntervalValuesCount));
}
System.out.println("minIntervalValuesCount: "+minIntervalValuesCount);
System.out.println(intervals);
System.out.println(result);
}
}