unable to get right output from spliting int arrays - c#

I have goggled and looked various threads at this website and I am still not getting this right.
Objective
Would like to split int arrays in 3 groups
Problem
I am getting an output...
1
4
7
instead of...
[1,2,3]
[4,5,6]
[7,8,9]
1st attempt
static void Main(string[] args)
{
int[] arraya = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Array.Sort(arraya);
int a = 0;
int divideBya = 3;
var resulta = arraya.GroupBy(s => a++ / divideBya).Select(g => g.ToArray()).ToArray();
foreach(var item in resulta)
{
Console.WriteLine("{0}", item[0]);
}
}
2nd attempt
static void Main(string[] args)
{
int u = 0;
int divideByb = 3;
IEnumerable<List<int>> groupedObjects = Enumerable.Range(1, 9).GroupBy(x => u++ / divideByb)
.Select(group => group.ToList()).ToList();
foreach (var item in groupedObjects)
{
Console.WriteLine("{0}", item[0]);
}
}
Can someone point me to the right direction?
Thanks

Your code seems almost correct. Your main problem is your output:
foreach (var item in groupedObjects)
{
Console.WriteLine("{0}", item[0]);
}
You only output the first element of each array. Try this:
foreach (var items in groupedObjects)
Console.WriteLine(string.Join(", ", items));

Try changing your writeline statement to:
Console.WriteLine($"[{item[0]},{item[1]},{item[2]}]");

Related

C# Algorithm for Combinations/Permutations of a defined range of integers

