5

Actually it's quite hard to describe:
I want to implement an algorithm which compares figure by figure of the same position (as I do my calculations in a 10-based system it's rather the same "power of ten") of two given integers/number (with the same "length"). It should return the grade of equality as following:

  • 4491 and 1020 = 0
  • 4491 and 4123 = 1
  • 4491 and 4400 = 2
  • 4491 and 4493 = 3
  • 4491 and 4491 = 4
  • 4491 and 4091 = 1

I do not want to do my calculations based on a string-comparison, as I'll doing this in a way bigger scenario :)

6 Answers6

3
public static int Compare(int i1, int i2)
{
    int result = 0;
    while(i1 != 0 && i2 != 0)
    {
        var d1 = i1 % 10;
        var d2 = i2 % 10;
        i1 /= 10;
        i2 /= 10;
        if(d1 == d2)
        {
            ++result;
        }
        else
        {
            result = 0;
        }
    }
    if(i1 != 0 || i2 != 0)
    {
        throw new ArgumentException("Integers must be of same length.");
    }
    return result;
}

Note: it does not handle negative integers

Update: fixed after question update

max
  • 33,369
  • 7
  • 73
  • 84
  • I like this (after my solution went down in flames...) What does it do if the numbers are different lengths? – Rawling May 15 '12 at 13:40
  • 2
    Well, it is not handled (though it is very easy to add). OP didn't defined any behavior for this case, so i'll just throw an exception. – max May 15 '12 at 13:43
  • Well, you've got my +1. Hopefully some of the others will take note too. – Rawling May 15 '12 at 13:46
1

See the Answer to this SO Question

You can Split the digits by the first method and Get the Similarity from the Second Method:

int[] GetIntArray(int num)
{
    List<int> listOfInts = new List<int>();
    while(num > 0)
    {
        listOfInts.Add(num % 10);
        num /= 10;
    }
    listOfInts.Reverse();
    return listOfInts.ToArray();
}

int GetSimilarity(int firstNo, int secondNo)
{
    int[] firstintarray = GetIntArray(firstNo)
    int[] secondintarray = GetIntArray(secondNo)
    if (firstintarray.Count != secondintarray.Count)
    {
        throw new ArgumentException("Numbers Unequal in Length!");
    }
    int similarity = 0;
    for(i = 0; i < firstintarray.Count; i++)
    {
        if (secondintarray[i] = firstintarray[i])
        {
            similarity++;
            continue;
        }
        break;
    }
}

Now you can Compare the the two int arrays like this :

int Similarity = GetSimilarity(4491, 4461);// Returns 2
Community
  • 1
  • 1
Writwick
  • 2,133
  • 6
  • 23
  • 54
  • interesting ... but too much array-handling which is on the performance-down-side ... –  May 15 '12 at 13:14
1

For all cases where X and Y are not equal:

Length - Math.Floor(Math.Log10(Math.Abs(X - Y)) + 1)

4491 and 1020

4 - Math.Floor(Math.Log10(Math.Abs(4491 - 1020)) + 1) = 0

4491 and 4493

4 - Math.Floor(Math.Log10(Math.Abs(4491 - 4493)) + 1) = 3
Myxville
  • 31
  • 7
1

Just to try to salvage something from this question after my last attempt...

int Compare(int x, int y)
{
    int pow10 = (int)Math.Pow(10, Math.Floor(Math.Log(Math.Max(x, y), 10)));
    int matches = 0;
    while(pow10 > 0 && (x / pow10) == (y / pow10))
    {
        matches++;
        pow10 /= 10;
    }
    return matches;
}
Rawling
  • 49,248
  • 7
  • 89
  • 127
0

It sounds like the Levenshtein Distance would be appropriate. This is a standard way to measure the difference between two strings. In your case, the strings are the decimal representations of the numbers.

Joshua Frank
  • 13,120
  • 11
  • 46
  • 95
-1

I thing the best way to calculate it is using Euclidean Similarity.

Please see this link: http://stackoverflow.com/questions/11555355/calculating-the-distance-between-2-points

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321