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 need a way to compare two array and compute the Equivalence percentage
so if equivalence Percentage exceed (for example 60%) do some actions
used language is C# .NET 4.0
The question is poorly defined, so I've taken some broad assumptions, but here's a sample implementation that measures equivalence based on element equality:
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 1, 7, 3, 4 };
int equalElements = a.Zip(b, (i, j) => i == j).Count(eq => eq);
double equivalence = (double)equalElements / Math.Max(a.Length, b.Length);
if (equivalence >= .6)
{
// 60%+ equivalent
}
Zip: "Applies a specified function to the corresponding elements of two sequences." In this case, we're comparing each element from a with the corresponding element from b, and producing true if they're equal. For example, we compare 1 with 1, 2 with 7, 3 with 3, and 4 with 4. We then count the number of equalities we encountered, storing this value into equalElements. Finally, we divide this by the total number of elements in the larger sequence, and thus get the equivalence ratio.
Assuming you're comparing two int lists (or arrays, it's the same) you can compute the percentage of equivalent elements between list1 and list2 this way:
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
List<int> list2 = new List<int>() { 3, 5, 8 };
var res = list1.Intersect(list2).ToList().Count();
float perc = (float)list1.Count() / res;
Related
This question already has answers here:
LINQ Zip all elements
(3 answers)
How to Zip two Lists of different size to create a new list that is same as the size of the longest amongst the original lists?
(4 answers)
Closed 9 months ago.
var firstArray = new int[] { 1, 2, 3 };
var secondArray = new int[] { 4, 5, 6, 5, 9, 10};
var sum = Enumerable.Zip(first, second, (a, b) => a + b);
I want the sum to be [5, 7, 9, 5 , 9 , 10] since firstArray only have 3 elements. Any workaround? Would be better if I am using for loop for this?
I haven't been able to find anything on google.
I have this piece of code:
Random r = new Random();
int[] output = Enumerable.Range(0, 11).Select(x => x / 2).OrderBy(x => r.Next()).ToArray();
and I am having trouble actually understanding what each element does.
It generates a range of numbers and elements between 0 and 11.
But what does the select(x => x / 2) do ? does it just make pairs of elements,
I know what the whole thing spits out, an array with pairs of numbers, with a single number which has no pair.
but it is a bit above me to fully understand it ?
( is this even okay to ask on here ?? or should I delete the question again ? )
It generates a range of numbers and elements between 0 and 11. But what does the select(x => x / 2) do ? does it just make pairs of elements.
No, Select does what in some programming languages is known as map. It is called on an IEnumerable<T> and has as parameter Func<T,U> a function, and it produces an IEnumerable<U> where each element if the given IEnumerable<T> is processes through the function and the result is emitted in the result.
So in this case, it will take a range from 0 to (excluding) 11, and for each of those integers, perform an integer divsion by two:
csharp> Enumerable.Range(0, 11);
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
csharp> Enumerable.Range(0, 11).Select(x => x/2);
{ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5 }
Since:
{ 0/2, 1/2, 2/2, 3/2, 4/2, 5/2, 6/2, 7/2, 8/2, 9/2, 10/2 }
== { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5 }
Later then that IEnumerable<int> is reordered (using the OrderBy) by using pseudo-random numbers (so we shuffle them) and converted into a list.
.Range(0, 11) // generate a sequence of integers starting at 0 and incrementing 11 times (i.e. the values 0 up to and including 10)
.Select(x => x / 2) // divide each of those values from previous result by 2 and return them
.OrderBy(x => r.Next()) // then order them randomly using a random number
.ToArray(); // return the end result as an array
Think of Select as doing a transformation to each element of the IEnumerable.
For example, let's say we have a list like this:
0 1 2 3 4 5 6 7 8 9 10
Then we call .Select(x => x / 2), we are saying that for each element x in the list, do the following transformation:
x / 2
We divide each element in the list by two:
Original Transformation Result
0 0 / 2 0
1 1 / 2 0
2 2 / 2 1
3 3 / 2 1
4 4 / 2 2
5 5 / 2 2
6 6 / 2 3
7 7 / 2 3
8 8 / 2 4
9 9 / 2 4
10 10 / 2 5
We get
0 0 1 1 2 2 3 3 4 4 5 5
What Select() does is that it evaluates the given expression for every element of the Enumerable it's called on (the original list), and returns a new Enumerable with the results.
For a list:
[2, 4, 6]
it's going to return:
[2/2, 4/2, 6/2]
where / means "division", so the result of the Select() (not the entire LINQ chain) will be:
[1, 2, 3]
Analogously, if your source list is:
words = ["dog", "child", "building"]
And you call:
words.Select(word => word.Length)
you get a list of all the lengths of the strings in the list in order:
[3, 5, 7]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's say I have array which contains unique random numbers (where numbers have small possible range of 0 to 20). For example:
[6, 3, 11, 9, 4, 5]
How can I convert following array to something like this:
[3, 0, 5, 4, 1, 2]
The second array starts from 0 and ends with (array.Length-1), but placement is relative to magnitude in the first array.
How can I implement this in an efficient way in C/C++/C#? (more interested in the method)
I gave just one example. It can be really anything:
[7, 10, 0, 19, 50, 33, 45, 100]
[1, 2, 0, 3, 6, 4, 5, 7]
Smallest number from array A is 0 in array B. Biggest number in array A is (array.Length-1) in array B. Array A can be completely random (just it will never contain two or more identical numbers), but array A have to contain all numbers from 0 to array.Length-1) in same order as in array A.
int[] list1 = new[] { 7, 10, 0, 19, 50, 33, 45, 100 };
var orderedList = list1.OrderBy(x => x).ToList();
int[] list2 = list1.Select(x => orderedList.IndexOf(x)).ToArray();
EDIT
Per #Blorgbeard's request
int[] list1 = new[] { 6, 3, 11, 9, 4, 5 };
var dict = list1.OrderBy(x => x)
.Select((i, inx) => new { i, inx })
.ToDictionary(x => x.i, x => x.inx);
int[] list2 = list1.Select(x => dict[x]).ToArray();
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Assuming you have a sequence like this: 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, etc. Basically a sequence consisting of N numbers, repeating over and over.
What is the simplest algorithm for finding the distance/difference between two numbers in this sequence? For example the distance from 5 to 7 is +2 and the distance from 0 to 6 is -2. For a more high level view what I have a looping/repeating sequence number, and I need to find out how much "before" or "after" a number is of another on the closest path (fewest number between them).
Assuming X>Y:
dist(X, Y) = min { X-Y, N-(X-Y-1) }
Examples for N = 7:
dist(7, 5) = min {7-5, 7-(7-5-1)} = min {2, 6} = 2
dist(6, 0) = min {6-0, 7-(6-0-1)} = min {6, 2} = 2
dist(5, 1) = min {5-1, 7-(5-1-1)} = min {4, 4} = 4
The last example points a small flaw in your distance definition: Is dist(5, 1) = 4 or dist(5, 1) = -4 ? I've changed your definition a little bit to avoid negative distances (so my algorithm calculates the absolute value of the distance). If you want to keep your definition then make the distance negative if and only if the first argument of the min is greater than the second.
This is quite simple and work with consecutive and non consecutive numbers, maybe make it an extension method :
[TestFixture]
public class NumbersFixture
{
[Test]
public void FindDistance()
{
var numbers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2 };
var num1 = 6;
var num2 = 0;
var from = numbers.IndexOf(num1);
var indexFound = numbers.FindIndex(from, f => f == num2);
var distance = from - indexFound;
var result = string.Format("{0}", distance);
Console.WriteLine(result);
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Finding all possible combinations of numbers to reach a given sum
I have to create method which from array of numbers selects numbers, which sum will be exact as required one or if such doesn't exist select the minimal greater one.
What would be the algorithm of this function?
public int[] selectExactSum(int[] X, int SUM) {
}
example:
numbers are: {5, 2, 8, 4, 6} and required sum is 12.
The result would be: {2, 4, 6}
If required sum is 13, the result would be:{2, 8, 4} - so, the sum would be in this case 14 - the first minimal greater one.
If Required sum is 15, the possible results would be: {5, 2, 8} or {5, 4, 6}. In this case return the one of your choice - probably the first one you get.
What would be the algorithm for custom numbers and sum?
Thanks,
Simon
This is a generalized case of the problem called subset sum. It's an NP-complete problem, thus the best known algorithm is pseudo-polynomial.
If you understand the above linked algorithm, you can deduct the modification necessary to solve your problem.
How about recursively?
public static int[] SelectExactSum(int[] x, int sum) {
int[]
rest = x.Skip(1).ToArray(),
with = x.Length == 1 ? x : x.Take(1).Concat(SelectExactSum(rest, sum - x[0])).ToArray(),
without = x.Length == 1 ? new int[0] : SelectExactSum(rest, sum);
int withSum = with.Sum(), withoutSum = without.Sum();
return
withSum >= sum ? (withoutSum >= sum ? (withSum < withoutSum ? with : without) : with) :
withoutSum >= sum ? without : new int[0];
}
Note: Calling SelectExactSum(new int[] {5,2,8,4,6}, 13) doesn't return {2,8,4} as stated in the question, but {5,8} as that actually sums up to 13.
Took me about 15 minutes to made it, you can see it running here:
http://jesuso.net/projects/selectExactSum/index.php?numbers=5%2C2%2C8%2C4%2C6&reqSum=15
And here's the code:
http://jesuso.net/projects/selectExactSum/selectExactSum.txt
I made it as simple as possible, but its made on PHP, let me know if you need some help translating it to c#.