I am trying to generate a list of unique combinations/permutations of 3 allowable values for 20 different participants (each of the 20 participants can be assigned a value of either 1, 2, or 3).
An example of one combination would be an array on length 20 with all ones like such:
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }
...and everything possible all the way up to
{ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 }
where each value in the array can be 1, 2 or 3.
I am stuck on writing my GetAllCombinations() function and I looked at some articles on permutation, but everything I have found is just confusing me more. I am not even sure if permutation is what I need here
So far I have this:
public List<int[]> GetValidCombinations()
{
const int positions = 20;
int[] acceptableValues = new int[3] { 1, 2, 3 };
//DO I USE PERMUTATION HERE TO BUILD MY ARRAY LIST OF ALL POSSIBLE COMBINATIONS?
var allPossibleCombinations = GetAllCombinations(positions, acceptableValues);
List<int[]> validList = new List<int[]>();
foreach (var combination in allPossibleCombinations)
{
//omited for berevity but I would
//do some other validations here...
if (true)
{
validList.Add(combination);
}
}
return validList;
}
public List<int[]> GetAllCombinations(int positions, int[] acceptableValues)
{
//For now returning null because I
//don't know How the heck to do this...
return null;
}
I have looked at some examples of permutation and I tried to use something like this below, but it did not produce what I was looking for:
static IEnumerable<IEnumerable<T>>
GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(o => !t.Contains(o)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
public void Test()
{
const int k = 20;
var n = new[] { 1, 2, 3 };
var combinations = GetPermutations(n, k);
//DOES NOT WORK FOR WHAT I NEED
}
Running Test() worked with k was 3 or less but returned nothing if k was greater then 3.
Try this:
public static List<int[]> GetAllCombinations(int position, int[] acceptableVaues)
{
List<int[]> result = new List<int[]>();
int[] parent = new int[] { };
result = AddAPosition(parent, acceptableVaues);
while(position > 1)
{
var tmpResult = new List<int[]>();
foreach(var _parent in result)
{
tmpResult.AddRange(AddAPosition(_parent, acceptableVaues));
}
position--;
result = tmpResult;
}
return result;
}
public static List<int[]> AddAPosition(int[] parent, int[] acceptableValues)
{
List<int[]> result = new List<int[]>();
for (int i = 0; i< acceptableValues.Length; i++)
{
var anArray = new int[parent.Length + 1];
for (int j = 0; j< parent.Length; j++)
{
anArray[j] = parent[j];
}
anArray[parent.Length] = acceptableValues[i];
result.Add(anArray);
}
return result;
}

Create two random lists from one list

I want to take a List of strings with around 12 objects and split it into two List of strings but completely randomise it.
Example of List:
List 1:
EXAMPLE 1
EXAMPLE 2
EXAMPLE 3
EXAMPLE 4
EXAMPLE 5
EXAMPLE 6
EXAMPLE 7
EXAMPLE 8
Apply some logic here...
Result gives me two lists:
List 1:
EXAMPLE 5
EXAMPLE 6
EXAMPLE 1
EXAMPLE 8
List 2:
EXAMPLE 2
EXAMPLE 3
EXAMPLE 4
EXAMPLE 7
I'm a newbie to C# MVC, so I've found some answers on Stack but none have been able to answer my question.
Edit:
What I've tried so far gives me one random member of the team. I want to now expand on this and create the two lists as mentioned above.
[HttpPost]
public ActionResult Result(Models.TeamGenerator model)
{
var FormNames = model.Names;
string[] lines = FormNames.Split(
new[] { Environment.NewLine },
StringSplitOptions.None);
List<string> listOfLines = new List<string>();
foreach (var i in lines)
{
listOfLines.Add(i);
}
string[] result1 = listOfLines.Where(item => item != string.Empty).ToArray();
Random genRandoms = new Random();
int aRandomTeam = genRandoms.Next(listOfLines.Count);
string currName = listOfLines[aRandomTeam];
return View();
}
*EDIT** Thanks for the solution! I've now got my application working and managed to publish it to the web, https://www.teamgenerator.online.
Create an array of bools the same size as the list of strings.
Fill one half of the array of bools with true, the other half with false.
Shuffle the array of bools.
Iterate through the list of strings. For each element, if the corresponding element in the bool array is true, include that element in the first list; otherwise include it in the second list.
This approach keeps the items in the same order as they were in the original array, if that is important. (If not, just shuffle the entire array of strings and take the first half and the second half).
Sample code:
using System;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
public static void Main()
{
var strings = Enumerable.Range(1, 20).Select(i => i.ToString()).ToList();
var rng = new Random();
int n = strings.Count;
var include = // Create array of bools where half the elements are true and half are false
Enumerable.Repeat(true, n/2) // First half is true
.Concat(Enumerable.Repeat(false, n-n/2)) // Second half is false
.OrderBy(_ => rng.Next()) // Shuffle
.ToArray();
var list1 = strings.Where((s, i) => include[i]).ToList(); // Take elements where `include[index]` is true
var list2 = strings.Where((s, i) => !include[i]).ToList(); // Take elements where `include[index]` is false
Console.WriteLine(string.Join(", ", list1));
Console.WriteLine(string.Join(", ", list2));
}
}
}
Here's a completely different approach that uses a modified version of a standard algorithm for selecting K items from N items (in this case, K = N/2):
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
public static void Main()
{
var strings = Enumerable.Range(1, 20).Select(n => n.ToString()).ToList();
var list1 = new List<string>();
var list2 = new List<string>();
var rng = new Random();
int available = strings.Count;
int remaining = available / 2;
foreach (var s in strings)
{
if (rng.NextDouble() < remaining / (double) available)
{
list1.Add(s);
--remaining;
}
else
{
list2.Add(s);
}
--available;
}
Console.WriteLine(string.Join(", ", list1));
Console.WriteLine(string.Join(", ", list2));
}
}
}
This approach is much more performant than my first solution, but since your list is only about 12 items long, this is hardly important for your problem.
You're currently only generating one random number and getting one value. What you need to do is put that into a loop that is run half as many times as there are items in the list.
var genRandoms = new Random();
var numberRequired = listOfLines.Count/2;
var output = new List<string>();
for (var i=0; i<numberRequired; i++)
{
var aRandomTeam = genRandoms.Next(listOfLines.Count);
output.Add(listOfLines[aRandomTeam]);
listOfLines.RemoveAt(aRandomTeam);
}
Also, this bit at the beginning:
string[] lines = FormNames.Split(
new[] { Environment.NewLine },
StringSplitOptions.None);
List<string> listOfLines = new List<string>();
foreach (var i in lines)
{
listOfLines.Add(i);
}
string[] result1 = listOfLines.Where(item => item != string.Empty).ToArray();
Can be rewritten as:
var listOfLines = FormNames.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries).ToList();
Removing the empty items as part of the split, and using a built-in method to convert it to a list.
First, try to shuffle the list using the Random function
static class MyExtensions
{
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
then split the list into two using Linq
static void Main(String[] args)
{
List<string> examples = new List<string>();
for(int i=1;i<=12;i++)
{
examples.Add($"Example {i}");
}
examples.Shuffle();
var firstlist = examples.Take(examples.ToArray().Length / 2).ToArray();
Console.WriteLine(String.Join(", ", firstlist));
var secondlist = examples.Skip(examples.ToArray().Length / 2).ToArray();
Console.WriteLine(String.Join(", ", secondlist));
Console.ReadLine();
}
the output looks like this
Example 6, Example 8, Example 3, Example 9, Example 5, Example 2
Example 10, Example 11, Example 4, Example 7, Example 12, Example 1
So I wrote an example in a console app, but the concept works just the same... See comments in code block below
Sample List
var fullList = new List<string>()
{
"ITEM 01", "ITEM 02", "ITEM 03", "ITEM 04", "ITEM 05", "ITEM 06",
"ITEM 07", "ITEM 08", "ITEM 09", "ITEM 10", "ITEM 11", "ITEM 12"
};
Initialize Two Lists to Split Values
var list1 = new List<string>();
var list2 = new List<string>();
Creating Two Random Lists
// Initialize one Random object to use throughout the loop
var random = new Random();
// Note: Start at count and count down because we will alter the count of the list
// so counting up is going to mess up. Ex: Count = 4, Remove 1 (Count = 3), Loop won't go to 4
for(int i = fullList.Count; i > 0; i--)
{
// Pull random index
var randomIndex = random.Next(fullList.Count);
// Pull item at random index
var listItem = fullList[randomIndex];
// If i is even, put it in list 1, else put it in list 2.
// You could do whatever you need to choose a list to put it
if (i % 2 == 0)
list1.Add(listItem);
else
list2.Add(listItem);
// Remove random item from the full list so it doesn't get chosen again
fullList.RemoveAt(randomIndex);
}
Results
Console.WriteLine("LIST 1");
Console.WriteLine(string.Join(Environment.NewLine, list1));
Console.WriteLine();
Console.WriteLine("LIST 2");
Console.WriteLine(string.Join(Environment.NewLine, list2));
-----------------------
LIST 1
ITEM 05
ITEM 04
ITEM 12
ITEM 11
ITEM 08
ITEM 01
LIST 2
ITEM 02
ITEM 03
ITEM 09
ITEM 06
ITEM 10
ITEM 07
Here's a simple solution that falls in line with how you were attempting to solve the problem.
The main logic is as followed:
while there are still items in the master list:
choose a random number [0,list.count) as the current target index
choose a random number [0,1] as the current target list to add to
add the item chosen randomly to the randomly selected list
remove the item chosen from the master list
Here's the code:
var random = new Random();
var list = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
var newList1 = new List<string>();
var newList2 = new List<string>();
while(list.Count > 0)
{
//choose the index randomly
int index = random.Next(list.Count);
//get the item at the randomly chosen index
string curItem = list[index];
//choose the list randomly(1==newList1, 2==newList2)
int listChoice = random.Next(2);
//Add the item to the correct list
if(listChoice == 1)
{
newList1.Add(curItem);
}
else
{
newList2.Add(curItem);
}
//finally, remove the element from the string
list.RemoveAt(index);
}

Detecting similar elements in jagged arrays

Is there a way to detect similar elements within multidimensional arrays? For example:
int[][] arrayA = {{1 , 2}, {4 , 6}, {3, 7}};
int[][] arrayB = {{3 , 2}, {1 , 2}, {8, 5}};
Both arrayA and arrayB have the element {1 , 2}. (Or simply, any element in common) Is there a way to detect that it's true?
Yes, you just need to code the logic and you can detect.
bool result = false;
foreach (var arrayAItem in arrayA)
{
foreach (var arrayBItem in arrayB)
{
if (arrayAItem.SequenceEqual(arrayBItem))
{
result = true;
break;
}
}
if (result == true)
{
break;
}
}
and a one liner
bool result = arrayA.Any(arrayAItem => arrayB.Any(arrayBItem => arrayAItem.SequenceEqual(arrayBItem)));
It's hard to give you the answer without just feeding you code, but:
For each element in Array A, check if it's equal to each element in Array B. So basically, you'd need a nested foreach. If you want to record which elements they have in common, add them to a List<ElementType> (not an array since you want to dynamically populate it).
It's not quite that simple since your arrays are nested. You can't just check arrayA[x] == arrayB[x] because you'd be comparing arrays, which are reference types (it would return false unless they both pointed to the same piece of memory). Instead, you'll have to add one more loop inside of the two foreaches you already have to check each int in arrayA against the correlating one in arrayB. int is a value type, so an == comparison would behave how you'd expect it to.
Here is how you can do this (and not only):
var commonItems = from innerA in arrayA
from innerB in arrayB
where innerA.SequenceEqual(innerB)
select innerA;
bool anyCommon = commonItems.Any();
var firstCommon = commonItems.FirstOrDefault();
var commonCount = commonItems.Count();
I guess the variable names are self explanatory :-)
Something along this?
arrayA.Where(arr => arrayB.Any(arr2 => arr1.OrderBy(x => x)
.SequenceEquals(arr.OrderBy(x => x)))).ToArray()
You can remove the two OrderBy calls if you want, depending on how you wish to consider two arrays "the same".
The following code becomes lengthened, but will give you the output you want:
private void TestArrays()
{
int[,] arrayA = new[,] { { 1, 2 }, { 4, 6 }, { 3, 7 } };
int[,] arrayB = new[,] { { 3, 2 }, { 1, 2 }, { 8, 5 } };
int parentLengthA = arrayA.GetLength(0);
int childLengthA = arrayA.GetLength(1);
int parentLengthB = arrayB.GetLength(0);
int childLengthB = arrayB.GetLength(1);
int[] itemsOfA;
int[] itemsOfB;
List<int[]> matchedArrays = new List<int[]>();
for (int i = 0; i < parentLengthA; i++)
{
itemsOfA = new int[childLengthA];
for (int j = 0; j < parentLengthB; j++)
{
itemsOfB = new int[childLengthB];
bool isMatched = true;
if (itemsOfA.Length != itemsOfB.Length)
{
isMatched = false;
break;
}
for (int k = 0; k < itemsOfA.Length; k++)
{
if (arrayA[i, k] != arrayB[j, k])
{
isMatched = false;
break;
}
else
{
itemsOfA[k] = arrayA[i, k];
}
}
if (isMatched)
{
matchedArrays.Add(itemsOfA);
}
}
}
//Just to output the matched array(s)
if (matchedArrays.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (int[] matchedArray in matchedArrays)
{
foreach (int i in matchedArray)
{
sb.Append(i + ",");
}
sb.AppendLine();
}
MessageBox.Show(sb.ToString());
}
}

