Combination of two arrays [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a integer array that shows the nodes of a path : {1,2,3,4,5}
1 --> 2 --> 3 --> 4 --> 5
But some nodes of this path can be optional.
For example node 4 is optional.
So, I can use this path 1 --> 2 --> 3 --> 4 --> 5
or this path 1 --> 2 --> 3 --> 5 to reach my destination.
I want to produce all path combinations.
//ProduceCombinations(int[] path,int[] possibleNodes)
{1*,2,3,4*,5*}
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--

Here you go:
static void Main(string[] args)
{
int[] pathNodes = new int[] {1,2,3,4,5};
int[] optionalNodes = new int[] { 1, 4, 5 };
List<int[]> combies = ProduceCombinations(pathNodes, optionalNodes);
}
public static List<int[]> ProduceCombinations(int[] PathNodes, int[] OptionalNodes)
{
List<int[]> results = new List<int[]>();
results.Add((int[])PathNodes.Clone());
int index = 0;
for (int j = 0; j < OptionalNodes.Length; j++)
{
while (PathNodes[index] < OptionalNodes[j]) index++;
int lenght = results.Count;
for(int i = 0; i < lenght; i++)
{
var newSol = (int[])results[i].Clone();
newSol[index] = 0;
results.Add(newSol);
}
}
return results;
}

Related

Have N elemnts of sum of a list and repeat until the end of the list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm facing a problem when trying to play with C# list. Currently I have a list of integer. And I'm looking for a way to sum up every 5 integer, until the end of the list.
For example I have a list like this:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
How to sum up every N elements (let's say 5) in the list and become:
[ 15, 40 ]
FYI, the list consist of hundred and thousand int elements.
Thanks.
Since noone's mentioned the simple way yet...
Note that if the input is not divisible by the group size, any extra elements will be ignored. For example, if the input is 11 elements, and the group size is 5, this will give 2 sums of 5 elements and then ignore the 11th input element.
public static int[] SumEvery(int[] input, int groupSize)
{
// The number of groups we have
int numGroups = input.Length / groupSize;
int[] result = new int[numGroups];
// For each group of numbers...
for (int group = 0; group < numGroups; group++)
{
// ... loop through each number in that group, adding to the sum ...
int sum = 0;
for (int i = 0; i < groupSize; i++)
{
sum += input[group * groupSize + i];
}
// ... then store that sum in our array of results
result[group] = sum;
}
return result;
}
int[] input = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] result = SumEvery(input, 5);
If you want to stream the results as an IEnumerable<int>, rather than collecting them all into an array, then:
public static IEnumerable<int> SumEvery(int[] input, int groupSize)
{
int numGroups = input.Length / groupSize;
for (int group = 0; group < numGroups; group++)
{
int sum = 0;
for (int i = 0; i < groupSize; i++)
{
sum += input[group * groupSize + i];
}
yield return sum;
}
}
You could do this with a bit of Linq
var ints = new []{1,2,3,4,5,6,7,8,9,10};
var result = ints.Select((x,i) => new{Num = x,Index = i}) // Project to a list with the index and number
.GroupBy (i => i.Index/5) // group by the int division by 5
.Select(g => g.Sum(a => a.Num)); // sum the numbers in each group
Live example: https://dotnetfiddle.net/BabP5N
Note that is is by far the least efficient way - and with a large data set will not perform well. But for a small dataset will work fine. This code is possibly the clearest interpretation of the problem.

for loop printing last number in sequence [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can you print out the last number in the sequence and the amount of times it has ran in a for loop?
int numOne = 50;
for (int i = 0; i < numOne; i++)
{
Console.WriteLine(i);
}
var list = new List<int> { 1, 2, 3, 6, 10, 1, 5, 1 };
var lastItem = list.Last();
Console.WriteLine("{0} {1}", lastItem, list.Count(item => item == lastItem));

How to get the remain value in linq? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have List which contains:(without remain column data)
Recive Pay Remain
---------------------------------------------
2 0 0
0 4 0
3 0 0
4 0 0
And I have initialRecieve=1 variable
How can I calculate the Remain Column in C# to obtain the below result?
Recive Pay Remain
---------------------------------------------
2 0 3 (initialRecieve+Recive-Pay)
0 4 -1 (Remain+Recive-Pay)
3 0 2 (Remain+Recive-Pay)
4 0 6 (Remain+Recive-Pay)
I create a simple console app depends what you need
class doc{
public int Pay{get;set;}
public int Receive{get;set;}
}
public class Program
{
public static void Main(string[] args)
{
List<doc> lst = new List<doc>();
lst.Add(new doc(){Receive=2,Pay=0});
lst.Add(new doc(){Receive=0,Pay=4});
lst.Add(new doc(){Receive=3,Pay=0});
lst.Add(new doc(){Receive=4,Pay=0});
int remain = 1; // initialRecieve=1
var result = (from line in lst
select new {
Receive = line.Receive,
Pay = line.Pay,
Remain = (remain = remain + line.Receive - line.Pay)
}).ToList();
foreach(var item in result){
Console.WriteLine(item);
}
}
}
And the result would be:
{ Receive = 2, Pay = 0, Remain = 3 }
{ Receive = 0, Pay = 4, Remain = -1 }
{ Receive = 3, Pay = 0, Remain = 2 }
{ Receive = 4, Pay = 0, Remain = 6 }
Check it on DotNetFiddle
Simply do something like the below:
for (var i = 0; i < DataTable.Rows.Count; i++)
{
if (i != 0)
{
DataTable.Rows[i][2] = (DataTable.Rows[i - 1][2] + DataTableRows[i][0]) - DataTable.Rows[i][1];
}
else if (i == 0) DataTable.Rows[i][2] = (InitialRecieve + DataTable.Rows[i][0]) - DataTable.Rows[i][1];
}
That should calculate what you need with the data in memory.

C# simple grid filling algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to find an efficient way to fill a gird (2 arrays) with numbers representing the area the cell in the grid occupies. For example:
The numbers correlates to the root area. The question is how can I randomly fill the matrix with these values that correlates to the root area
// Grid could look something like this
// 22333
// 22333
// 11333
// or
// 33311
// 33322
// 33322
// What i have currently
int numRows = 4;
int numColums = 5;
int cellSize = Random.Range(0,4);
for (int iRow = 0; iRow < numRows; iRow++) {
for (int iCol = 0; iCol <numColums ; iCol++) {
// Fill value
test.gridData.rows[iRow].colum[iCol] = cellSize;
}
}
int numRows = 4;
int numColums = 5;
int cellSize = Random.Range(0,4);
for (int iRow = 0; iRow < numRows; iRow++) {
for (int iCol = 0; iCol <numColums ; iCol++) {
// HERE
test.gridData[iRow][iCol].MyVal= val;
}
}
Assuming your actual code reflects the code in your question, the MyVal property of your Test matrix at position [row][column] is now equal to the value of cellSize.
You can access individual elements of a 2D array as such:
Assuming your grid is:
0 1 2 3 4
1 1 2 3 4
2 1 2 3 4
3 1 2 3 4
You can access individual cells using something like test.GridData[2][4] which will give you (assuming the above grid):
the value at row[2] => 2
⬇
0 1 2 3 4
1 1 2 3 4
2 1 2 3 4
3 1 2 3 4
and column[4] => 4:
0 1 2 3 4
1 1 2 3 4
2 1 2 3 4
3 1 2 3 4 ⬅

C# How to: 2d array of jagged arrays? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make a multidemesional array of jagged arrays. Is this possible? How?
For Instance I see lokts of examples like the following:
int[][,] jaggedArray4 = new int[3][,]
I want to create the following:
int[,,][] myFixedJagged = new int[2,2,3][]
where the last [] is Jagged. How can I declare that?
Thanks!
This just works:
int[,,][] myFixedJagged = new int[2, 2, 3][];
myFixedJagged[0, 0, 0] = new int[10];
myFixedJagged[0, 0, 0][9] = 1;
I wouldn't want to use it though.
Is this what you want?
static void Main(string[] args)
{
// This is the silliest thing I have seen in my life
int[, ,][][] jgarray=new int[2, 2, 3][][];
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
for (int k=0; k<3; k++)
{
var array =new int[10][];
for (int z=0; z<10; z++)
{
array[z]=new int[20];
for (int v=0; v<20; v++)
{
array[z][v]=v+20*(z+10*(k+3*(j+2*i)));
}
}
jgarray[i, j, k]=array;
}
}
}
}

Categories