Advertisement
ArchieCoder

Untitled

Sep 14th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | Software | 0 0
  1. using System.Diagnostics;
  2.  
  3. namespace MTGOImporter
  4. {
  5.     internal class Program
  6.     {
  7.         private string[] _allCards = new string[]
  8.         {
  9.             "Plains",
  10.             "Mountain",
  11.             "Island",
  12.             "Swamp",
  13.             "Forest",
  14.             "Up the Beanstalk",
  15.             "Lórien Revealed",
  16.             "Fire/Ice",
  17.             "Fireblast",
  18.             "Fireball"
  19.         };
  20.        
  21.         static void Main(string[] args)
  22.         {
  23.             var closestCard1 = FindClosestCard("Fire // Ice");
  24.             Debug.Assert(closestCard1 == "Fire/Ice");
  25.  
  26.             var closestCard2 = FindClosestCard("L356ie Revealed");
  27.             Debug.Assert(closestCard2 == "Lórien Revealed");
  28.  
  29.             var closestCard3 = FindClosestCard("Plains");
  30.             Debug.Assert(closestCard3 == "Plains");
  31.  
  32.             var closestCard4 = FindClosestCard("Forest");
  33.             Debug.Assert(closestCard4 == "Forest");
  34.         }
  35.  
  36.         private static string FindClosestCard(string input)
  37.         {
  38.             string[] cardList = { "Plains", "Mountain", "Island", "Swamp", "Forest", "Up the Beanstalk", "Lórien Revealed", "Fire/Ice", "Fireblast", "Fireball" };
  39.             string closestCard = string.Empty;
  40.             int closestDistance = int.MaxValue;
  41.  
  42.             foreach (string card in cardList)
  43.             {
  44.                 int distance = ComputeLevenshteinDistance(card, input);
  45.  
  46.                 if (distance < closestDistance)
  47.                 {
  48.                     closestDistance = distance;
  49.                     closestCard = card;
  50.                 }
  51.             }
  52.  
  53.             return closestCard;
  54.         }
  55.  
  56.         private static int ComputeLevenshteinDistance(string s, string t)
  57.         {
  58.             int[,] distance = new int[s.Length + 1, t.Length + 1];
  59.  
  60.             for (int i = 0; i <= s.Length; i++)
  61.             {
  62.                 distance[i, 0] = i;
  63.             }
  64.             for (int j = 0; j <= t.Length; j++)
  65.             {
  66.                 distance[0, j] = j;
  67.             }
  68.  
  69.             for (int j = 1; j <= t.Length; j++)
  70.             {
  71.                 for (int i = 1; i <= s.Length; i++)
  72.                 {
  73.                     if (s[i - 1] == t[j - 1])
  74.                     {
  75.                         distance[i, j] = distance[i - 1, j - 1];
  76.                     }
  77.                     else
  78.                     {
  79.                         distance[i, j] = Math.Min(distance[i - 1, j] + 1, Math.Min(distance[i, j - 1] + 1, distance[i - 1, j - 1] + 1));
  80.                     }
  81.                 }
  82.             }
  83.  
  84.             return distance[s.Length, t.Length];
  85.         }
  86.     }
  87. }
Tags: MTGO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement