How to perform comparison function in C #

Let's say that I have elements (X, Y and Z) in the list, I have a function that generates a percentage of which two objects are similar to each other.

What I want to do is run X against Y and Z using my Elements comparisons, therefore:

compareElements(X,Y); // equals 55
compareElements(X,Z); // equals 60

Then Y versus X and Z

compareElements(Y,X); // equals 55
compareElements(Y,Z); // equals 62

Then Z vs Y and X

compareElements(Z,X); // equals 60
compareElements(Z,Y); // equals 62

Then I return the highest value equal to 62.

Obviously there is a repetition there, I do not need a repetition, but I am not sure how to eliminate it.

How can I structure my LINQ query or function / algorithm for this comparison for each element without repeating?

LINQ, , , , , , .

, - , , 62.

. . 3 10 , .

+5
7

:

IEnumerable<Tuple<T, T>> GetComparisons<T>(IEnumerable<T> elements)
{
    var visited = new List<T>();

    foreach(T current in elements)
    {
        foreach(T previous in visited) 
            yield return new Tuple<T, T>(current, previous);

        visited.Add(current);
    }
}

:

var highScore = GetComparisons(listOfElements)
                    .Select(x=>compareElements(x.Item1, x.Item2)
                    .Max();

( , Smelch , LINQ , , .)

+2

:

int count = list.Count;
var query = from index1 in Enumerable.Range(0, count)
            from index2 in Enumerable.Range(index1 + 1, count - (index1 + 1))
            select ComputeSimilarity(list[index1], list[index2]);
var maxSimilarity = query.Max();
+8

, , - :

    public int compareElementList(List<Element> elements)
    {
        int result = 0;
        for (int i = 0; i < elements.Count - 1; i++)
        {
            for (int q = i + 1; q < elements.Count; q++)
            {
                result = Math.Max(result, compareElements(elements[i], elements[q]));
            }
        }

        return result;
    }

. LINQ, , .

UPDATE. IEnumerables. , , .

    public int compareElementEnumerable(IEnumerable<Element> elements)
    {
        int result = 0, i = 0, q = 1;
        foreach (Element el in elements)
        {
            foreach (Element el2 in elements)
            {
                if (q > i)
                {
                    result = Math.Max(result, compareElements(el, el2));
                }
                q++;
            }
            i++;
        }

        return result;
    }
+5

, , List<Tuple<int, int>>

 mylist.Select(i => new [] { Tuple.New(i.X, i.Y}, Tuple.New(i.X, i.Z), Tuple.New(i.Y, i.Z)})
       .Max(t => compareElements(t.First, t.Second))
+2

, , , LINQ :

var linq = from el1 in list
           from el2 in list
           where el1 != el2
           select CompareFunction(el1, el2);

int max = linq.Max();

:

int CompareFunction(string a, string b)
{
    return a.Length - b.Length;
}

That way, you are comparing each item with the other items in the list (it's some kind of permutation, I think), except for yourself, then select the comparison value and finally the highest value.

+2
source

Pretty much the same as a pair, but does not require one to assign a list in the first place.

List<Element> soFar = new List<Element>();
// If I expected a good few duplicate values, and if 
// compareElements(x, x) isn't 100% - i.e. it not a similarity
// check for example, then I'd use HashSet<Element> and skip
// when .Add() fails.

int result = 0;
foreach(Element el in sourceEnumeration)
{
  for(int i = 0; i != soFar.Count; ++i)
  {
    int cmp = compareElements(el, soFar[i]);
    if(cmp > result)
    {
      if(cmp == 100)
        return 100;
      cmp = result;
    }
  }
  soFar.Add(el);
}
return result;
+1
source

Unread LINQ implementation (may not compile, I have not tested):

Enumerable.Range(0, listOfElements.Length).ToList().ForEach(i=>Enumerable.Range(i, listOfElements.Length-i-1).ToList().ForEach(j=>compareElements(listOfElements[i], listOfElements[j]))).Max();
0
source

All Articles