How to get a complete row or column from 2D array in C#

I do not want to use a jagged array and I have a 2D array and I want to get a complete column or row without looping through it. Does anyone have an idea how it can be done.
double [,] array = new double [3,3] ;
1 2 3
4 5 6
Out: 1 2 3 or 2 5
To get a specific row or column from the multidimensional array you can use some LINQ:
public class CustomArray<T>
{
public T[] GetColumn(T[,] matrix, int columnNumber)
{
return Enumerable.Range(0, matrix.GetLength(0))
.Select(x => matrix[x, columnNumber])
.ToArray();
}
public T[] GetRow(T[,] matrix, int rowNumber)
{
return Enumerable.Range(0, matrix.GetLength(1))
.Select(x => matrix[rowNumber, x])
.ToArray();
}
}
You can optimise it for getting rows by using Buffer.BlockCopy(), but to get a column you'll have to loop. Buffer.BlockCopy() ultimately uses a processor instruction to copy a block of memory, so it is pretty fast.
It's convenient to put the code into an extension method to make it easier to call. Note that Buffer.BlockCopy() can only be used on arrays of primitive types, i.e. int, double, char etc. This does NOT include string.
Here's a compilable example:
using System;
using System.Linq;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public static class Program
{
private static void Main()
{
var array = new [,]
{
{0.1, 0.2, 0.3, 0.4, 0.5},
{1.1, 1.2, 1.3, 1.4, 1.5},
{2.1, 2.2, 2.3, 2.4, 2.5},
{3.1, 3.2, 3.3, 3.4, 3.5},
};
var row = array.GetRow(2);
// This prints 2.1, 2.2, 2.3, 2.4, 2.5
Console.WriteLine(string.Join(", ", row.Select(element => element.ToString())));
}
}
public static class ArrayExt
{
public static T[] GetRow<T>(this T[,] array, int row)
{
if (!typeof(T).IsPrimitive)
throw new InvalidOperationException("Not supported for managed types.");
if (array == null)
throw new ArgumentNullException("array");
int cols = array.GetUpperBound(1) + 1;
T[] result = new T[cols];
int size;
if (typeof(T) == typeof(bool))
size = 1;
else if (typeof(T) == typeof(char))
size = 2;
else
size = Marshal.SizeOf<T>();
Buffer.BlockCopy(array, row*cols*size, result, 0, cols*size);
return result;
}
}
}
As of March 2021, you can now use the very cool Span2D class for this!
If you are happy using spans (I highly recommend reading about them, they are awesome), you can use the following code
var span2D = new Span2D<double>(array);
//Gets the first row and returns a span of it
var rowSpan = span2D.GetRowSpan(0);
foreach(var number in rowSpan)
{
//Do something with numbers
}
//Gets the 2nd Column as a RefEnumerable and converts it to an array
var column = span2D.GetColumn(1).ToArray();
Here is how i have done it you can use
GetLength(0)
to get the columns and use
GetLength(1)
to get the rows of the 2 Dimensional array and you loop thru it with the for loop if any one else needs this.
string text = "";
for (int i = 0; i < array.GetLength(0); i++)
{
text += Convert.ToString(array[i, 2]) + "\n";
}
an alternative way you can do it is by using a List instead of an array.
Specifically in your case you'd do something like that:
Initially create an inner class that represents a tuple of the array
Create a List of the inner class
Populate the inner class
Get the row that contains something specific
Get the column that contains something specific
public static void Main(string[] args)
{
// #2 -- Instantiate List of myClass
List<myClass> myList = new List<myClass>();
//
// #3 -- Populate the list
myList.Add(new myClass(1,2,3));
myList.Add(new myClass(3,4,5));
myList.Add(new myClass(5,6,6));
//
// #4 -- Get the line where a == 1
myList.Find(x=>x.a == 1);
//
// #5 -- Get column b
myList.Select(x=>x.b);
}
// #1 -- Create the inner class
public class myClass
{
public int a;
public int b;
public int c;
public myClass(int a, int b, int c)
{
this.a =a;
this.b =b;
this.c =c;
}
}
what is needed is a jagged array (not a multidimensional array)
https://msdn.microsoft.com/en-us/library/2s05feca.aspx
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
full example with columns:
using System;
using System.Collections.Generic;
namespace Rextester
{
public class Program
{
public static T[] column<T>(T[][] jaggedArray,int wanted_column)
{
T[] columnArray = new T[jaggedArray.Length];
T[] rowArray;
for(int i=0;i<jaggedArray.Length;i++)
{
rowArray=jaggedArray[i];
if(wanted_column<rowArray.Length)
columnArray[i]=rowArray[wanted_column];
}
return columnArray;
}
public static void Main(string[] args)
{
//Your code goes here
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
Console.WriteLine("Hello, world!");
Console.WriteLine(string.Join(" ",jaggedArray[1]));
Console.WriteLine(string.Join(" ",column(jaggedArray,1)));
}
}
}
similar idea, using extensions:
using System;
using System.Collections.Generic;
namespace Rextester
{
public static class MyExtensions
{
public static string Extend(this Array array)
{
return "Yes, you can extend an array";
}
public static T[] column<T>(this T[,] multidimArray,int wanted_column)
{
int l=multidimArray.GetLength(0);
T[] columnArray = new T[l];
for(int i=0;i<l;i++)
{
columnArray[i]=multidimArray[i,wanted_column];
}
return columnArray;
}
public static T[] row<T>(this T[,] multidimArray,int wanted_row)
{
int l=multidimArray.GetLength(1);
T[] rowArray = new T[l];
for(int i=0;i<l;i++)
{
rowArray[i]=multidimArray[wanted_row,i];
}
return rowArray;
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
int [,] multidimArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Console.WriteLine(string.Join(" ",multidimArray.column(0)));
Console.WriteLine(string.Join(" ",multidimArray.row(0)));
}
}
}
My use case differs from the question, but is similar. I needed a 2D array of float[2] arrays, that I was using to represent complex numbers.
float[,,] myarray = new float[100,100,2];
float[] result = myarray[1,1]; <-- fails to compile needs all 3 coordinates
The jagged array Simmon mentioned provided the solution.
float[,][] myarray = new float[100,100][];
...
myarray[x,y] = new float[2]; <-- Initialise all elements of jagged 2D array in loop
...
float[] result = [100,100];
if you know the index of the numbers to output..then you don't need to use a loop to get the output desired...
double[,] array = new double[3,3] {{1,2,3}, {4,5,6}, {7,8,9}};
int firstNum = array[0,1];
int secondNum = array[1,1];
this will get 2, 5

Loop Through Integer Array

What I am trying to do is loop through an integer array
int[] integerarray = { 1, 2, 3, 4 };
for (??????)
{
// What do I do here?
}
Until I get to 3. I'm not sure how though.
we can achieve this by using simple for each loop
foreach(int i in integerarray)
{
if(i==3)
{
// do your stuf here;
break;
}
}
int[] integerarray = { 1, 2, 3, 4 };
for (int i=0;i<integerarray.Length;i++)
{
if(integerarray[i]==3)
break;
//Do something here
}
One way to do a loop of a fixed number is a while loop.
int counter = 0;
while(counter < 3)
{
tmp = integerArray[counter];
????
counter++;
}
You can use linq TakeWhile method to get the elements from a sequence as long as a specified condition is true.
Here you want that return element untill we found 3 in the sequence so you can write this statement like this
var result = integerarray.TakeWhile(x => !x.Equals(3)).ToList();
result will contains the items which comes before 3
in your case result will have 1,2
Use LINQ.
int[] integerarray = { 1, 2, 3, 4 };
for (var number = integerarray.TakeWhile(x => x != 3))
{
// Do something
}

Categories