+ 4

Did someone like an interesting challenge?

We have a jagged array (entered by user): " jArray" that contain the three arrays: jArray[0][ ]={"A", "B", "C"}; jArray[1][ ]={"D"}; jArray[2][ ]={"E", "F"}; write a method that receive jArray as argument and returns what following (in the same order): ADE ADF BDE BDF CDE CDF The method must be reusable and must allows a jagged array of any length: "jArray[n][m]". Tip: recursivity could help.

24th Sep 2017, 2:01 AM
S.D.T.
3 Antworten
+ 5
You simply use nested for loops I don't know C# but still How is that even a challenge?
24th Sep 2017, 8:58 AM
Rugved Modak
Rugved Modak - avatar
+ 1
Hello, to say it that way sounds like explaining that to make a house you have to use cement and bricks without adding anything else. It is not as easy as it may seem at first. I've been through the nested loops when I was looking for the solution, but they have not been enough, although they are obviously necessary. I forgot to mention that I already have the solution (using LINQ). I would just like to see if there are other algorithms and what they are (including in others languages like java ;) )
24th Sep 2017, 9:35 AM
S.D.T.
0
Ok, this is part of the code I've found on Internet and it work very well. Also I've another version (totally mine) that work non-recursively and only with arrays. static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) { // base case: IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() }; foreach (var sequence in sequences) { // don't close over the loop variable var s = sequence; // recursive case: use SelectMany to build the new product out of the old one result = from seq in result from item in s select seq.Concat(new[] { item }); } return result; }
5th Oct 2017, 2:49 AM
S.D.T.