Group items of array by three using Linq - c#

I have an Array of bytes, representing the RGB values of an image.
How could I group each offset of 3 values (RGB) of this array to apply my tweaks (like removing the repeated colors), maybe using Linq?
["120", "100", "10", "120", "100", "10", "10", "60", "110"]
to
["120", "100", "10", "10", "60", "110"]

You can use Select to add index to your enumeration and later group by index / 3. A bit of post-processing on each of the groups and you should be able to get what you want:
var grouped = source.Select((x,i) => new { x, i })
.GroupBy(x -> x.i / 3)
.Select(g => g.ToList())
.Select(g => new { R = g[0], G = g[1], B = g[2] })
.Distinct();
But that feels quite ugly. If I were you I'd probably write a simple custom LINQ method (an extension method on IEnumerable<int>) to do this more efficiently.

Shorter version that gets the distinct RGB values and their indexes:
string[] a = { "120", "100", "10", "120", "100", "10", "10", "60", "110" };
var l = Enumerable.Range(0, a.Length / 3)
.ToLookup(i => new { R = a[i * 3], G = a[i * 3 + 1], B = a[i * 3 + 2] });

If you don't mind using a loop instead of Linq:
class Program
{
static void Main(string[] args)
{
byte[] array = new byte[] { 120, 100, 10, 120, 100, 10, 10, 60, 110 };
List<byte[]> grouped = new List<byte[]>();
// This loop will populate the list grouped with arrays of 3 bytes each, each representing an value for RGB
for(int i = 0; i + 2 < array.Length; i += 3)
{
byte[] currentColor = new byte[]
{
array[i],
array[i + 1],
array[i + 2]
};
grouped.Add(currentColor);
}
// Here you will remove repeated elements for RGB
// Notice you will have to create the ByteArrayComparer class, you will find the code right under this one
var noRepeatedElements = grouped.Distinct<byte[]>(new ByteArrayComparer());
// Print the non repeated elements for testing purposes
foreach(var rgb in noRepeatedElements)
{
foreach(var value in rgb)
{
Console.Write($"\"{value}\"");
}
}
Console.ReadKey();
}
}
Where ByteArrayComparer is the following class
// This class will compare two distinct byte arrays and check if their elements are the same
public class ByteArrayComparer : IEqualityComparer<byte[]>
{
public bool Equals(byte[] x, byte[] y)
{
int smallerArrayLength = Math.Min(x.Length, y.Length);
bool elementsWithSameValue = true;
for(int i = 0; i < smallerArrayLength; i++)
{
// If there is a single element which is different, we know the arrays are different and can break the loop.
if(x[i] != y[i])
{
elementsWithSameValue = false;
break;
}
}
return elementsWithSameValue;
}
public int GetHashCode(byte[] obj)
{
int hash = 0;
for(int i = 0; i < obj.Length; i++)
{
hash += obj[i].GetHashCode();
}
return hash;
}
}
Note that grouped now is a List of arrays of bytes. Each element in grouped has three elements, representing a single RGB value.
Now you can work with the rgb values as you please.

Using Microsoft's Reactive Framework Team's Interactive Extensions (NuGet "Ix-Main") you can do this:
byte[] array = new byte[]
{
120, 100, 10, 120, 100, 10, 10, 60, 110
};
byte[] results =
array
.Buffer(3)
.Distinct(xs => String.Join(",", xs))
.SelectMany(x => x)
.ToArray();
That will give you { 120, 100, 10, 10, 60, 110 }.

Related

Splitting String Arrays into Groups

