public void Find_SomeSimplePatterns_ReturnsCorrectLocations(string input1, string input2) { int index1 = input1.IndexOf('['); int length1 = input1.IndexOf(']') - index1 - 1; Assert.That(index1, Is.GreaterThanOrEqualTo(0)); Assert.That(length1, Is.GreaterThan(0)); int index2 = input2.IndexOf('['); int length2 = input2.IndexOf(']') - index2 - 1; Assert.That(index2, Is.GreaterThanOrEqualTo(0)); Assert.That(length2, Is.GreaterThan(0)); Assert.That(length1, Is.EqualTo(length2)); input1 = input1.Replace("[", "").Replace("]", ""); input2 = input2.Replace("[", "").Replace("]", ""); LongestCommonSubstringResult lcsr = new LongestCommonSubstring <char>(input1, input2).Find(); Assert.That(lcsr.PositionInCollection1, Is.EqualTo(index1)); Assert.That(lcsr.PositionInCollection2, Is.EqualTo(index2)); Assert.That(lcsr.Length, Is.EqualTo(length1)); }
[TestCase(0, 10, 5, 4)] // lower1 is greater than upper1 public void Find_RangeParametersOutOfRange_ThrowsArgumentOutOfRangeException(int lower1, int upper1, int lower2, int upper2) { const string collection = "0123456789"; var lcs = new LongestCommonSubstring<char>(collection, collection); Assert.Throws<ArgumentOutOfRangeException>(() => lcs.Find(lower1, upper1, lower2, upper2)); }
[TestCase(0, 10, 5, 4)] // lower1 is greater than upper1 public void Find_RangeParametersOutOfRange_ThrowsArgumentOutOfRangeException(int lower1, int upper1, int lower2, int upper2) { const string collection = "0123456789"; var lcs = new LongestCommonSubstring <char>(collection, collection); Assert.Throws <ArgumentOutOfRangeException>(() => lcs.Find(lower1, upper1, lower2, upper2)); }
public void TestTwoStringWithSharedSubtring() { string s1 = "abcdefg"; string s2 = "hiabcjkfg"; LongestCommonSubstring c = new LongestCommonSubstring(); string s = c.GetLongestCommonSubstring(s1, s2); Assert.AreEqual(s, "abc"); }
public void CanFindSubstringAtBeginning() { var input = "abcabcbb"; var expected = "abc"; var lcs = new LongestCommonSubstring(); Assert.Equal(expected.Length, lcs.LengthOfLongestSubstring(input)); }
public void CanFindSubstringInMiddle() { var input = "pwwkew"; var expected = "wke"; var lcs = new LongestCommonSubstring(); Assert.Equal(expected.Length, lcs.LengthOfLongestSubstring(input)); }
public void Find_EqualStrings_ReturnsWholeString() { const string collection = "This is a test collection"; LongestCommonSubstringResult lcsr = new LongestCommonSubstring <char>(collection, collection).Find(); Assert.That(lcsr.PositionInCollection1, Is.EqualTo(0)); Assert.That(lcsr.PositionInCollection2, Is.EqualTo(0)); Assert.That(lcsr.Length, Is.EqualTo(collection.Length)); }
public void Find_WhenNothingInCommon_ReturnsNull() { const string collection1 = "This is a test of Longest Common Substring"; const string collection2 = "0123456789"; LongestCommonSubstringResult lcsr = new LongestCommonSubstring <char>(collection1, collection2).Find(); Assert.That(lcsr, Is.Null); }
public void TestTwoStringEqual() { string s1 = "abcdefg"; string s2 = "abcdefg"; LongestCommonSubstring c = new LongestCommonSubstring(); string s = c.GetLongestCommonSubstring(s1, s2); Assert.AreEqual(s, "abcdefg"); }
public void ShallFind_aba_and_bab() { var results = LongestCommonSubstring.Find("abab", "baba"); Assert.AreEqual("aba", results[0]); Assert.AreEqual("bab", results[1]); results = LongestCommonSubstring.Find("baba", "abab"); Assert.AreEqual("bab", results[0]); Assert.AreEqual("aba", results[1]); }
public void ShallFind_abc_AtTheBeginning() { var results = LongestCommonSubstring.Find("abc21", "12abc43"); Assert.AreEqual("abc", results[0]); results = LongestCommonSubstring.Find("21abc21", "abc43"); Assert.AreEqual("abc", results[0]); results = LongestCommonSubstring.Find("abc21", "abc43"); Assert.AreEqual("abc", results[0]); }
public void Find_EqualStringsButNotSameInstance_ReturnsWholeString() { const string collection1 = "This is a test collection"; string collection2 = "This is a test collectio"; collection2 += "n"; Assert.That(collection1, Is.Not.SameAs(collection2)); LongestCommonSubstringResult lcsr = new LongestCommonSubstring <char>(collection1, collection2).Find(); Assert.That(lcsr.PositionInCollection1, Is.EqualTo(0)); Assert.That(lcsr.PositionInCollection2, Is.EqualTo(0)); Assert.That(lcsr.Length, Is.EqualTo(collection1.Length)); }
public bool IsLinked(string tfnA) { var previousResults = _context .Validations .Where(p => p.CreatedOn >= DateTime.Now.AddSeconds(-30)) .OrderByDescending(p => p.CreatedOn).Take(3).ToList(); if (previousResults != null && previousResults.Count() == 3) { string tfnB = previousResults[1].Tfn; string tfnC = previousResults[2].Tfn; if (LongestCommonSubstring.Get(tfnA, tfnB).Length >= 4 && LongestCommonSubstring.Get(tfnB, tfnC).Length >= 4) { return(true); } } return(false); }
public void Find_SomeSimplePatterns_ReturnsCorrectLocations(string input1, string input2) { int index1 = input1.IndexOf('['); int length1 = input1.IndexOf(']') - index1 - 1; Assert.That(index1, Is.GreaterThanOrEqualTo(0)); Assert.That(length1, Is.GreaterThan(0)); int index2 = input2.IndexOf('['); int length2 = input2.IndexOf(']') - index2 - 1; Assert.That(index2, Is.GreaterThanOrEqualTo(0)); Assert.That(length2, Is.GreaterThan(0)); Assert.That(length1, Is.EqualTo(length2)); input1 = input1.Replace("[", "").Replace("]", ""); input2 = input2.Replace("[", "").Replace("]", ""); LongestCommonSubstringResult lcsr = new LongestCommonSubstring<char>(input1, input2).Find(); Assert.That(lcsr.PositionInCollection1, Is.EqualTo(index1)); Assert.That(lcsr.PositionInCollection2, Is.EqualTo(index2)); Assert.That(lcsr.Length, Is.EqualTo(length1)); }
public void Find_EqualStringsButNotSameInstance_ReturnsWholeString() { const string collection1 = "This is a test collection"; string collection2 = "This is a test collectio"; collection2 += "n"; Assert.That(collection1, Is.Not.SameAs(collection2)); LongestCommonSubstringResult lcsr = new LongestCommonSubstring<char>(collection1, collection2).Find(); Assert.That(lcsr.PositionInCollection1, Is.EqualTo(0)); Assert.That(lcsr.PositionInCollection2, Is.EqualTo(0)); Assert.That(lcsr.Length, Is.EqualTo(collection1.Length)); }
public void ShallFind_abc() { var results = LongestCommonSubstring.Find("1abc2", "3abc4"); Assert.AreEqual("abc", results[0]); }
public void Find_EqualStrings_ReturnsWholeString() { const string collection = "This is a test collection"; LongestCommonSubstringResult lcsr = new LongestCommonSubstring<char>(collection, collection).Find(); Assert.That(lcsr.PositionInCollection1, Is.EqualTo(0)); Assert.That(lcsr.PositionInCollection2, Is.EqualTo(0)); Assert.That(lcsr.Length, Is.EqualTo(collection.Length)); }
public void Find_WhenNothingInCommon_ReturnsNull() { const string collection1 = "This is a test of Longest Common Substring"; const string collection2 = "0123456789"; LongestCommonSubstringResult lcsr = new LongestCommonSubstring<char>(collection1, collection2).Find(); Assert.That(lcsr, Is.Null); }
/// <summary> /// Calculates the distance between two syntax tokens, disregarding trivia. /// </summary> /// <remarks> /// Distance is a number within [0, 1], the smaller the more similar the tokens are. /// </remarks> public static double ComputeDistance(SyntaxToken oldToken, SyntaxToken newToken) => LongestCommonSubstring.ComputeDistance(oldToken.Text, newToken.Text);
private static void LongestCommonSubstringDriverCode() { LongestCommonSubstring longestCommonSubstring = new LongestCommonSubstring(); longestCommonSubstring.LongestCommonSubstringMethod("yuytraaaa", "jkmnsyfsxtry"); }
public void EmptyStringsReturnZero() { var lcs = new LongestCommonSubstring(); Assert.Equal(0, lcs.LengthOfLongestSubstring("")); }
/// <summary> /// interview questions for string problems /// </summary> /// <param name="args"></param> static void Main(string[] args) { String[] words = { "abcde", "hello", "apple", "kite", "padle" }; foreach (string w in words) { Console.WriteLine(w + " : " + RandomStringProblem.isUniqueChars(w)); } String s = "helloiloveyou"; Console.WriteLine(s + " -> " + RandomStringProblem.RemoveDuplicateCharacters(s)); Console.WriteLine(s + " -> " + RandomStringProblem.RemoveDuplicateCharacters(s.ToCharArray())); Console.WriteLine("apple, papel : " + RandomStringProblem.anagram("apple", "papel")); Console.WriteLine("carrot, tarroc : " + RandomStringProblem.anagram("carrot", "tarroc")); Console.WriteLine("hello, llloh : " + RandomStringProblem.anagram("hello", "llloh")); s = "A quick brown fox jumped over the log"; var sarr = s.ToArray(); var saar2 = s.ToArray(); RandomStringProblem.ReplaceFun(ref sarr, sarr.Length); RandomStringProblem.ReplaceFun(ref saar2, sarr.Length); Console.WriteLine(string.Concat(sarr)); Console.WriteLine(string.Concat(saar2)); Console.WriteLine("aabcccaa => " + RandomStringProblem.compress("aabcccaa")); Console.WriteLine("aaaaaaab => " + RandomStringProblem.compress("aaaaaaab")); Console.WriteLine("ababababab => " + RandomStringProblem.compress("ababababab")); Console.WriteLine("avfffff => " + RandomStringProblem.compress("avfffff")); FindFirstNonRepeatingChar.Test(); /// Linked List Problems LinkedListNode head = AssortedMethods.randomLinkedList(10, 0, 3); Console.WriteLine(head.printForward()); LinkedListProblems.deleteDups(head); Console.WriteLine(head.printForward()); // wordbreak problems TestWordBreak.Run(); //MedianOfMedians.TestMedianOfMedians(); LongestCommonSubsequence.Test(); SumOfMaxRunningSequence.Test(); TestRankNode.Test(); NextInorderSuccessor.Test(); CircularArrayTest.Test(); LongestPalindrome.Test(); IsomorphicStrings.Test(); FindMinAbsFrom2Arrays.Test(); ValidateIsBST.Test(); CoinPuzzle.Test(); BinarySearch.Test(); RansomNote.Test(); atoi.Test(); TrieTest.Test(); IsTreeBalanced.Test(); //TestProducerConsumer.Test(); ShuffleDeck.Test(); //QuickRankSearch.Test(); ParenthesisCombo.Test(); Permutations.Test(); Combinations.Test(); CompressString.Test(); BuildTreeFromMatrixInput.Test(); FindAllRepeatingSubstrings.Test(); PrintTreePaths.Test(); FindLowestCommonAncestor.Test(); PrintLevelsOfABinaryTree.Test(); FindPathBetween2NodesInTree.Test(); FindPathExistsInGraph.Test(); FindPathBetween2GraphNodes.Test(); // RealTimeCounterTest.Test(); DistanceBetweenWordsInASentence.Test(); SearchingForNextCharInSearch.Test(); HashMapTester.Test(); FindElementInRotatedArray.Test(); SubTrees.Test(); LongestCommonSubstring.Test(); LongestIncreasingSequenceQuestion.Test(); ConvertTreeIntoDoublyLinkedList.Test(); Count2sBetween0andN.Test(); TestSuffixTrees.Test(); TransformWordIntoOtherWord.Test(); IsNumberAPalindrome.Test(); ConvertNumberToPhrase.Test(); CountBinary1sInStream.Test(); ReverseBitsInUnsignedInt.Test(); ReverseLinkedList.Test(); DeleteDups.Test(); _3SUM.Test(); ArraySequencesThatAddUpToSum.Tests(); FindBSTNodesThatSumUpToValue.Test(); LengthOfLongestSubstringProblem.Test(); //BuildTreeFromInorderAndPreorderTraversal.Test(); ConvertTreeToDLL.Test(); FindMissingNumberInString.Test(); SortStackWithAnotherStack.Test(); QueueFrom2Stacks.Test(); Sort2Queues.Test(); ConvertRomanNumeralToInt.Test(); MergeAllLists.Test(); DivideWithoutSlash.Test(); //RegularExpression.Test(); IsNumberValid.Test(); Console.ReadKey(); }
public void ShallFind_abc_02() { var results = LongestCommonSubstring.Find("12abc21", "34abc43"); Assert.AreEqual("abc", results[0]); }