1

This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.

    char[] array = digits.ToCharArray();

    var intArray = new List<int>(new int[span]);

    for (int i = 0; i < span; i++) 
    {
       intArray[i] =  (int)Char.GetNumericValue(array.ElementAt(i));
    }

    var data = new List<int[]>();
    int n = 0;
    while (data[n].Length == span)
    {
       data[n] = intArray.Skip(n).Take(span).ToArray();
       n++;
    }

Output:

Parameter name: index
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
   at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53

Line 20 comes up in my IDE as while (data[n].Length == span).


Updated

I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition function is super helpful in this situation) but I still don't have a solution for my initial problem.

largestSeriesProduct[string_, int_Integer] := Module[{numbers,lists},
   numbers = ToExpression /@ Characters @ string;
   lists = Times @@@ Partition[numbers,int,1];
   Max @ lists
]

Which for example returns:

largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]

23520
BBirdsell
  • 207
  • 2
  • 13
  • this is not a compiler error – adjan Nov 10 '18 at 17:41
  • 1
    The type/name of the exception itself should give you quite a big hint... ;-) –  Nov 10 '18 at 17:43
  • n is the counter starting at zero and then n++ – BBirdsell Nov 10 '18 at 17:43
  • `data` is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the **[AWESOME Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Nov 10 '18 at 17:45
  • `intArray` is not a good variable name for a List. – mjwills Nov 12 '18 at 05:21
  • 1
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – mjwills Nov 12 '18 at 05:23

2 Answers2

0

to help you out:

var intArray = new List<int>(new int[span]);

            for (int i = 0; i < span; i++)
            {
                intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));
            }
Aldert
  • 4,209
  • 1
  • 9
  • 23
0

My solution.

        var intArray = new List<int>(new int[array.Length]);
        for (int i = 0; i < array.Length; i++) 
        {
            intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
        }
        var data = new List<int[]>();
        int n = 0;
        while (intArray.Skip(n).Take(span).ToArray().Length == span)
        {
            data.Add(intArray.Skip(n).Take(span).ToArray());
            n++;   
        }
        var list = new List<long>(data.Count());
        foreach (int[] nums in data)
        {
            list.Add((long)nums.Aggregate((total, next) => total * next));
        }
        long maxProduct = list.Max();
        return maxProduct;
BBirdsell
  • 207
  • 2
  • 13