There is a string [] yield that can contain N count data. I have defined 15 count to be an example.
I want to divide these data into 6 groups.However, I cannot load the last remaining items into the array.
Where am I making a mistake?
string[] tags = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
double tagLength = (int)Math.Floor(tags.Length / (double)6);
for (int i = 0; i <= tagLength-1; i++)
{
string[] groupArrays = new string[6];
Array.Copy(tags, i * 6, groupArrays, 0, 6);
}
The output i see
[0] = {1,2,3,4,5,6}
[1] = {7,8,9,10,11,12}
Should be output
[0] = {1,2,3,4,5,6}
[1] = {7,8,9,10,11,12}
[2] = {13,14,15}
I would suggest changing your code to calculate the number of groups you need to this:
int groups = (count / groupSize);
bool hasPartialGroup = count % groupSize != 0;
if (hasPartialGroup)
{
++groups;
}
The result of the first line will be integer division, so 15 / 6 will result in 2. We then see if there is a remainder using the remainder operator (%): count % groupSize. If its result isn't 0, then there is a remainder, and we have a partial group, so we have to account for that.
So for groups = 15 and groupSize = 6, we'll get count = 3. For groups = 12 and groupSize = 6, we'll get count = 2, etc.
Fixing your code to use this, it might look like:
string[] tags = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
int count = tags.Length;
const int groupSize = 6;
int groups = (count / groupSize);
bool hasPartialGroup = count % groupSize != 0;
if (hasPartialGroup)
{
++groups;
}
for (int i = 0; i < groups; i++)
{
// you can't copy beyond the end of the array so we have to choose between the remaining ungrouped items and the group size
int currentGroupSize = Math.Min(tags.Length - i*groupSize, groupSize);
// I'm assuming for a partial group you only want this to be as big as the number of items.
// If you want it to always be 6 then change new string[currentGroupSize] to new string[groupSize] and you should be OK.
string[] groupArrays = new string[currentGroupSize];
Array.Copy(tags, i * groupSize, groupArrays, 0, currentGroupSize);
Console.WriteLine(string.Join(",", groupArrays));
}
Try it online // Example with fixed group size
Alternatively, you could create a batching helper method:
private static IEnumerable<T[]> BatchItems<T>(IEnumerable<T> source, int batchSize)
{
var collection = new List<T>(batchSize);
foreach (var item in source)
{
collection.Add(item);
if (collection.Count == batchSize)
{
yield return collection.ToArray();
collection.Clear();
}
}
if (collection.Count > 0)
{
yield return collection.ToArray();
}
}
This will collect batchSize number items together and then return one group at a time. You can read about how this works with yield return here.
Usage:
string[] tags = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
List<string[]> batchedTags = BatchItems(tags, 6).ToList();
This will result in 3 string arrays, containing 1,2,3,4,5,6, 7,8,9,10,11,12, and 13,14,15.
You could also make this into an extension method.
Try it online
If you mean why you are not getting groups of 6, the reason for this is that you are flooring the length of tags / 6. So, if the last group has the length of less that 6, it won't get added. Add this to the end:
if (tags.Length%6!=0) { string[] groupArrays = tags[i..tags.Length] } // You can do this manually.
As said before, it's because you use Math.Floor(). Use either Math.Ceiling or remove the -1 from i<= taglength - 1.
Array.Copy will still produce errors when you're tags aren't an exact multiple of 6.
Below code should do the trick and won't produce an error
string[] tags = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
int baselength = 6;
double tagLength = (int)Math.Floor(tags.Length / (double)6);
int length = baselength;
for (int i = 0; i <= tagLength; i++)
{
string[] groupArrays = new string[baselength];
if (i == tagLength)
length = ((i + 1) * length) - tags.Length;
if(length > 0 && length < baselength)
Array.Copy(tags, i * 6, groupArrays, 0, length);
}
Because you use Math.Floor(...). You should use Math.Ceiling(...) instead.
(int)Math.Floor(15d / 6d) // returns 2 >> 2 groups.
(int)Math.Ceiling(15d / 6d) // returns 3 >> 3 groups.
Beware though, you will get an ArgumentOutOfRangeException in Array.Copy, since index 3 * 6 does not exist. You will have to find a way around that.
One possible solution:
string[] tags = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
double tagLength = (int)Math.Ceiling(tags.Length / (double)6);
for (int i = 0; i <= tagLength - 1; i++)
{
int arrLength = (i + 1) * 6 <= tags.Length ? 6 : tags.Length % 6;
string[] groupArrays = new string[arrLength]; // or six if you always want a length of 6
Array.Copy(tags, i * 6, groupArrays, 0, arrLength);
}
Or using Linq:
for (int i = 0; i < (int)Math.Ceiling(tags.Length / 6d); i++)
{
string[] groupArrays = tags.Skip(i * 6).Take(6).ToArray();
}

