Csharp updates for 4, 9 chapters (#245)

* GetGCDList fix

* Csharp longest_common_subsequence update

* Csharp levenshtein added

* Couple changes in levenshtein
This commit is contained in:
Alexandr
2023-07-19 21:49:54 +06:00
committed by GitHub
parent f53fe3b98a
commit ed5a723fa5
3 changed files with 127 additions and 20 deletions

View File

@@ -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<int> lst)
{
var result = lst[0];
result = GetGCD(result, lst.Skip(1).FirstOrDefault());
return result;
}
static void Main(string[] args)
{
var lst = new List<int> { 32,696,40,50 };
var GCD = GetGCD( 640, 1680);
var lst = new List<int> { 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<int> 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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}