Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate two separate arrays from one comma-delimited list?

Tags:

arrays

c#

I have a comma delimited text file that contains 20 digits separated by commas. These numbers represent earned points and possible points for ten different assignments. We're to use these to calculate a final score for the course.

Normally, I'd iterate through the numbers, creating two sums, divide and be done with it. However, our assignment dictates that we load the list of numbers into two arrays.

so this:

10,10,20,20,30,35,40,50,45,50,45,50,50,50,20,20,45,90,85,85

becomes this:

int[10] earned   = {10,20,30,40,45,50,20,45,85};
int[10] possible = {10,20,35,50,50,50,20,90,85};

Right now, I'm using

for (x=0;x<10;x++)
{
     earned[x] = scores[x*2]
     poss  [x] = scores[(x*2)+1]
}

which gives me the results I want, but seems excessively clunky.

Is there a better way?

like image 325
dwwilson66 Avatar asked Dec 16 '13 17:12

dwwilson66


4 Answers

The following should split each alternating item the list into the other two lists.

int[20] scores = {10,10,20,20,30,35,40,50,45,50,45,50,50,50,20,20,45,90,85,85};

int[10] earned;
int[10] possible;

int a = 0;
for(int x=0; x<10; x++)
{
    earned[x] = scores[a++];
    possible[x] = scores[a++];
}
like image 191
AeroX Avatar answered Sep 28 '22 18:09

AeroX


You can use LINQ here:

var arrays = csv.Split(',')
                .Select((v, index) => new {Value = int.Parse(v), Index = index})
                .GroupBy(g => g.Index % 2, 
                         g => g.Value, 
                         (key, values) => values.ToArray())
                .ToList();

and then

var earned = arrays[0];
var possible  = arrays[1];
like image 27
Konrad Kokosa Avatar answered Sep 28 '22 18:09

Konrad Kokosa


Get rid of the "magic" multiplications and illegible array index computations.

var earned = new List<int>();
var possible = new List<int>();
for (x=0; x<scores.Length; x += 2)
{
     earned.Add(scores[x + 0]);
     possible.Add(scores[x + 1]);
}

This has very little that would need a text comment. This is the gold standard for self-documenting code.

I initially thought the question was a C question because of all the incomprehensible indexing. It looked like pointer magic. It was too clever.

In my codebases I usually have an AsChunked extension available that splits a list into chunks of the given size.

var earned = new List<int>();
var possible = new List<int>();
foreach (var pair in scores.AsChunked(2)) {
     earned.Add(pair[0]);
     possible.Add(pair[1]);
}

Now the meaning of the code is apparent. The magic is gone.

Even shorter:

var pairs = scores.AsChunked(2);
var earned = pairs.Select(x => x[0]).ToArray();
var possible = pairs.Select(x => x[1]).ToArray();
like image 23
usr Avatar answered Sep 28 '22 19:09

usr


I suppose you could do it like this:

int[] earned = new int[10];
int[] possible = new int[10];
int resultIndex = 0;

for (int i = 0; i < scores.Count; i = i + 2)
{
    earned[resultIndex] = scores[i];
    possible[resultIndex] = scores[i + 1];
    resultIndex++;
}

You would have to be sure that an equal number of values are stored in scores.

like image 45
MAV Avatar answered Sep 28 '22 17:09

MAV