Find subsets of a string but just which is neighbours

I am trying to extract subsets but not all of them, just which is a neighbour. The simple example below;
Input : "123456789"
Result :
3.Level Subset: out3 : [123,234,345,456,567,678,789]
..
5.Level Subset : out5 :[12345,23456,34567,45678,56789]
..
8.Level Subset: out8 :[12345678,23456789]
result = [out1,..,out5,..out8]
If there is a cool solution for this and if it can be string operation it will be good.
Many thanks
You'll need to do additional error checks to see if level is longer than the length of the string etc.
public static void Main(string[] args)
{
var results = FindSubsets("123456789", 3);
Console.Read();
}
public static List<string> FindSubsets(string data, int level)
{
if (level > data.Length || level < 1)
return null;
var results = new List<string>();
for (int i = 0; i < data.Length - level + 1; i++)
{
results.Add(data.Substring(i, level));
}
return results;
}
Edit:
Added string length check against level.
Edit2:
If you want to find subsets of certain length, you can do something like the following. Create a List<int> with all the levels you want to find subsets of, then repeatedly call the function. For example, let's say you want to find subsets for levels 3, 5, and 8. Then:
var data = "123456789";
var levels = new List<int>() { 3, 5, 8 };
var results = new List<List<string>>();
foreach(var level in levels)
{
results.Add(FindSubsets(data, level));
}
public IEnumerable<string> GetSubsets(string input, int length)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentException("Invalid string");
if (length <= 0)
throw new ArgumentException("Length must be greater than 0.");
if (length > input.Length)
throw new ArgumentException("The desired set length is longer than the string");
for(int i = 0; i<=input.Length - length;i++)
{
yield return input.Substring(i, length);
}
}
Yes this can be done using Regex:
string input = "123456789";
int size = 3; // set to whatever
MatchCollection split = Regex.Matches(input, #$"\d{{size}}");
string delimited = string.Join(",", split);
Or as an extension method:
public static string Delimit(this string s, int size)
{
MatchCollection split = Regex.Matches(input, #$"\d{{size}}");
return delimited = string.Join(",", split);
}
I basically just brute forced the answer:
import java.util.ArrayList;
public class MyClass {
public static ArrayList<ArrayList<String>> getAllSubets(String input){
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> out = new ArrayList<String>();
for(int i= input.length(); i>=0; i--){
int z = i;
for(int j = 0; j+i<input.length() ;j++){
z++;
out.add(input.substring(j, z));
}
}
list.add(out);
return list;
}
public static void main(String args[]) {
System.out.println(MyClass.getAllSubets("123456789"));
}
}
Output:
[[123456789, 12345678, 23456789, 1234567, 2345678, 3456789, 123456, 234567, 345678, 456789, 12345, 23456, 34567, 45678, 56789, 1234, 2345, 3456, 4567, 5678, 6789, 123, 234, 345, 456, 567, 678, 789, 12, 23, 34, 45, 56, 67, 78, 89, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
This was a fun question to do its easy to get tripped up with the indexes.
Finding all Subsets:
var Input = "123456789";
var Result = Enumerable
.Range(1, Input.Length)
.Select(i => Enumerable
.Range(0, Input.Length - i + 1)
.Select(j => Input.Substring(j, i)))
.ToList();
To restrain to only some levels, like 3, 5, or 8, simply filter before performing the work, as is done below:
var Levels = new List<int>{ 3, 5, 8 };
var Input = "123456789";
var Result = Enumerable
.Range(1, Input.Length)
.Where(i => Levels.Contains(i)) // Filter the levels you need before constructing the subsets.
.Select(i => Enumerable
.Range(0, Input.Length - i + 1)
.Select(j => Input.Substring(j, i)))
.ToList();
Results:
123, 234, 345, 456, 567, 678, 789
12345,
23456,
34567,
45678,
56789
12345678,
23456789

How to make matrix from string in c#

I have string:
string t = "{ { 1, 3, 23 } , { 5, 7, 9 } , { 44, 2, 3 } }"
Can I make a string matrix using string input like this?
I can't simply assign:
int [ , ] matrix = t
Is there some function I could use or do I have to split my string in some way?
PS: 't' string could have various number of rows and columns.
This should serve your purpose
string t = "{ { 1, 3, 23 } , { 5, 7, 9 } , { 44, 2, 3 } }";
var cleanedRows = Regex.Split(t, #"}\s*,\s*{")
.Select(r => r.Replace("{", "").Replace("}", "").Trim())
.ToList();
var matrix = new int[cleanedRows.Count][];
for (var i = 0; i < cleanedRows.Count; i++)
{
var data = cleanedRows.ElementAt(i).Split(',');
matrix[i] = data.Select(c => int.Parse(c.Trim())).ToArray();
}
I came up with something rather similar but with some test cases you might want to think about - hope it helps :
private void Button_Click(object sender, RoutedEventArgs e)
{
int[][] matrix;
matrix = InitStringToMatrix("{ { 1, 3, 23 } , { 5, 7, 9 } , { 44, 2, 3 } }");
matrix = InitStringToMatrix("{ {0,1, 2 ,-3 ,4}, {0} }");
matrix = InitStringToMatrix("{} ");
matrix = InitStringToMatrix("{ {}, {1} } ");
matrix = InitStringToMatrix("{ { , 1,2,3} } ");
matrix = InitStringToMatrix("{ {1} ");
matrix = InitStringToMatrix("{ {1}{2}{3} }");
matrix = InitStringToMatrix(",,,");
matrix = InitStringToMatrix("{1 2 3}");
}
private int[][] InitStringToMatrix(string initString)
{
string[] rows = initString.Replace("}", "")
.Split('{')
.Where(s => !s.Trim().Equals(String.Empty))
.ToArray();
int [][] result = new int[rows.Count()][];
for (int i = 0; i < rows.Count(); i++)
{
result[i] = rows[i].Split(',')
.Where(s => !s.Trim().Equals(String.Empty))
.Select(val => int.Parse(val))
.ToArray();
}
return result;
}
If you like optimizations here's a solution with only one expression:
string text = "{ { 1, 3, 23 } , { 5, 7, 9 } , { 44, 2, 3 } }";
// remove spaces, makes parsing easier
text = text.Replace(" ", string.Empty) ;
var matrix =
// match groups
Regex.Matches(text, #"{(\d+,?)+},?").Cast<Match>()
.Select (m =>
// match digits in a group
Regex.Matches(m.Groups[0].Value, #"\d+(?=,?)").Cast<Match>()
// parse digits into an array
.Select (ma => int.Parse(ma.Groups[0].Value)).ToArray())
// put everything into an array
.ToArray();
Based on Arghya C's solution, here is a function which returns a int[,] instead of a int[][] as the OP asked.
public int[,] CreateMatrix(string s)
{
List<string> cleanedRows = Regex.Split(s, #"}\s*,\s*{")
.Select(r => r.Replace("{", "").Replace("}", "").Trim())
.ToList();
int[] columnsSize = cleanedRows.Select(x => x.Split(',').Length)
.Distinct()
.ToArray();
if (columnsSize.Length != 1)
throw new Exception("All columns must have the same size");
int[,] matrix = new int[cleanedRows.Count, columnsSize[0]];
string[] data;
for (int i = 0; i < cleanedRows.Count; i++)
{
data = cleanedRows[i].Split(',');
for (int j = 0; j < columnsSize[0]; j++)
{
matrix[i, j] = int.Parse(data[j].Trim());
}
}
return matrix;
}

How can I get the sum (average) value of three lists in C#?

I have three lists which each list represents only 0s and 1s which related to the pixel values of three images.
My question is how can I get the sum (average) of those three lists and represent it in a new list?
here is example of my image1:
List<int> image1 = new List<int>();
int blackColor = 0;
for (int x = 0; x < bmp1.Width; x++)
{
for (int y = 0; y < bmp1.Height; y++)
{
Color color = bmp1.GetPixel(x, y);
if (color.ToArgb() == Color.Black.ToArgb())
{
image1.Add(0);
blackColor++;
}
else
{
image1.Add(1);
}
}
}
Let me makes sure I understand the problem. You have three lists of the same length:
list A: 1, 2, 4, 3
list B: 3, 2, 4, 1
List C: 2, 7, 1, 8
and you wish to get a third list that is the average of each:
List D: 2, 4, 3, 4
Yes?
This is a job for zip join.
var sumOfFirstTwo = list1.Zip(list2, (x, y)=>x + y);
sumOfFirstTwo is now the sequence that is the sum of the first two lists.
var sumOfAllThree = sumOfFirstTwo.Zip(list3, (x, y)=>x + y);
sumOfAllThree is now the sequence that is the sum of all three lists.
var average = sumOfAllThree.Select(x=>x/3).ToList();
Make sense?
This works for an arbitrary number of lists
var firstList = new[] { 1, 2, 3, 1 };
var secondList = new[] { 2, 3, 1, 1 };
var thirdList = new[] { 3, 1, 2, 2 };
var lists = new[] { firstList, secondList, thirdList };
var listLengths = lists.Select(x => x.Count());
if (listLengths.Distinct().Count() != 1)
throw new Exception("Line lengths must be the same");
var lengthOfEachList = listLengths.First();
var averages = new List<double>();
for (var i = 0; i != lengthOfEachList; ++i) {
averages.Add(lists.Average(x => x[i]));
}
The LINQ way would be
var averages = Enumerable.Range(0, lengthOfEachList).Select(x => lists.Average(y => y[x]));

how to add array elements in one array according to condition in other array in C#?

I have two arrays say one is string array and the other is int array
string array has---> "11","11","11","11","12","12" elements and the int array has 1,2,3,4,5,6 respectively.
I want result two arrays containing string array--->"11","12"
and int array---->10,11
If the string array has duplicate elements, the other array containing that respective index value must be added .For example "11" is in 1st,2nd,3rd,4th index So its corresponding value must sum of all those elements in other array.Can it be done?
I have written some code but unable to do it..
static void Main(string[] args)
{
//var newchartValues = ["","","","","","",""];
//var newdates = dates.Split(',');
//string[] newchartarray = newchartValues;
//string[] newdatearray = newdates;
int[] newchartValues = new int[] { 1, 2, 3, 4, 5, 6 };
string[] newdates = new string[] { "11", "11","11","12","12","12" };
int[] intarray = new int[newchartValues.Length];
List<int> resultsumarray = new List<int>();
for (int i = 0; i < newchartValues.Length - 1; i++)
{
intarray[i] = Convert.ToInt32(newchartValues[i]);
}
for (int i = 0; i < newdates.Length; i++)
{
for (int j = 0; j < intarray.Length; j++)
{
if (newdates[i] == newdates[i + 1])
{
intarray[j] += intarray[j + 1];
resultsumarray.Add(intarray[j]);
}
}
resultsumarray.ToArray();
}
}
I don't quite get what you need, but I think I fixed your code, result will contain 10 and 11 in this example:
int[] newchartValues = new int[] { 1, 2, 3, 4, 5, 6 };
string[] newdates = new string[] { "11", "11", "11", "11", "12", "12" };
List<int> result = new List<int>();
if (newdates.Length == 0)
return;
string last = newdates[0];
int cursum = newchartValues[0];
for (var i = 1; i <= newdates.Length; i++)
{
if (i == newdates.Length || newdates[i] != last)
{
result.Add(cursum);
if (i == newdates.Length)
break;
last = newdates[i];
cursum = 0;
}
cursum += newchartValues[i];
}
Here is an approach that should do what you want:
List<int> resultsumarray = newdates
.Select((str, index) => new{ str, index })
.GroupBy(x => x.str)
.Select(xg => xg.Sum(x => newchartValues[x.index]))
.ToList();
Result is a List<int> with two number: 6, 15
Something like this?
int[] newchartValues = new int[] { 1, 2, 3, 4, 5, 6 };
int[] newdates = new int[] { 11, 11,11,12,12,12 };
var pairs = Enumerable.Zip(newdates, newchartValues, (x, y) => new { x, y })
.GroupBy(z => z.x)
.Select(g => new { k = g.Key, s = g.Sum(z => z.y) })
.ToList();
var distinctDates = pairs.Select(p => p.k).ToArray();
var sums = pairs.Select(p => p.s).ToArray();

Categories

Resources