Reputation: 1
I need to find max value within each 1000 values in a file, i.e., if there is 5000 values within a file then there should be 5 max values. Within first 1000 values: one max value, then within next 1000 values one more max value.... and so on.
The size of the file will be unknown
I had prepared a code for finding max value within first 1000 values in the file. I was thinking of iterating it, to get the desired results, but could not. The codes are as follows.
List<Double> list = new List<Double>();
StreamReader dat = new StreamReader(file path);
//int count = 1;
string dataline = "";
for (int i = 1; i <= dat.BaseStream.Length; i+=1000)
{
while ((dataline = dat.ReadLine()) != null)
{
Double sdat = Double.Parse(dataline);
list.Add(sdat);
richTextBox1.Text = list.Take(1000).Max().ToString();
}
}
Upvotes: 0
Views: 182
Reputation: 700512
You can group the values on the index and get the max from each group:
List<double> max =
File.ReadAllLines(filePath)
.Select((s, i) => new KeyValuePair<int, double>(i, Double.Parse(s)))
.GroupBy(x => x.Key / 1000, x => x.Value)
.Select(g => g.Max())
.ToList();
Upvotes: 2