I have an array:
int[] months = new int[4] {1, 4, 7, 10};
I would like to sort the array starting by the given value and sort the rest of the array in the original order.
Let's say I want to start sorting the array by a value of 7. The sorted array would be then in order of:
7, 10, 1, 4
Or starting with a value 4 the sorted array would be an order of
4, 7, 10, 1
How about:
var orderedMonths = months.Where(x => x >= 7)
.OrderBy(x => x)
.Concat(months.Where(x => x < 7));
Note that this will mean that the elements of the "rest of the array" will be in order of appearance rather than increasing numeric order. If you meant the latter (i.e. sort both 'segments' numerically) , I would do:
var orderedMonths = months.OrderBy(x => x < 7) // false comes before true
.ThenBy(x => x);
On the other hand, if you want to sort both segments by order of appearance, I would do:
var orderedMonths = months.GroupBy(x => x < 7)
.OrderBy(group => group)
.SelectMany(x => x);
(or)
var orderedMonths = months.Where(x => x >= 7)
.Concat(months.Where(x => x < 7));
Assuming this is your sorted int array you could
int[] months = new int[4] { 1, 4, 7, 10 };
int value = 10;
int[] chk1 = new int[4];
chk1 = months.SkipWhile(a => a != value).
Concat(months.TakeWhile(a => a != value)).ToArray();
This should get you the required order
Can you use a list?
int NumberToBeFound = 7;
int IndexOfNumber = -1;
for(int i=0;i<months.count;i++){
if(months[i] == NumberToBeFound){
IndexOfNumber = i;
break;
}
}
List<int> Sorted = new List<int>();
for(int i = IndexOfNumber; i < months.count;i++){
Sorted.Add(months[i]);
}
for(int i = 0; i < IndexOfNumber; i++){
Sorted.Add(months[i]);
}
months = Sorted.ToArray();
Related
I have int array representing elements of square matrix. I need to get rows,columns, 2 main diagonals of it. I have trouble with getting second diagonal (for 3*3 matrix it is elements with indexes 2,4,6 for 6*6 - 5,10,15,20,25,30). I'm wondering is there a neat way to do it without for loops.
var matrix = new int[] { 6, 7, 2, 1, 5, 9, 8, 3, 4 };
int size = 3;
int i = 0;
var rows = matrix.GroupBy(x => i++ / size);
i = 0;
var columns = matrix.GroupBy(x => i++ % size);
var diag1 = matrix.Where((x, index)=>index%(size+1)==0);
var diag2 = matrix.Where((x, index) => index % (size - 1) == 0);//PROBLEM - takes 0,8 indexes also.
Try this
var matrix = new int[] { 6, 7, 2, 1, 5, 9, 8, 3, 4 };
int size = 3;
var rows = matrix.Select((x,i) => new {x = x, i = i}).GroupBy(x => x.i / size).Select(x => x.Select(y => y.x).ToArray()).ToArray();
var columns = matrix.Select((x, i) => new { x = x, i = i }).GroupBy(x => x.i % size).Select(x => x.Select(y => y.x).ToArray()).ToArray();
var diag1 = matrix.Where((x, index) => (index /size) == (index % size)).Select(x => x).ToArray();
var diag2 = matrix.Where((x, index) => (index / size) == (size - 1) - (index % size)).Select(x => x).ToArray();
given:
int[] myArray = new int[]{-8, -17, 12, 8, 16, -3, 7, 3};
Find all values in array closest to 0.
Currently using another code found on the site but it only tells me 1 value.
int nearest = myArray.Select(p => new {Value = p, Difference = Math.Abs(p - searchValue)}).OrderBy(p => p.Difference).First().Value;
In the current case both -3 and 3 are closest to 0 but since -3 comes first it only outputs -3.
Is there another way to do this where it finds all values instead of just First()?
Just to add to this. If you want to do this using O(n) complexity without sorting then you can do it this way :
public List<int> GetClosestNumbers(int searchVal)
{
int[] myArray = new int[] { -8, -17, 12, 8, 16, -3, 7, 3 };
int minimumDist = int.MaxValue;
List<int> output = new List<int>();
for (int i = 0; i < myArray.Length; i++)
{
var currentDistance = Math.Abs(myArray[i] - searchVal);
if (currentDistance < minimumDist)
{
minimumDist = currentDistance;
output.Clear();
output.Add(myArray[i]);
}
else if (minimumDist == currentDistance)
{
output.Add(myArray[i]);
}
}
return output;
}
Use a grouping on the lowest absolute value
int[] result = myArray
.OrderBy(i => Math.Abs(i - searchValue))
.GroupBy(i => Math.Abs(i - searchValue))
.First()
.ToArray();
I want to return elements between two variable indexes in a list.
For example, given this list -
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
I want to loop through the list using to variables for index values. Let’s call the index values X and Y.
So if X equals an index value of 0 and Y equals a value of 5, I need to loop through the index 0-5 and return all of the element values. X and Y could later become index values of 5 through 8 for example.
How would I accomplish this?
You can use Enumerable.Skip and Enumerable.Take
var res = list.Skip(noOfElementToSkip).Take(noOfElementsToTake);
To using variable as indexes
var res = list.Skip(x).Take(y-x+1);
Note You need to pass the start element index to Skip and for taking number of elements you need to pass number of element you want in Take parameter minus the start element number, plus one list is zero-based index.
you can use List.GetRange
var result = list.GetRange(X, Y-X+1);
or a simple for loop
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = X; i <= Y; i++)
{
Console.WriteLine(list[i]);
}
or reinventing the wheel the way you want
public static class Extensions
{
public static IEnumerable<T> GetRange<T>(this IList<T> list, int startIndex, int endIndex)
{
for (int i = startIndex; i <= endIndex; i++)
{
yield return list[i];
}
}
}
foreach(var item in list.GetRange(0, 5))
{
Console.WriteLine(item);
}
int x = 0, y = 5;
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (; x < y; x++)
{
Console.WriteLine(list[x]);
}
This would work if X is always smaller than Y.
If you don't know which is bigger add this before the loop:
if (x > y)
{
x = x ^ y;
y = y ^ x;
x = x ^ y;
}
Another alternative:
int X = 0, Y = 5;
Enumerable.Range(X, Y - X + 1)
.Select(index => list[index]);
It should do the trick -
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int startindex = 1;
int endIndex = 7;
var subList = list.Skip(startindex).Take(endIndex - startindex-1).ToList();
I have a list of numbers as below:
var mylist = new List<double> {1, 5, 8, 10, 12};
How can I get the number after a specific number. I want an LINQ expression that receives for example 8 and gives me 10;
You can use the following:
double result = mylist.SkipWhile(n => n != 8).Skip(1).First();
This should also work if you do not have duplicate numbers.
int index = mylist.IndexOf(8);
if (index != -1)
{
double result = index == mylist.Count - 1 ? mylist[index] : mylist[index + 1];
}
This should work,
var mylist = new List<double> { 1, 5, 8, 10, 12 };
double p = 8;
var result = mylist.Where(x => x > p).Take(1).SingleOrDefault();
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]));