Compute difference of numbers of 2 arrays [closed] - c#

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 8 years ago.
Improve this question
for each number in A and each number in B if their difference is more than 3 ,add the new number
A={6,7,10}
B={2,3}
result={4,5,4,8,7}
for example :
6-2=4 true Add 4
6-3=3 false
7-2=5 true Add 5
my attempt :
var result = A.Select((a, i) => new
{
desired = a - B[i] > 3 ? a - B[i] :0
});

It sounds like you want:
var query = from a in A
from b in B
let diff = a - b
where diff > 3
select diff;
Or in dot notation, which drops out really neatly:
var query = A.SelectMany(a => B, (a, b) => a - b)
.Where(x => x > 3);
With your test data:
using System;
using System.Linq;
class Test
{
public static void Main()
{
int[] A = {6, 7, 10};
int[] B = {2, 3};
var query = A.SelectMany(a => B, (a, b) => a - b)
.Where(x => x > 3);
foreach (var result in query)
{
Console.WriteLine(result);
}
}
}

var query = from a in A
from b in B
where (a - b) > 3
select a - b;

Related

Linq OrderByDescending after that ThenByDescending in C#

I have list of integer and I am trying to sort this based on first in descending order and after that I want to sort with in two list based on even and odds but still in descending order like this
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var numbers = new List<int>() { 3, 7, 1, 5, 4, 6, 2 };
var sortedDescendingNumbers = numbers.OrderByDescending(x => x);
var sortedNumbers = sortedDescendingNumbers.ThenByDescending(x => x % 2 == 0);
foreach (var num in sortedNumbers)
{
Console.Write(num + " ");
}
}
}
but this one print the result as 7 6 5 4 3 2 1 and I am expecting output like 7 5 3 1 6 4 2
Your first block of numbers should be the odd ones, so you have to order by divisibility by 2 first and then by descending value.
var sortedOddEvenNumbers = numbers.OrderBy(x => x % 2 == 0);
var sortedNumbers = sortedOddEvenNumbers.ThenByDescending(x => x);

