public void Validate(string text1, string text2, int expectedLength) { var sut = new LongestCommonSubsequence(); var result = sut.Solve(text1, text2); Assert.Equal(expectedLength, result); }
public void FixTranspositionTest2() { var orig = "baaaq"; var entered = "zzzaaabbq"; var alignments = LongestCommonSubsequence.LeftAlignedLCS(orig, entered); var additions = LongestCommonSubsequence.GetAddedCharIndices(entered, alignments); var omissions = LongestCommonSubsequence.GetMissingCharIndices(orig, alignments); var fixedouts = LongestCommonSubsequence.FixTranspositions(alignments, additions, omissions, orig, entered); var entered2 = fixedouts.Item1; var alignments2 = fixedouts.Item2; var additions2 = fixedouts.Item3; var omissions2 = fixedouts.Item4; var deltas = fixedouts.Item5; Assert.AreEqual(3, deltas.Head); Assert.AreEqual("bzzzaaabq", entered2); int[] correct_additions = { 1, 2, 3, 7 }; Assert.AreEqual(true, correct_additions.SequenceEqual <int>(additions2)); int[] correct_omissions = { }; Assert.AreEqual(true, correct_omissions.SequenceEqual <int>(omissions2)); Tuple <int, int>[] correct_alignments = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 4), new Tuple <int, int>(2, 5), new Tuple <int, int>(3, 6), new Tuple <int, int>(4, 8) }; Assert.AreEqual(true, correct_alignments.SequenceEqual <Tuple <int, int> >(alignments2)); }
public void TypoString() { var original = "Testing"; var entered = "Tesying"; // get LCS var alignments = LongestCommonSubsequence.LeftAlignedLCS(original, entered); // find all character additions var additions = LongestCommonSubsequence.GetAddedCharIndices(entered, alignments); // find all character omissions var omissions = LongestCommonSubsequence.GetMissingCharIndices(original, alignments); // find all transpositions var outputs = LongestCommonSubsequence.FixTranspositions(alignments, additions, omissions, original, entered); // new string string entered2 = outputs.Item1; // new alignments var alignments2 = outputs.Item2; // new additions var additions2 = outputs.Item3; // new omissions var omissions2 = outputs.Item4; // deltas var deltas = outputs.Item5; // get typos var typos = LongestCommonSubsequence.GetTypos(alignments2, original, entered2); // there are two outcomes: // 1) 's' -> "sy" and 't' -> "" // 2) 's' -> "s" and 't' -> "y" Tuple <OptChar, string>[] t1 = { new Tuple <OptChar, string>(OptChar.None, ""), new Tuple <OptChar, string>(OptChar.Some('T'), "T"), new Tuple <OptChar, string>(OptChar.Some('e'), "e"), new Tuple <OptChar, string>(OptChar.Some('s'), "sy"), new Tuple <OptChar, string>(OptChar.Some('t'), ""), new Tuple <OptChar, string>(OptChar.Some('i'), "i"), new Tuple <OptChar, string>(OptChar.Some('n'), "n"), new Tuple <OptChar, string>(OptChar.Some('g'), "g") }; Tuple <OptChar, string>[] t2 = { new Tuple <OptChar, string>(OptChar.None, ""), new Tuple <OptChar, string>(OptChar.Some('T'), "T"), new Tuple <OptChar, string>(OptChar.Some('e'), "e"), new Tuple <OptChar, string>(OptChar.Some('s'), "s"), new Tuple <OptChar, string>(OptChar.Some('t'), "y"), new Tuple <OptChar, string>(OptChar.Some('i'), "i"), new Tuple <OptChar, string>(OptChar.Some('n'), "n"), new Tuple <OptChar, string>(OptChar.Some('g'), "g") }; Assert.AreEqual(true, typos.SequenceEqual(t1) || typos.SequenceEqual(t2)); }
public void Baseline() { var sut = new LongestCommonSubsequence(); Assert.Equal(3, sut.GetLongestSunsequence("ABCDGH", "AEDFHR")); Assert.Equal(4, sut.GetLongestSunsequence("AGGTAB", "GXTXAYB")); }
public void TypoTest() { var s1 = "abcd"; var s2 = "zdcd"; var ss = LongestCommonSubsequence.LeftAlignedLCS(s1, s2); var typos = LongestCommonSubsequence.GetTypos(ss, s1, s2); OptChar[] keys = { OptChar.None, new OptChar('a'), new OptChar('b'), new OptChar('c'), new OptChar('d') }; char[] values = { 'z', 'd', 'c', 'd' }; var key_hs = new System.Collections.Generic.HashSet <OptChar>(keys); var value_hs = new System.Collections.Generic.HashSet <char>(values); var keys_seen = new System.Collections.Generic.HashSet <OptChar>(); var values_seen = new System.Collections.Generic.HashSet <char>();; foreach (var typo in typos) { var key = typo.Item1; var str = typo.Item2; keys_seen.Add(key); foreach (char c in str) { values_seen.Add(c); } } Assert.AreEqual(true, key_hs.SetEquals(keys_seen)); Assert.AreEqual(true, value_hs.SetEquals(values_seen)); }
public void LCSSingleFindsLCSInLCSMulti() { var s1 = "aacc"; var s2 = "aaaccac"; // Run LCS var m = s1.Length; var n = s2.Length; var C = LongestCommonSubsequence.LCSLength(s1, s2); // Set timeout var t = new TimeSpan(0, 0, 1); var sw = new System.Diagnostics.Stopwatch(); sw.Start(); // Run backtrack-all var multi = LongestCommonSubsequence.getCharPairs(C, s1, s2, m, n, sw, t); // Run backtrack-single var single = LongestCommonSubsequence.getCharPairs_single(C, s1, s2, m, n); // single should be in multi var found = false; foreach (var alignment in multi) { if (alignment.SequenceEqual(single)) { found = true; } } Assert.AreEqual(true, found); }
public void Test() { var a = "AGGTAB"; var b = "GXTXAYB"; var res = LongestCommonSubsequence.LCSSimple(a.ToCharArray(), b.ToCharArray(), a.Length, b.Length); Assert.AreEqual(4, res); res = LongestCommonSubsequence.LCS(a.ToCharArray(), b.ToCharArray(), a.Length, b.Length); Assert.AreEqual(4, res); /* * _____________________________ * |___|_A_|_G_|_G_|_T_|_A_|_B_| * | G | - | - | 4 | - | - | - | * | X | - | - | - | - | - | - | * | T | - | - | - | 3 | - | - | * | X | - | - | - | - | - | - | * | A | - | - | - | - | 2 | - | * | Y | - | - | - | - | - | - | * | B | - | - | - | - | - | 1 | * |____________________________ */ }
public void TestLongestCommonSubsequence() { LongestCommonSubsequence lcs = new LongestCommonSubsequence(); var output = lcs.PrintLongestCommonSubsequence("AGGTAB", "GXTXAYB"); Assert.AreEqual(output, "GTAB"); }
public void TestAddedAtStart() { string a = "abc"; string b = "ddabc"; // get LCS var al = LongestCommonSubsequence.LeftAlignedLCS(a, b); // find all character additions var ad = LongestCommonSubsequence.GetAddedCharIndices(b, al); // find all character omissions var om = LongestCommonSubsequence.GetMissingCharIndices(a, al); // find all transpositions var ou = LongestCommonSubsequence.FixTranspositions(al, ad, om, a, b); // new string string b2 = ou.Item1; // new alignments var al2 = ou.Item2; // new additions var ad2 = ou.Item3; // new omissions var om2 = ou.Item4; // deltas var d = ou.Item5; // get typos var t = LongestCommonSubsequence.GetTypos(al2, a, b2); Assert.AreEqual(0, om.Count()); Assert.AreEqual(4, t.Count()); Assert.AreEqual(2, ad.Count()); }
public Route FindRoute(string name) { var route = this.SingleOrDefault(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (route != null) { return(route); } var longestCommonSubsequence = new LongestCommonSubsequence(); var distance = double.MaxValue; Route currentRoute = null; foreach (var tmp in this) { var currentDistance = longestCommonSubsequence.Distance(tmp.Name, name); if (currentDistance < distance) { distance = currentDistance; currentRoute = tmp; } } return(currentRoute); }
public Tuple <int, int> ProcessTypos(string original, string entered) { // get LCS var alignments = LongestCommonSubsequence.LeftAlignedLCS(original, entered); // find all character additions var additions = LongestCommonSubsequence.GetAddedCharIndices(entered, alignments); // find all character omissions var omissions = LongestCommonSubsequence.GetMissingCharIndices(original, alignments); // find all transpositions var outputs = LongestCommonSubsequence.FixTranspositions(alignments, additions, omissions, original, entered); // new string string entered2 = outputs.Item1; // new alignments var alignments2 = outputs.Item2; // new additions var additions2 = outputs.Item3; // new omissions var omissions2 = outputs.Item4; // deltas var deltas = outputs.Item5; // get typos var typos = LongestCommonSubsequence.GetTypos(alignments2, original, entered2); int count_deltas = 0; int count_typos = 0; // train the model for all non-transpositions foreach (var alignment in alignments) { AddTranspositionError(0); } // train the model for all actual transpositions foreach (var delta in deltas) { AddTranspositionError(delta); count_deltas += 1; } // train the model for each "typo", including non-typos foreach (var typo in typos) { OptChar c = typo.Item1; string s = typo.Item2; AddTypoError(c, s); // non-empty string if (OptChar.get_IsSome(c)) { count_typos += c.Value.ToString().Equals(s) ? 0 : 1; } // empty string else { count_typos += s.Equals("") ? 0 : 1; } } return(new Tuple <int, int>(count_deltas, count_typos)); }
public void TestSubstring() { var s1 = "Hello"; var s2 = "Helloo"; var ss = LongestCommonSubsequence.LCS_Hash(s1, s2); Assert.AreEqual(true, ss.Contains(s1)); }
public void LongestCommonSubsequence() { var input = new[] { "XMJYAUZ;MZJAWXU" }; var expected = "MJAU"; var result = new LongestCommonSubsequence(input).Run().First(); Assert.AreEqual(expected, result); }
public void TestDistance() { var instance = new LongestCommonSubsequence(); // LCS = GA or GC => distance = 4 (remove 3 letters and add 1) Assert.Equal(expected: 4, actual: instance.Distance("AGCAT", "GAC")); Assert.Equal(expected: 1, actual: instance.Distance("AGCAT", "AGCT")); }
public void EnumFixTranspositionTests() { var orig1 = "acc"; var ent1 = "cca"; var al1 = LongestCommonSubsequence.LeftAlignedLCS(orig1, ent1); var ad1 = LongestCommonSubsequence.GetAddedCharIndices(ent1, al1); var om1 = LongestCommonSubsequence.GetMissingCharIndices(orig1, al1); var fix1 = LongestCommonSubsequence.FixTranspositions(al1, ad1, om1, orig1, ent1); Assert.AreEqual(orig1, fix1.Item1); Tuple <int, int>[] correct_alignments = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 2) }; Assert.AreEqual(true, correct_alignments.SequenceEqual <Tuple <int, int> >(fix1.Item2)); int [] correct_additions = {}; Assert.AreEqual(true, correct_additions.SequenceEqual <int>(fix1.Item3)); int[] correct_omissions = { }; Assert.AreEqual(true, correct_omissions.SequenceEqual <int>(fix1.Item4)); Assert.AreEqual(2, fix1.Item5.Head); var orig2 = "acc"; var ent2 = "cac"; // this line is to avoid nondeterministic choice of alignments Tuple <int, int>[] al2_a = { new Tuple <int, int>(0, 1), new Tuple <int, int>(2, 2) }; var al2 = LongestCommonSubsequence.ToFSList(al2_a); var ad2 = LongestCommonSubsequence.GetAddedCharIndices(ent2, al2); var om2 = LongestCommonSubsequence.GetMissingCharIndices(orig2, al2); var fix2 = LongestCommonSubsequence.FixTranspositions(al2, ad2, om2, orig2, ent2); Assert.AreEqual(orig2, fix2.Item1); Tuple <int, int>[] correct_alignments2 = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 2) }; Assert.AreEqual(true, correct_alignments2.SequenceEqual <Tuple <int, int> >(fix2.Item2)); int[] correct_additions2 = { }; Assert.AreEqual(true, correct_additions2.SequenceEqual <int>(fix2.Item3)); int[] correct_omissions2 = { }; Assert.AreEqual(true, correct_omissions2.SequenceEqual <int>(fix2.Item4)); Assert.AreEqual(-1, fix2.Item5.Head); var orig3 = "cac"; var ent3 = "acc"; // this line is to avoid nondeterministic choice of alignments Tuple <int, int>[] al3_a = { new Tuple <int, int>(0, 1), new Tuple <int, int>(2, 2) }; var al3 = LongestCommonSubsequence.ToFSList(al3_a); var ad3 = LongestCommonSubsequence.GetAddedCharIndices(ent3, al3); var om3 = LongestCommonSubsequence.GetMissingCharIndices(orig3, al3); var fix3 = LongestCommonSubsequence.FixTranspositions(al3, ad3, om3, orig3, ent3); Assert.AreEqual(orig3, fix3.Item1); Tuple <int, int>[] correct_alignments3 = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 2) }; Assert.AreEqual(true, correct_alignments3.SequenceEqual <Tuple <int, int> >(fix2.Item2)); int[] correct_additions3 = { }; Assert.AreEqual(true, correct_additions3.SequenceEqual <int>(fix2.Item3)); int[] correct_omissions3 = { }; Assert.AreEqual(true, correct_omissions3.SequenceEqual <int>(fix2.Item4)); Assert.AreEqual(-1, fix3.Item5.Head); }
public void TestPerformance3000Letters() { var first = "xxxxx" + new string('a', 3000) + "xxxxx"; var second = "bbb" + new string('a', 3000) + "bbb"; var expectedLCS = new string('a', 3000); var actualLCS = LongestCommonSubsequence. FindLongestCommonSubsequence(first, second); Assert.AreEqual(expectedLCS, actualLCS); }
public void TestSimilarStrings() { var first = "Petko Marinov"; var second = "Pletko Malinov"; var expectedLCS = "Petko Mainov"; var actualLCS = LongestCommonSubsequence. FindLongestCommonSubsequence(first, second); Assert.AreEqual(expectedLCS, actualLCS); }
public void TestSingleLetter() { var first = "a"; var second = "a"; var expectedLCS = "a"; var actualLCS = LongestCommonSubsequence. FindLongestCommonSubsequence(first, second); Assert.AreEqual(expectedLCS, actualLCS); }
public void TestSingleLetterOverlappingStrings() { var first = "hello"; var second = "beer"; var expectedLCS = "e"; var actualLCS = LongestCommonSubsequence. FindLongestCommonSubsequence(first, second); Assert.AreEqual(expectedLCS, actualLCS); }
public void TestNonOverlappingStrings() { var first = "hello"; var second = "rakiya"; var expectedLCS = ""; var actualLCS = LongestCommonSubsequence. FindLongestCommonSubsequence(first, second); Assert.AreEqual(expectedLCS, actualLCS); }
public void TestSimilarLongStrings() { var first = "dynamic progamming we brak the oiginal prolem to smaller sub-problms that hae the same strcture"; var second = "In dynmic prgamming we break th oriinal problem to smaler subprobems that have the sme struture"; var expectedLCS = "dynmic prgamming we brak th oiinal prolem to smaler subprobms that hae the sme strture"; var actualLCS = LongestCommonSubsequence. FindLongestCommonSubsequence(first, second); Assert.AreEqual(expectedLCS, actualLCS); }
public void Get_Exists_Find() { var lhs = "abcbdab"; var rhs = "bdcaba"; var solver = new LongestCommonSubsequence(lhs, rhs); var lcs = solver.Get(); using var scope = new AssertionScope(); lcs.Length.Should().Be(4); lcs.Should().Be("bdab"); }
private void AssertMatrix <T>(LongestCommonSubsequence <T> lcm, int[,] expected) { for (int i = 0; i < expected.GetLength(0); i++) { for (int j = 0; j < expected.GetLength(1); j++) { Assert.True( expected[i, j] == lcm[i, j], $"[{i}, {j}] expected to be {expected[i, j]} but was {lcm[i, j]}" ); } } }
public void IndicesExcluded() { var s1 = "abc"; var s2 = "bc"; // this returns exactly one left-aligned common subsequence, chosen randomly var ss = LongestCommonSubsequence.LeftAlignedLCS(s1, s2); // get the excluded indices var idxs = LongestCommonSubsequence.GetMissingCharIndices(s1, ss); Assert.AreEqual(1, idxs.Count()); Assert.AreEqual(idxs[0], 0); }
public void TestCharSequences() { var s1 = "abc"; var s2 = "abcc"; Tuple <int, int>[] shouldbe_1_a = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 2) }; Tuple <int, int>[] shouldbe_2_a = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 3) }; var shouldbe_1 = new System.Collections.Generic.List <Tuple <int, int> >(shouldbe_1_a); var shouldbe_2 = new System.Collections.Generic.List <Tuple <int, int> >(shouldbe_2_a); var sss = LongestCommonSubsequence.LCS_Hash_Char(s1, s2); Assert.AreEqual(2, sss.Count); }
public void TestLeftAlignedLCS() { var s1 = "abc"; var s2 = "abcc"; // this returns exactly one left-aligned common subsequence, chosen randomly var ss = LongestCommonSubsequence.LeftAlignedLCSList(s1, s2); // but in this case, both left-aligned subsequences will be the same Tuple <int, int>[] shouldbe = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 2) }; Assert.AreEqual(true, ss.SequenceEqual <Tuple <int, int> >(shouldbe)); var s3 = "aaab"; var s4 = "bzzzaaa"; var ss2 = LongestCommonSubsequence.LeftAlignedLCS(s3, s4); Tuple <int, int>[] shouldbe2 = { new Tuple <int, int>(0, 4), new Tuple <int, int>(1, 5), new Tuple <int, int>(2, 6) }; Assert.AreEqual(true, ss2.SequenceEqual <Tuple <int, int> >(shouldbe2)); }
public void TestCharSequence() { var s1 = "Hello"; var s2 = "Heellloo"; Tuple <int, int>[] shouldbe_a = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 2), new Tuple <int, int>(2, 4), new Tuple <int, int>(3, 5), new Tuple <int, int>(4, 7) }; Tuple <int, int>[] shouldalsobe_a = { new Tuple <int, int>(0, 0), new Tuple <int, int>(1, 2), new Tuple <int, int>(2, 4), new Tuple <int, int>(3, 5), new Tuple <int, int>(4, 6) }; var shouldbe = new System.Collections.Generic.List <Tuple <int, int> >(shouldbe_a); var shouldalsobe = new System.Collections.Generic.List <Tuple <int, int> >(shouldalsobe_a); var sss = LongestCommonSubsequence.LCS_Hash_Char(s1, s2); var found = 0; foreach (var ss in sss) { if (ss.SequenceEqual <Tuple <int, int> >(shouldbe) || ss.SequenceEqual <Tuple <int, int> >(shouldalsobe)) { found += 1; } } Assert.AreEqual <int>(2, found); Assert.AreEqual(12, sss.Count); }
private void FindTwoSimilarString(List <string> strings, out string s1, out string s2) { int min = -1; var LCS = new LongestCommonSubsequence(); s1 = ""; s2 = ""; for (int i = 0; i < strings.Count - 1; i++) { for (int j = i + 1; j < strings.Count; j++) { int distance = (int)LCS.Distance(strings[i], strings[j]); if (min == -1 || distance < min) { min = distance; s1 = strings[i]; s2 = strings[j]; } } } }
public void LcsStringDynamic(string s1, string s2, string expected) { string actual = LongestCommonSubsequence.LcsStringDynamic(s1, s2); Assert.AreEqual(expected, actual); }
public void LcsLengthDynamic_ValidStrings_ReturnsExpected(string s1, string s2, int expected) { int actual = LongestCommonSubsequence.LcsLengthDynamic(s1, s2); Assert.AreEqual(expected, actual); }
public void LongestCommonSubsequence() { var input = new[] {"XMJYAUZ;MZJAWXU"}; var expected = "MJAU"; var result = new LongestCommonSubsequence(input).Run().First(); Assert.AreEqual(expected, result); }