0
\$\begingroup\$

I wrote a solution Please look at the my approach is correct? There is a better solution?

List<Tuple<object, object, object>> OneDimensionalLists = new List<Tuple<object, object, object>>();
List<Tuple<object, object, object>[]> TwoDimensionalLists = new List<Tuple<object, object, object>[]>();

int i1 = 0;
for (int i = 0; i < OneDimensionalLists.Count; i++)
{
    var random = new Random(seed);
    int next = random.Next(10, 30);
    TwoDimensionalLists.Add(new Tuple<object, object, object>[next]);
    for (int j = 0; j < next; j++)
    {
        if (OneDimensionalLists.Count == i1)
            goto go;

        TwoDimensionalLists[i][j] = OneDimensionalLists[i1++];
    }
}
go:
\$\endgroup\$
2
  • 3
    \$\begingroup\$ I wrote a solution - well, this isn't a good description of your code and thegoto this is a no-go ;-D \$\endgroup\$
    – t3chb0t
    Jul 27 '16 at 11:51
  • 1
    \$\begingroup\$ Could kindly explain what you are trying to achieve with this code e.g the output, input etc? \$\endgroup\$
    – Tolani
    Jul 27 '16 at 12:11
5
\$\begingroup\$
for (int i = 0; i < OneDimensionalLists.Count; i++)
{
    var random = new Random(seed);
    int next = random.Next(10, 30);
    ...

Have you checked the value of next on the various iterations? I can't see seed changing anywhere in this code. Assuming it remains the same, random.Next() will return the same value each time. Unless this is intentional, create the Random(seed) outside of the loop.

\$\endgroup\$
1
\$\begingroup\$

Whenever you declare and assign a variable in a single statement, if you are not going to assign it as null, use the var keyword instead of the type name.

List<Tuple<object, object, object>> oneDimensionalList = new List<Tuple<object, object, object>>();

could become

var oneDimensionalList = new List<Tuple<object, object, object>>();

which is far simpler.

The odds that someone will be randomly working with Tuple<object, object, object> are pretty slim in my opinion, so you should perhaps consider changing your method(?) to accept a List<T> if not a ICollection<T> or even an IEnumerable<T> instead.

Speaking of "method," it seems that this code should be one. It effectively takes an input and produces an output, so why not isolate it to be its own piece of code that you can call wherever you want? You could also get rid of that goto statement in favor of a proper return.

It would look something like this:

public static List<Tuple<object, object, object>[]> MakeTwoDimensional(
    List<Tuple<object, object, object>> oneDimensionalList)
{
    var twoDimensionalList = new List<Tuple<object, object, object>[]>();

    // Somehow transform 'oneDimensionalList` into `twoDimensionalList`

    return twoDimensionalList;
}

Your conditional statement using the i1 variable reads very oddly. Granted, I normally wouldn't be using a loop over a collection with a randomly generated number as the conditional, but maybe that's just me.

Outside of the code itself, I don't understand what "problem" you are trying to solve. I don't see how this type of transformation would be useful. In your example, the code will actually do nothing, as the first list is initialized empty and nothing is added to it.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.