Question

I'm trying to solve a simple algorithm a specific way where it takes the current row and adds it to the top most row. I know there are plenty of ways to solve this but currently I have a text file that gets read line by line. Each line is converted to an sbyte (there's a certain reason why I am using sbyte but it's irrelevant to my post and I won't mention it here) and added to a list. From there, the line is reversed and added to another list. Here's the code I have for that first part:

        List<List<sbyte>> largeNumbers = new List<List<sbyte>>();
        List<string> total = new List<string>();
        string bigIntFile = @"C:\Users\Justin\Documents\BigNumbers.txt";


        string result;

        StreamReader streamReader = new StreamReader(bigIntFile);

        while ((result = streamReader.ReadLine()) != null)
        {
            List<sbyte> largeNumber = new List<sbyte>();
            for (int i = 0; i < result.Length; i++)
            {
                sbyte singleConvertedDigit = Convert.ToSByte(result.Substring(i, 1));
                largeNumber.Add(singleConvertedDigit);
            }
            largeNumber.Reverse();
            largeNumbers.Add(largeNumber);
        }

From there, I want to use an empty list that stores strings which I will be using later for adding my numbers. However, I want to be able to add numbers to this new list named "total". The numbers I'll be adding to it are not all the same length and because so, I need to check if an index exists at a certain location, if it does I'll be adding the value I'm looking at to the number that resides in that index, if not, I need to create that index and set it's value to 0. In trying to do so, I keep getting an IndexOutOfRange exception (obviously because that index doesn't exist). :

        foreach (var largeNumber in largeNumbers)
        {
            int totalIndex = 0;

            foreach (var digit in largeNumber)
            {
                if (total.Count == 0)
                {
                    total[totalIndex] = digit.ToString(); //Index out of Range exception occurs here
                }
                else
                {
                    total[totalIndex] = (Convert.ToSByte(total[totalIndex]) + digit).ToString();
                }

                totalIndex ++;
            }
        }

I'm just at a loss. Any Ideas on how to check if that index exists; if it does not create it and set it's underlying value equal to 0? This is just a fun exercise for me but I am hitting a brick wall with this lovely index portion. I've tried to use SingleOrDefault as well as ElementAtOrDefault but they don't seem to be working so hot for me. Thanks in advance!

Was it helpful?

Solution

Depending on if your result is have small number of missing elements (i.e. have more than 50% elements missing) consider simply adding 0 to the list till you reach neccessary index. You may use list of nullable items (i.e. List<int?>) instead of regular values (List<int>) if you care if item is missing or not.

Something like (non-compiled...) sample:

// List<long> list; int index; long value
if (index >= list.Count) 
{
  list.AddRange(Enumerable.Repeat(0, index-list.Count+1);
}
list[index] = value;

If you have significant number of missing elements use Dictionary (or SortedDictionary) with (index, value) pairs.

Dictionary<int, long> items;
if (items.ContainsKey(index))
{ 
  items[key] = value;
}
else
{
  items.Add(index, value);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top