From ed5a723fa5782057f604be2156a643fff3faf168 Mon Sep 17 00:00:00 2001 From: Alexandr <32848544+ManSnowThe@users.noreply.github.com> Date: Wed, 19 Jul 2023 21:49:54 +0600 Subject: [PATCH] Csharp updates for 4, 9 chapters (#245) * GetGCDList fix * Csharp longest_common_subsequence update * Csharp levenshtein added * Couple changes in levenshtein --- 04_quicksort/csharp/06_GetGCD/program.cs | 41 ++++++++----- .../01_longest_common_subsequence/Program.cs | 60 +++++++++++++++++-- .../csharp/02_levenshtein/Program.cs | 46 ++++++++++++++ 3 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 09_dynamic_programming/csharp/02_levenshtein/Program.cs diff --git a/04_quicksort/csharp/06_GetGCD/program.cs b/04_quicksort/csharp/06_GetGCD/program.cs index 64abed2..51bc385 100644 --- a/04_quicksort/csharp/06_GetGCD/program.cs +++ b/04_quicksort/csharp/06_GetGCD/program.cs @@ -4,28 +4,37 @@ using System.Linq; namespace GCD { - public class program + public class Program { - - //Get great Comman Divisor - public static int GetGCD(int FirstNumber, int SecondNumber) - => SecondNumber == default ? FirstNumber : GetGCD(SecondNumber, FirstNumber % SecondNumber); - - //Get great Comman Divisor of list - public static int GetGCDList(List lst) - { - var result = lst[0]; - result = GetGCD(result, lst.Skip(1).FirstOrDefault()); - return result; - } - static void Main(string[] args) { - var lst = new List { 32,696,40,50 }; - var GCD = GetGCD( 640, 1680); + var lst = new List { 32, 696, 40, 50 }; + var GCD = GetGCD(640, 1680); var GCDList = GetGCDList(lst); + Console.WriteLine(GCD); Console.WriteLine(GCDList); } + + //Get great Comman Divisor + public static int GetGCD(int firstNumber, int secondNumber) + => secondNumber == default ? firstNumber : GetGCD(secondNumber, firstNumber % secondNumber); + + //Get great Comman Divisor of list + public static int GetGCDList(IEnumerable lst) + { + var result = lst.FirstOrDefault(); + + if (lst.Count() > 2) + { + result = GetGCD(result, GetGCDList(lst.Skip(1))); + } + else + { + result = GetGCD(result, lst.Skip((1)).FirstOrDefault()); + } + + return result; + } } } \ No newline at end of file diff --git a/09_dynamic_programming/csharp/01_longest_common_subsequence/Program.cs b/09_dynamic_programming/csharp/01_longest_common_subsequence/Program.cs index 63ebdba..83f10d4 100644 --- a/09_dynamic_programming/csharp/01_longest_common_subsequence/Program.cs +++ b/09_dynamic_programming/csharp/01_longest_common_subsequence/Program.cs @@ -6,15 +6,67 @@ namespace ConsoleApplication { public static void Main(string[] args) { + var result = LongestCommonSubsequence("fish", "vistafh"); + Console.WriteLine($"{result.Item1}: {result.Item2}"); // ish: 3 + } - if (word_a[i] == word_b[1]) + public static (string, int) LongestCommonSubsequence(string word1, string word2) + { + if (string.IsNullOrEmpty(word1) || string.IsNullOrEmpty(word2)) + return ("", 0); + + string subSeq; + var matrix = new int[word1.Length + 1, word2.Length + 1]; + + for (int i = 1; i <= word1.Length; i++) { - cell[i][j] = cell[i - 1][j - 1] + 1; + for (int j = 1; j <= word2.Length; j++) + { + if (word1[i - 1] == word2[j - 1]) + { + matrix[i, j] = matrix[i - 1, j - 1] + 1; + } + else + { + matrix[i, j] = Math.Max(matrix[i, j - 1], matrix[i - 1, j]); + } + } } - else + + subSeq = Read(matrix, word1, word2); + + return (subSeq, subSeq.Length); + } + + private static string Read(int[,] matrix, string word1, string word2) + { + string subSeq = null; + int x = word1.Length; + int y = word2.Length; + + while (x > 0 && y > 0) { - cell[i][j] = Math.Max(cell[i - 1][j], cell[i][j - 1]); + if (word1[x - 1] == word2[y - 1]) + { + subSeq += word1[x - 1]; + x--; + y--; + } + else if (matrix[x - 1, y] > matrix[x, y - 1]) + { + x--; + } + else + { + y--; + } } + + var charArray = subSeq.ToCharArray(); + Array.Reverse(charArray); + subSeq = new string(charArray); + + return subSeq; } } } diff --git a/09_dynamic_programming/csharp/02_levenshtein/Program.cs b/09_dynamic_programming/csharp/02_levenshtein/Program.cs new file mode 100644 index 0000000..29704c6 --- /dev/null +++ b/09_dynamic_programming/csharp/02_levenshtein/Program.cs @@ -0,0 +1,46 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static int LevenshteinDistance(string source, string target) + { + var matrix = CreateMatrix(source, target); + + for (int i = 1; i <= source.Length; i++) + { + for (int j = 1; j <= target.Length; j++) + { + matrix[i, j] = Math.Min(matrix[i, j - 1] + 1, Math.Min( + matrix[i - 1, j] + 1, + matrix[i - 1, j - 1] + (source[i - 1] != target[j - 1] ? 1 : 0))); + } + } + + return matrix[source.Length, target.Length]; + } + + private static int[,] CreateMatrix(string source, string target) + { + var matrix = new int[source.Length + 1, target.Length + 1]; + + if (source.Length < target.Length) + { + (source, target) = (target, source); + } + + for (int i = 0; i <= source.Length; i++) + { + matrix[i, 0] = i; + + if (i <= target.Length) + { + matrix[0, i] = i; + } + } + + return matrix; + } + } +}