Finding the smallest closest value in an array [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 2 years ago.
Improve this question
I have the code as below
var searchValues = new double[] { 21.1, 21.65, 22.2, 22.75, 23.3, 23.85, 24.4, 24.95, 25.5, 26.05, 26.6, 27.15, 27.7, 28.25, 28.8, 29.35, 29.9, 30.45, 31, 31.55, 32.1, 32.65, 33.2, 33.75, 34.3, 34.85, 35.4, 35.95 };
var searchValue = 22;
double nearest = searchValues .Select(p => new { Value = p, Difference = Math.Abs(p - searchValue ) })
.OrderBy(p => p.Difference)
.First().Value;
This code returns me 22.2 . However i want the result to be the smallest value close to 22, i.e it will be 21.65. How do i achieve this? I am relatively new to C# and would appreciate some help.
As per your expected output you are trying to find closed value which is less than searchValue. Try below
var searchValues = new double[] { 21.1, 21.65, 22.2, 22.75, 23.3, 23.85, 24.4, 24.95, 25.5, 26.05, 26.6, 27.15, 27.7, 28.25, 28.8, 29.35, 29.9, 30.45, 31, 31.55, 32.1, 32.65, 33.2, 33.75, 34.3, 34.85, 35.4, 35.95 };
var searchValue = 22;
double nearest = searchValues
.Where(x => x <= searchValue) //Filter all values which are less than or equal to searchValue
.OrderBy(y=> y) //Order resultant values by ascending order
.LastOrDefault(); //Get the max value from an array.
Console.WriteLine(nearest == 0 && searchValue > 0 ? "Invalid Input" : $"Nearest Value : {nearest}");
.net Fiddle
You can just apply Math.Round() in difference.
var searchValues = new double[] { 21.1, 21.65, 22.2, 22.75, 23.3,
23.85, 24.4, 24.95, 25.5, 26.05, 26.6, 27.15, 27.7,
28.25, 28.8, 29.35, 29.9, 30.45, 31, 31.55, 32.1,
32.65, 33.2, 33.75, 34.3, 34.85, 35.4, 35.95 };
var searchValue = 22;
double nearest = searchValues.Select(p => new { Value = p, Difference
= Math.Round(Math.Abs(p - searchValue)) })
.OrderBy(p => p.Difference)
.First().Value;
Console.WriteLine(nearest);

Compare to strings and get the distinct in c# [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 8 years ago.
Improve this question
Example :
string a= "6,9";
string b= "5,9";
on comparing the above i need unique elements
result:
a=6;
b=5;
string a= "6,9"; string b= "5,9";
string uniquea = string.Join(",", a.Split(',').Except(b.Split(',')));
string uniqueb = string.Join(",", b.Split(',').Except(a.Split(',')));
This will keep duplicates within a and b.
string a= "6,9"; string b= "5,9";
char[] splitters = new[] { ',', ' '};
var aList = a.Split(splitters);
var bList = b.Split(splitters);
var uniqueA = aList.Except(bList).ToList();
var uniqueB = bList.Except(aList).ToList();
string[] assets, allassets = null;
int[] list1, list2, removed_list, added_list = null;
assets = a.Split(',');
list1 = Array.ConvertAll(assets, x => int.Parse(x))
allassets = b.Split(',');
list2 = Array.ConvertAll(allassets, x => int.Parse(x));
removed_list = list2.Where(x => !list1.Contains(x)).ToArray(); // which gives =>a
added_list = list1.Where(x => !list2.Contains(x)).ToArray(); // which gives =>b
I am done with this ,What u people say ?

Grouping A List - Looking For A Better Way [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 8 years ago.
Improve this question
I have a list something like that
var x = new List<string>{"A","B","C","D","E","F"};
I want to group it in doubles like that
1-A
1-B
2-C
2-D
3-E
3-F
What my solution is
var result = new Dictionary<int, List<string>>();
var sequence = 1;
var groupList = new List<string>();
for (var i = 0; i < x.Length ; i++)
{
groupList.Add(x[i]);
if ((i + 1)%2 == 0)
{
result.Add(sequence, groupList);
groupList = new List<string>();
sequence++;
}
}
return result;
I am looking for alternative (better) ways to get same result.
Here is another way:
int numPairs = 3;
char[] abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
var dict = new Dictionary<int, char[]>();
for (int i = 1, k=1; i <= numPairs && k<abc.Length; i++, k+=2)
{
dict.Add(i, new char[] { abc[k - 1], abc[k] });
}
return dict;
This will do the same.
var y = x.GroupBy(i =>Convert.ToInt32((x.IndexOf(i) + 1.1) * 0.5));
I think this is a readable Linq solution:
var result = x
.Select((value, index) => new { value, index })
.GroupBy(arg => arg.index / 2 + 1, arg => arg.value);
The Select projects the value with its index into an anonymous type, and the GroupBy groups by the index.
If you need the groupings in a Dictionary like in your question, use ToDictionary:
var dictionary = result.ToDictionary(
grouping => grouping.Key,
grouping => grouping.ToList());

How to get from points array points with biggest Y and smallest Y? [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 9 years ago.
Improve this question
I have points Array Points[].
How can I get from this array points with biggest Y and smallest Y.
Thank in advance.
Point Max = Points.MaxBy(p => p.Y);
Point Min = Points.MinBy(p => p.Y);
This is using MoreLINQ.
var maxY= array.Max(x=>x.Y);
var minY= array.Min(x=>x.Y);
Point maxPoint = array.FirstOrDefault(p => p.Y == maxY);
Point minPoint = array.FirstOrDefault(p => p.Y == minY);
Pretty easy with Linq:
var pointYGroups = points.GroupBy(p => p.Y).OrderBy(g => g.Key);
IEnumerable<Point> smallessY_Points = pointYGroups.First();
IEnumerable<Point> largestY_Points = pointYGroups.Last();
// output
foreach (var minPoint in smallessY_Points)
Console.WriteLine("Point: [{0}|{1}]", minPoint.X, minPoint.Y);
Try this:
class Program
{
static void Main(string[] args)
{
Point[] a = new Point[]{
new Point() { X = 0, Y = 3 },
new Point() { X = 1, Y = 3 },
new Point() { X = 2, Y = 1 },
new Point() { X = 3, Y = 1 },
new Point() { X = 1, Y = 2 }
};
var max = a.Max(p1 => p1.Y);
var min = a.Min(p1 => p1.Y);
var maxY = a.Where(p => p.Y == max).ToArray();
var minY = a.Where(p => p.Y == min).ToArray();
}
}
you can use Array.lenth(),and add an element,to get the biggest or smallest.But if you want improve the ability of algorithm,you must learn how the Array deal with the numbers.

Categories