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