diff --git a/Ch 01. Arrays and Strings/Ch 01. Arrays and Strings.csproj b/Ch 01. Arrays and Strings/Ch 01. Arrays and Strings.csproj index 8ab28a9..ec74bf5 100644 --- a/Ch 01. Arrays and Strings/Ch 01. Arrays and Strings.csproj +++ b/Ch 01. Arrays and Strings/Ch 01. Arrays and Strings.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 Chapter01 diff --git a/Ch 01. Arrays and Strings/Q1_05_One_Away_A.cs b/Ch 01. Arrays and Strings/Q1 05 One Edit Away.cs similarity index 69% rename from Ch 01. Arrays and Strings/Q1_05_One_Away_A.cs rename to Ch 01. Arrays and Strings/Q1 05 One Edit Away.cs index 5a25bf7..5e06c27 100644 --- a/Ch 01. Arrays and Strings/Q1_05_One_Away_A.cs +++ b/Ch 01. Arrays and Strings/Q1 05 One Edit Away.cs @@ -1,113 +1,117 @@ -using ctci.Contracts; -using System; - -namespace Chapter01 -{ - public class Q1_05_One_Away_A : Question - { - public static bool OneEditReplace(String s1, String s2) - { - bool foundDifference = false; - for (int i = 0; i < s1.Length; i++) - { - if (s1[i] != s2[i]) - { - if (foundDifference) - { - return false; - } - - foundDifference = true; - } - } - return true; - } - - /* Check if you can insert a character into s1 to make s2. */ - - public static bool OneEditInsert(String s1, String s2) - { - int index1 = 0; - int index2 = 0; - while (index2 < s2.Length && index1 < s1.Length) - { - if (s1[index1] != s2[index2]) - { - if (index1 != index2) - { - return false; - } - index2++; - } - else { - index1++; - index2++; - } - } - return true; - } - - public static bool OneEditAway(String first, String second) - { - if (first.Length == second.Length) - { - return OneEditReplace(first, second); - } - else if (first.Length + 1 == second.Length) - { - return OneEditInsert(first, second); - } - else if (first.Length - 1 == second.Length) - { - return OneEditInsert(second, first); - } - return false; - } - - public static bool OneEditAway2(String first, String second) - { - /* Length checks. */ - if (Math.Abs(first.Length - second.Length) > 1) - { - return false; - } - - /* Get shorter and longer string.*/ - String s1 = first.Length < second.Length ? first : second; - String s2 = first.Length < second.Length ? second : first; - - int index1 = 0; - int index2 = 0; - bool foundDifference = false; - while (index2 < s2.Length && index1 < s1.Length) - { - if (s1[index1] != s2[index2]) - { - /* Ensure that this is the first difference found.*/ - if (foundDifference) return false; - foundDifference = true; - if (s1.Length == s2.Length) - { // On replace, move shorter pointer - index1++; - } - } - else { - index1++; // If matching, move shorter pointer - } - index2++; // Always move pointer for longer string - } - return true; - } - - public override void Run() - { - String a = "pse"; - String b = "pale"; - bool isOneEdit = OneEditAway(a, b); - Console.WriteLine("{0}, {1}: {2}", a, b, isOneEdit); - - bool isOneEdit2 = OneEditAway2(a, b); - Console.WriteLine("{0}, {1}: {2}", a, b, isOneEdit2); - } - } +using ctci.Contracts; +using System; + +namespace Chapter01 +{ + public class Q1_05_One_Edit_Away : Question + { + public override void Run() + { + String a = "pse"; + String b = "pale"; + var c = "ple"; + var d = "pele"; + Console.WriteLine($"{a}, {b}: {OneEditAway(a, b)} / {OneEditAwayBook(a, b)}"); + Console.WriteLine($"{b}, {c}: {OneEditAway(b, c)} / {OneEditAwayBook(b, c)}"); + Console.WriteLine($"{b}, {d}: {OneEditAway(b, d)} / {OneEditAwayBook(b, d)}"); + + + } + public static bool OneEditReplace(String s1, String s2) + { + bool foundDifference = false; + for (int i = 0; i < s1.Length; i++) + { + if (s1[i] != s2[i]) + { + if (foundDifference) + { + return false; + } + + foundDifference = true; + } + } + return true; + } + + /* Check if you can insert a character into s1 to make s2. */ + + public static bool OneEditInsert(String s1, String s2) + { + int index1 = 0; + int index2 = 0; + while (index2 < s2.Length && index1 < s1.Length) + { + if (s1[index1] != s2[index2]) + { + if (index1 != index2) + { + return false; + } + index2++; + } + else + { + index1++; + index2++; + } + } + return true; + } + + public static bool OneEditAway(String first, String second) + { + if (first.Length == second.Length) + { + return OneEditReplace(first, second); + } + else if (first.Length + 1 == second.Length) + { + return OneEditInsert(first, second); + } + else if (first.Length - 1 == second.Length) + { + return OneEditInsert(second, first); + } + return false; + } + + public static bool OneEditAwayBook(String first, String second) + { + /* Length checks. */ + if (Math.Abs(first.Length - second.Length) > 1) + { + return false; + } + + /* Get shorter and longer string.*/ + String shorter = first.Length < second.Length ? first : second; + String longer = first.Length < second.Length ? second : first; + + int indexS = 0; + int indexL = 0; + bool foundDifference = false; + while (indexL < longer.Length && indexS < shorter.Length) + { + if (shorter[indexS] != longer[indexL]) + { + /* Ensure that this is the first difference found.*/ + if (foundDifference) return false; + foundDifference = true; + if (shorter.Length == longer.Length) + { // On replace, move shorter pointer + indexS++; + } + } + else + { + indexS++; // If matching, move shorter pointer + } + indexL++; // Always move pointer for longer string + } + return true; + } + + } } \ No newline at end of file diff --git a/Ch 01. Arrays and Strings/Q1_01_Is_Unique.cs b/Ch 01. Arrays and Strings/Q1_01_Is_Unique.cs index 42ff716..567675a 100644 --- a/Ch 01. Arrays and Strings/Q1_01_Is_Unique.cs +++ b/Ch 01. Arrays and Strings/Q1_01_Is_Unique.cs @@ -1,50 +1,42 @@ using ctci.Contracts; using System; using System.Collections.Generic; +using System.Linq; namespace Chapter01 { public class Q1_01_Is_Unique : Question { - private bool IsUniqueChars(string str) + public bool IsUniqueChars(string str) { if (str.Length > 256) - { - return false; - } + throw new ArgumentException("String has to be less than 256 characters"); var checker = 0; for (var i = 0; i < str.Length; i++) { var val = str[i] - 'a'; - - if ((checker & (1 << val)) > 0) - { - return false; - } + if ((checker & (1 << val)) > 0) return false; checker |= (1 << val); } return true; } - private bool IsUniqueChars2(String str) + public bool IsUniqueChars2(String str) { var hashset = new HashSet(); - foreach(var c in str) + foreach (var c in str) { if (hashset.Contains(c)) return false; hashset.Add(c); } - return true; } public override void Run() { - string[] words = { "abcde", "hello", "apple", "kite", "padle" }; - - foreach (var word in words) + foreach (var word in new string[] { "abcde", "hello", "apple", "kite", "padle" }) { Console.WriteLine(word + ": " + IsUniqueChars(word) + " " + IsUniqueChars2(word)); } diff --git a/Ch 01. Arrays and Strings/Q1_02_Check_Permutation.cs b/Ch 01. Arrays and Strings/Q1_02_Check_Permutation.cs index c2f18b4..b066780 100644 --- a/Ch 01. Arrays and Strings/Q1_02_Check_Permutation.cs +++ b/Ch 01. Arrays and Strings/Q1_02_Check_Permutation.cs @@ -1,6 +1,7 @@ using ctci.Contracts; using System; using System.Collections.Generic; +using System.Linq; namespace Chapter01 { @@ -9,27 +10,21 @@ public class Q1_02_Check_Permutation : Question private bool IsPermutation(string original, string valueToTest) { if (original.Length != valueToTest.Length) - { return false; - } var originalAsArray = original.ToCharArray(); Array.Sort(originalAsArray); - original = new string(originalAsArray); var valueToTestAsArray = valueToTest.ToCharArray(); Array.Sort(valueToTestAsArray); - valueToTest = new string(valueToTestAsArray); - return original.Equals(valueToTest); + return originalAsArray.SequenceEqual(valueToTestAsArray); } private bool IsPermutation2(string original, string valueToTest) { if (original.Length != valueToTest.Length) - { return false; - } var letterCount = new Dictionary(); diff --git a/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs b/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs index d8568c6..2f5268b 100644 --- a/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs +++ b/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs @@ -6,42 +6,44 @@ namespace Chapter01 { public class Q1_07_Rotate_Matrix : Question { - private void Rotate(int[][] matrix, int n) + private void Rotate(int[,] matrix, int n) { for (var layer = 0; layer < n / 2; ++layer) { var first = layer; var last = n - 1 - layer; - for (var i = first; i < last; ++i) { var offset = i - first; - var top = matrix[first][i]; // save top + var top = matrix[first, i]; // save top, left - // left -> top - matrix[first][i] = matrix[last - offset][first]; + // top, left <- bottom, left + matrix[first, i] = matrix[last - offset, first]; - // bottom -> left - matrix[last - offset][first] = matrix[last][last - offset]; + // bottom, left <- bottom, right + matrix[last - offset, first] = matrix[last, last - offset]; - // right -> bottom - matrix[last][last - offset] = matrix[i][last]; + // bottom, right <- top, right + matrix[last, last - offset] = matrix[i, last]; - // top -> right - matrix[i][last] = top; // right <- saved top + // top, right <- top + matrix[i, last] = top; } } } public override void Run() { - const int size = 3; - - var matrix = AssortedMethods.RandomMatrix(size, size, 0, 9); + var matrix = new[,] + { + {1,2,3 }, + {4,5,6 }, + {7,8,9 } + }; AssortedMethods.PrintMatrix(matrix); - Rotate(matrix, size); + Rotate(matrix, matrix.GetLength(0)); Console.WriteLine(); AssortedMethods.PrintMatrix(matrix); } diff --git a/Ch 01. Arrays and Strings/Q1_08_Zero_Matrix.cs b/Ch 01. Arrays and Strings/Q1_08_Zero_Matrix.cs index 7869d33..d4a560b 100644 --- a/Ch 01. Arrays and Strings/Q1_08_Zero_Matrix.cs +++ b/Ch 01. Arrays and Strings/Q1_08_Zero_Matrix.cs @@ -6,51 +6,52 @@ namespace Chapter01 { public class Q1_08_Zero_Matrix : Question { - private void NullifyRow(int[][] matrix, int row) + private void NullifyRow(int[,] matrix, int row) { - for (var j = 0; j < matrix[0].Length; j++) + var width = matrix.GetLength(1); + for (var column = 0; column < width; column++) { - matrix[row][j] = 0; + matrix[row, column] = 0; } } - private void NullifyColumn(int[][] matrix, int col) + private void NullifyColumn(int[,] matrix, int col) { - for (var i = 0; i < matrix.Length; i++) + var height = matrix.GetLength(0); + for (var row = 0; row < height; row++) { - matrix[i][col] = 0; + matrix[row, col] = 0; } } - private int[][] CloneMatrix(int[][] matrix) + private int[,] CloneMatrix(int[,] matrix) { - var clone = new int[matrix.Length][]; + var height = matrix.GetLength(0); + var width = matrix.GetLength(1); - for (var i = 0; i < matrix.Length; i++) + var clone = new int[height, width]; + for (var row = 0; row < height; row++) { - clone[i] = new int[matrix[0].Length]; - - for (var j = 0; j < matrix[0].Length; j++) + for (var column = 0; column < width; column++) { - clone[i][j] = matrix[i][j]; + clone[row, column] = matrix[row, column]; } } - return clone; } - private bool MatricesAreEqual(int[][] matrix1, int[][] matrix2) + private bool MatricesAreEqual(int[,] matrix1, int[,] matrix2) { - if (matrix1.Length != matrix2.Length || matrix1[0].Length != matrix2[0].Length) + if (matrix1.GetLength(0) != matrix2.GetLength(0) || matrix1.GetLength(1) != matrix2.GetLength(1)) { return false; } - for (var k = 0; k < matrix1.Length; k++) + for (var row = 0; row < matrix1.GetLength(0); row++) { - for (var j = 0; j < matrix1[0].Length; j++) + for (var column = 0; column < matrix1.GetLength(1); column++) { - if (matrix1[k][j] != matrix2[k][j]) + if (matrix1[row, column] != matrix2[row, column]) { return false; } @@ -60,131 +61,55 @@ private bool MatricesAreEqual(int[][] matrix1, int[][] matrix2) return true; } - private void SetZeros(int[][] matrix) + private void SetZeros(int[,] matrix) { - var row = new bool[matrix.Length]; - var column = new bool[matrix[0].Length]; + var height = matrix.GetLength(0); + var width = matrix.GetLength(1); + + var rowArray = new bool[height]; + var columnArray = new bool[width]; // Store the row and column index with value 0 - for (var i = 0; i < matrix.Length; i++) + for (var row = 0; row < height; row++) { - for (var j = 0; j < matrix[0].Length; j++) + for (var column = 0; column < width; column++) { - if (matrix[i][j] == 0) + if (matrix[row, column] == 0) { - row[i] = true; - column[j] = true; + rowArray[row] = true; + columnArray[column] = true; } } } // Nullify rows - for (var i = 0; i < row.Length; i++) + for (var i = 0; i < rowArray.Length; i++) { - if (row[i]) + if (rowArray[i]) { NullifyRow(matrix, i); } } // Nullify columns - for (var j = 0; j < column.Length; j++) + for (var j = 0; j < columnArray.Length; j++) { - if (column[j]) + if (columnArray[j]) { NullifyColumn(matrix, j); } } } - private void SetZeros2(int[][] matrix) - { - var rowHasZero = false; - var colHasZero = false; - - // Check if first row has a zero - for (var j = 0; j < matrix[0].Length; j++) - { - if (matrix[0][j] == 0) - { - rowHasZero = true; - break; - } - } - - // Check if first column has a zero - for (var i = 0; i < matrix.Length; i++) - { - if (matrix[i][0] == 0) - { - colHasZero = true; - break; - } - } - - // Check for zeros in the rest of the array - for (var i = 1; i < matrix.Length; i++) - { - for (var j = 1; j < matrix[0].Length; j++) - { - if (matrix[i][j] == 0) - { - matrix[i][0] = 0; - matrix[0][j] = 0; - } - } - } - - // Nullify rows based on values in first column - for (var i = 1; i < matrix.Length; i++) - { - if (matrix[i][0] == 0) - { - NullifyRow(matrix, i); - } - } - - // Nullify columns based on values in first row - for (var j = 1; j < matrix[0].Length; j++) - { - if (matrix[0][j] == 0) - { - NullifyColumn(matrix, j); - } - } - - // Nullify first row - if (rowHasZero) - { - NullifyRow(matrix, 0); - } - - // Nullify first column - if (colHasZero) - { - NullifyColumn(matrix, 0); - } - } - public override void Run() { const int numberOfRows = 10; const int numberOfColumns = 15; var matrix1 = AssortedMethods.RandomMatrix(numberOfRows, numberOfColumns, 0, 100); - var matrix2 = CloneMatrix(matrix1); - AssortedMethods.PrintMatrix(matrix1); - SetZeros(matrix1); - SetZeros2(matrix2); - Console.WriteLine(); - AssortedMethods.PrintMatrix(matrix1); - Console.WriteLine(); - AssortedMethods.PrintMatrix(matrix2); - - Console.WriteLine(MatricesAreEqual(matrix1, matrix2) ? "Equal" : "Not Equal"); } } } \ No newline at end of file diff --git a/Ch 02. Linked Lists/Ch 02. Linked Lists.csproj b/Ch 02. Linked Lists/Ch 02. Linked Lists.csproj index 6cb8dba..9d6dd83 100644 --- a/Ch 02. Linked Lists/Ch 02. Linked Lists.csproj +++ b/Ch 02. Linked Lists/Ch 02. Linked Lists.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 Chapter02 diff --git a/Ch 02. Linked Lists/Q2_04_Partition.cs b/Ch 02. Linked Lists/Q2_04_Partition.cs index 0917178..aff58eb 100644 --- a/Ch 02. Linked Lists/Q2_04_Partition.cs +++ b/Ch 02. Linked Lists/Q2_04_Partition.cs @@ -178,7 +178,7 @@ private LinkedListNode Partition4(LinkedListNode listHead, int pivot) public override void Run() { /* Create linked list */ - int[] vals = { 1, 3, 7, 5, 2, 9, 4 }; + int[] vals = { 3, 5, 8, 5, 10, 2, 1 }; var head = new LinkedListNode(vals[0], null, null); var current = head; diff --git a/Ch 04. Trees/Ch 04. Trees.csproj b/Ch 04. Trees/Ch 04. Trees.csproj index b01a999..f9301ad 100644 --- a/Ch 04. Trees/Ch 04. Trees.csproj +++ b/Ch 04. Trees/Ch 04. Trees.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 diff --git a/Ch 04. Trees/Q4 07 Build Order.cs b/Ch 04. Trees/Q4 07 Build Order.cs new file mode 100644 index 0000000..34f4e82 --- /dev/null +++ b/Ch 04. Trees/Q4 07 Build Order.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Text; +using ctci.Contracts; +using ctci.Library; + +namespace Chapter04 +{ + public class Q4_07_Build_Order : Question + { + // I wrote the solution based on Topological sort in the Algorithms Book, + // and its code is here. https://github.com/kevin-wayne/algs4. + // In topological sort, you are using a directed graph. + // If a -> b, then it means, b depends on a, + // in other words, 'a' needs to be built before 'b.' + // After a topological sort, you expect all the nodes to be arranged such that it is flowing in one direction. + // Since a cycle is not allowed, the code checks if a cycle exists and returns. + // In real life, jobs cannot have circular dependencies. + public override void Run() + { + var diGraph = new DirectedGraph('f'); + diGraph.AddEdge('a', 'd'); // d depends on a + diGraph.AddEdge('f', 'b'); // b depends on f + diGraph.AddEdge('b', 'd'); + diGraph.AddEdge('f', 'a'); + diGraph.AddEdge('d', 'c'); + + var topological = new Topological(diGraph); + var order = topological.Order(); + AssortedMethods.PrintList(order); + } + + public class DirectedGraph + { + Dictionary> AdjacentVertexes; + List Vertexes; + + public DirectedGraph(char v) + { + Vertexes = new List(); + AdjacentVertexes = new Dictionary>(); + for (var a = 'a'; a <= v; a++) + { + AdjacentVertexes[a] = new List(); + Vertexes.Add(a); + } + } + + public void AddEdge(char v, char w) + { + AdjacentVertexes[v].Add(w); + } + public IEnumerable GetVertexes() + { + return Vertexes; + } + + public IEnumerable GetAdjacentVertexes(char v) + { + return AdjacentVertexes[v]; + } + } + + public class DirectedCycle + { + HashSet marked; + Dictionary edgeTo; + + Stack cycle; + Dictionary onStack; + + public DirectedCycle(DirectedGraph G) + { + onStack = new Dictionary(); + edgeTo = new Dictionary(); + marked = new HashSet(); + foreach (var vertex in G.GetVertexes()) + if (!marked.Contains(vertex)) DepthFirstCycleDetection(G, vertex); + } + + private void DepthFirstCycleDetection(DirectedGraph graph, char vertex) + { + onStack[vertex] = true; + marked.Add(vertex); + foreach (var adjVertexes in graph.GetAdjacentVertexes(vertex)) + { + if (this.HasCycle()) return; + else if (!marked.Contains(adjVertexes)) + { + edgeTo[adjVertexes] = vertex; + DepthFirstCycleDetection(graph, adjVertexes); + } + else if (onStack[adjVertexes]) + { + cycle = new Stack(); + for (char x = vertex; x != adjVertexes; x = edgeTo[x]) + cycle.Push(x); + cycle.Push(adjVertexes); + cycle.Push(vertex); + } + } + onStack[vertex] = false; + } + + public bool HasCycle() + { + return cycle != null; + } + } + + public class DepthFirstOrder + { + HashSet marked; + Stack reversePost; + + public DepthFirstOrder(DirectedGraph graph) + { + reversePost = new Stack(); + marked = new HashSet(); + + foreach (var vertex in graph.GetVertexes()) + if (!marked.Contains(vertex)) + DepthFirstSearch(graph, vertex); + } + + private void DepthFirstSearch(DirectedGraph graph, char vertex) + { + marked.Add(vertex); + foreach (var adjVertex in graph.GetAdjacentVertexes(vertex)) + if (!marked.Contains(adjVertex)) + DepthFirstSearch(graph, adjVertex); + reversePost.Push(vertex); + } + + public IEnumerable ReversePost() + { + return reversePost; + } + } + + public class Topological + { + IEnumerable order; + + public Topological(DirectedGraph graph) + { + var cycleFinder = new DirectedCycle(graph); + if (!cycleFinder.HasCycle()) + { + var depthFirstOrder = new DepthFirstOrder(graph); + order = depthFirstOrder.ReversePost(); + } + } + + public IEnumerable Order() { return order; } + } + } +} diff --git a/Ch 04. Trees/Q4 09 BST Sequence.cs b/Ch 04. Trees/Q4 09 BST Sequence.cs index defc258..c4d0071 100644 --- a/Ch 04. Trees/Q4 09 BST Sequence.cs +++ b/Ch 04. Trees/Q4 09 BST Sequence.cs @@ -20,7 +20,7 @@ public override void Run() BTreePrinter.Print(root); var results = AllSequences(root); foreach (var list in results) - AssortedMethods.PrintIntList(list); + AssortedMethods.PrintList(list); } public List> AllSequences(TreeNode node) diff --git a/Ch 04. Trees/Q4_04_CheckBalanced.cs b/Ch 04. Trees/Q4_04_CheckBalanced.cs index 27857fd..0662bad 100644 --- a/Ch 04. Trees/Q4_04_CheckBalanced.cs +++ b/Ch 04. Trees/Q4_04_CheckBalanced.cs @@ -4,7 +4,7 @@ namespace Chapter04 { - public class Q4_04_CheckBalanced: Question + public class Q4_04_CheckBalanced : Question { public static bool IsBalanced(TreeNode root) { @@ -52,27 +52,16 @@ public override void Run() private static void PrintTree(TreeNode root) { BTreePrinter.Print(root); - var watch = System.Diagnostics.Stopwatch.StartNew(); - var isBalanced1 = IsBalanced(root); - watch.Stop(); - var elapsedMs = watch.ElapsedMilliseconds; - if (isBalanced1) + if (IsBalanced(root)) Console.WriteLine("Tree is balanced"); else - Console.WriteLine("Tree is not balalnced"); - Console.WriteLine($"Elapsed milliconds {elapsedMs}"); - - watch = System.Diagnostics.Stopwatch.StartNew(); - var isBalanced2 = IsBalancedBook(root); - watch.Stop(); - elapsedMs = watch.ElapsedMilliseconds; + Console.WriteLine("Tree is not balanced"); if (IsBalancedBook(root)) Console.WriteLine("Tree is balanced"); else - Console.WriteLine("Tree is not balalnced"); - Console.WriteLine($"Elapsed milliconds {elapsedMs}"); + Console.WriteLine("Tree is not balanced"); } } } \ No newline at end of file diff --git a/Ch 04. Trees/ReplaceNodeInImmutableTree.cs b/Ch 04. Trees/ReplaceNodeInImmutableTree.cs index b991a80..89e0e7a 100644 --- a/Ch 04. Trees/ReplaceNodeInImmutableTree.cs +++ b/Ch 04. Trees/ReplaceNodeInImmutableTree.cs @@ -1,6 +1,5 @@ using ctci.Contracts; using ctci.Library; -using System; namespace Chapter04 { @@ -9,8 +8,10 @@ static partial class TreeNodeExtension static public TreeNode Replace(this TreeNode node, int value) { if (node == null) return null; - var newNode = new TreeNode(node); - newNode.Data = value; + var newNode = new TreeNode(node) + { + Data = value + }; var newRoot = newNode; while (node.Parent != null) { diff --git a/Ch 05. Bit Manipulation/Ch 05. Bit Manipulation.csproj b/Ch 05. Bit Manipulation/Ch 05. Bit Manipulation.csproj index 98877a9..6f2721f 100644 --- a/Ch 05. Bit Manipulation/Ch 05. Bit Manipulation.csproj +++ b/Ch 05. Bit Manipulation/Ch 05. Bit Manipulation.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 Chapter05 diff --git a/Ch 10. Sorting and Searching/Ch 10. Sorting and Searching.csproj b/Ch 10. Sorting and Searching/Ch 10. Sorting and Searching.csproj index d5758af..7ac2872 100644 --- a/Ch 10. Sorting and Searching/Ch 10. Sorting and Searching.csproj +++ b/Ch 10. Sorting and Searching/Ch 10. Sorting and Searching.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 diff --git a/Ch 10. Sorting and Searching/Q10_09_Sorted_Matrix_Search.cs b/Ch 10. Sorting and Searching/Q10_09_Sorted_Matrix_Search.cs index 91973ec..506bd29 100644 --- a/Ch 10. Sorting and Searching/Q10_09_Sorted_Matrix_Search.cs +++ b/Ch 10. Sorting and Searching/Q10_09_Sorted_Matrix_Search.cs @@ -17,12 +17,12 @@ public Coordinate(int r, int c) column = c; } - public bool Inbounds(int[][] matrix) + public bool Inbounds(int[,] matrix) { return row >= 0 && column >= 0 && - row < matrix.Length && - column < matrix[0].Length; + row < matrix.GetLength(0) && + column < matrix.GetLength(1); } public bool IsBefore(Coordinate p) @@ -48,21 +48,22 @@ public void SetToAverage(Coordinate min, Coordinate max) } } - public static bool FindElement(int[][] matrix, int elem) + public static bool FindElement(int[,] matrix, int elem) { int row = 0; - int col = matrix[0].Length - 1; - while (row < matrix.Length && col >= 0) + int col = matrix.GetLength(1) - 1; + while (row < matrix.GetLength(0) && col >= 0) { - if (matrix[row][col] == elem) + if (matrix[row, col] == elem) { return true; } - else if (matrix[row][col] > elem) + else if (matrix[row, col] > elem) { col--; } - else { + else + { row++; } } @@ -73,13 +74,12 @@ public void Run1() { int M = 10; int N = 5; - var matrix = new int[M][]; + var matrix = new int[M, N]; for (int i = 0; i < M; i++) { - matrix[i] = new int[N]; for (int j = 0; j < N; j++) { - matrix[i][j] = 10 * i + j; + matrix[i, j] = 10 * i + j; } } @@ -97,21 +97,21 @@ public void Run1() public void Run2() { - int[][] matrix = new[] { - new [] {15, 30, 50, 70, 73}, - new [] {35, 40, 100, 102, 120}, - new [] {36, 42, 105, 110, 125}, - new [] {46, 51, 106, 111, 130}, - new [] {48, 55, 109, 140, 150} + int[,] matrix = new[,] { + {15, 30, 50, 70, 73}, + {35, 40, 100, 102, 120}, + {36, 42, 105, 110, 125}, + {46, 51, 106, 111, 130}, + {48, 55, 109, 140, 150} }; AssortedMethods.PrintMatrix(matrix); - int m = matrix.Length; - int n = matrix[0].Length; + int rows = matrix.GetLength(0); + int columns = matrix.GetLength(1); int count = 0; - int littleOverTheMax = matrix[m - 1][n - 1] + 10; + int littleOverTheMax = matrix[rows - 1, columns - 1] + 10; for (int i = 0; i < littleOverTheMax; i++) { Coordinate c = FindElement2(matrix, i); @@ -132,7 +132,7 @@ public override void Run() #region Solution B - public static Coordinate PartitionAndSearch(int[][] matrix, Coordinate origin, Coordinate dest, Coordinate pivot, int x) + public static Coordinate PartitionAndSearch(int[,] matrix, Coordinate origin, Coordinate dest, Coordinate pivot, int x) { Coordinate lowerLeftOrigin = new Coordinate(pivot.row, origin.column); Coordinate lowerLeftDest = new Coordinate(dest.row, pivot.column - 1); @@ -147,13 +147,13 @@ public static Coordinate PartitionAndSearch(int[][] matrix, Coordinate origin, C return lowerLeft; } - public static Coordinate FindElement2(int[][] matrix, Coordinate origin, Coordinate dest, int x) + public static Coordinate FindElement2(int[,] matrix, Coordinate origin, Coordinate dest, int x) { if (!origin.Inbounds(matrix) || !dest.Inbounds(matrix)) { return null; } - if (matrix[origin.row][origin.column] == x) + if (matrix[origin.row, origin.column] == x) { return origin; } @@ -174,12 +174,13 @@ public static Coordinate FindElement2(int[][] matrix, Coordinate origin, Coordin while (start.IsBefore(end)) { p.SetToAverage(start, end); - if (x > matrix[p.row][p.column]) + if (x > matrix[p.row, p.column]) { start.row = p.row + 1; start.column = p.column + 1; } - else { + else + { end.row = p.row - 1; end.column = p.column - 1; } @@ -189,10 +190,10 @@ public static Coordinate FindElement2(int[][] matrix, Coordinate origin, Coordin return PartitionAndSearch(matrix, origin, dest, start, x); } - public static Coordinate FindElement2(int[][] matrix, int x) + public static Coordinate FindElement2(int[,] matrix, int x) { Coordinate origin = new Coordinate(0, 0); - Coordinate dest = new Coordinate(matrix.Length - 1, matrix[0].Length - 1); + Coordinate dest = new Coordinate(matrix.GetLength(0) - 1, matrix.GetLength(1) - 1); return FindElement2(matrix, origin, dest, x); } diff --git a/Ch 16. Moderate/Ch 16. Moderate.csproj b/Ch 16. Moderate/Ch 16. Moderate.csproj index b01a999..f9301ad 100644 --- a/Ch 16. Moderate/Ch 16. Moderate.csproj +++ b/Ch 16. Moderate/Ch 16. Moderate.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 diff --git a/Ch 16. Moderate/Q16_08_English_Int.cs b/Ch 16. Moderate/Q16_08_English_Int.cs index 4f5134a..c8a9d2d 100644 --- a/Ch 16. Moderate/Q16_08_English_Int.cs +++ b/Ch 16. Moderate/Q16_08_English_Int.cs @@ -1,5 +1,4 @@ using ctci.Contracts; -using ctci.Library; using System; using System.Collections.Generic; using System.Text; diff --git a/Ch 17. Hard/Ch 17. Hard.csproj b/Ch 17. Hard/Ch 17. Hard.csproj new file mode 100644 index 0000000..f9301ad --- /dev/null +++ b/Ch 17. Hard/Ch 17. Hard.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0 + + + + + + + + \ No newline at end of file diff --git a/Ch 17. Hard/Q17_12_BiNode.cs b/Ch 17. Hard/Q17_12_BiNode.cs new file mode 100644 index 0000000..d67bbe4 --- /dev/null +++ b/Ch 17. Hard/Q17_12_BiNode.cs @@ -0,0 +1,202 @@ +using ctci.Contracts; +using ctci.Library; +using System; + +namespace Chapter17 +{ + public class Q17_12_BiNode : Question + { + public override void Run() + { + var root = Create(0, 1, 2, 3, 4, 5, 6); + BTreePrinter.Print(root); + Console.WriteLine("Solution 1"); + var sol1 = new Solution1(); + var result = sol1.Convert(root); + Display(result.Head); + + Console.WriteLine("Solution 2"); + root = Create(0, 1, 2, 3, 4, 5, 6); + var sol2 = new Solution2(); + var result2 = sol2.Convert(root); + Display(result2); + + Console.WriteLine("Solution 3"); + root = Create(0, 1, 2, 3, 4, 5, 6); + var sol3 = new Solution3(); + var result3 = sol3.Convert(root); + Display(result3); + } + + private static void Display(TreeNode curr) + { + while (curr.Right != null) + { + Console.Write($"{curr.Data} "); + curr = curr.Right; + } + Console.WriteLine($"{curr.Data} "); + + while (curr.Left != null) + { + Console.Write($"{curr.Data} "); + curr = curr.Left; + } + Console.WriteLine($"{curr.Data} "); + } + + public static TreeNode Create(params int[] sortedArray) + { + return Create(sortedArray, 0, sortedArray.Length - 1); + } + + private static TreeNode Create(int[] sortedArray, int left, int right) + { + if (left > right) return null; + var mid = left + (right - left) / 2; + var treeNode = new TreeNode(sortedArray[mid]); + treeNode.SetLeftChild(Create(sortedArray, left, mid - 1)); + treeNode.SetRightChild(Create(sortedArray, mid + 1, right)); + return treeNode; + } + + public class Solution1 + { + public NodePair Convert(TreeNode root) + { + if (root == null) return null; + + var part1 = Convert(root.Left); + var part2 = Convert(root.Right); + + if (part1 != null) + { + Concat(part1.Tail, root); + } + + if (part2 != null) + { + Concat(root, part2.Head); + } + + return new NodePair(part1 == null ? root : part1.Head, part2 == null ? root : part2.Tail); + } + + private void Concat(TreeNode x, TreeNode y) + { + x.Right = y; + y.Left = x; + } + + public class NodePair + { + public TreeNode Head { get; private set; } + public TreeNode Tail { get; private set; } + + public NodePair(TreeNode head, TreeNode tail) + { + this.Head = head; + this.Tail = tail; + } + + } + } + + public class Solution2 + { + public TreeNode Convert(TreeNode root) + { + if (root == null) return null; + + var part1 = Convert(root.Left); + var part2 = Convert(root.Right); + + if (part1 != null) + { + Concat(GetTail(part1), root); + } + + if (part2 != null) + { + Concat(root, part2); + } + + return part1 == null ? root : part1; + } + + private TreeNode GetTail(TreeNode part1) + { + if (part1 == null) return null; + while (part1.Right != null) part1 = part1.Right; + return part1; + } + + private void Concat(TreeNode x, TreeNode y) + { + x.Right = y; + y.Left = x; + } + } + + public class Solution3 + { + public TreeNode Convert(TreeNode root) + { + TreeNode head = ConvertToCircular(root); + head.Left.Right = null; + head.Left = null; + return head; + } + + private TreeNode ConvertToCircular(TreeNode root) + { + if (root == null) return null; + var part1 = ConvertToCircular(root.Left); + var part3 = ConvertToCircular(root.Right); + + if (part1 == null && part3 == null) + { + root.Left = root; + root.Right = root; + return root; + } + + var tail3 = part3 == null ? null : part3.Left; + + // Join left to root. + if (part1 == null) + { + Concat(part3.Left, root); + } + else + { + Concat(part1.Left, root); + } + + // Join right to root. + if (part3 == null) + { + Concat(root, part1); + } + else + { + Concat(root, part3); + } + + // join right to left + if (part1 != null && part3 != null) + { + Concat(tail3, part1); + } + + return part1 == null ? root : part1; + } + + private void Concat(TreeNode x, TreeNode y) + { + x.Right = y; + y.Left = x; + } + } + } +} diff --git a/Introduction/Introduction.csproj b/Introduction/Introduction.csproj index 294d446..dbf8049 100644 --- a/Introduction/Introduction.csproj +++ b/Introduction/Introduction.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 diff --git a/ctci.Contracts/ctci.Contracts.csproj b/ctci.Contracts/ctci.Contracts.csproj index b57c3c8..3cbda2f 100644 --- a/ctci.Contracts/ctci.Contracts.csproj +++ b/ctci.Contracts/ctci.Contracts.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 diff --git a/ctci.Library/AssortedMethods.cs b/ctci.Library/AssortedMethods.cs index f0e661f..9acf48a 100644 --- a/ctci.Library/AssortedMethods.cs +++ b/ctci.Library/AssortedMethods.cs @@ -28,15 +28,14 @@ public static bool RandomBoolean(int percentTrue) return RandomIntInRange(1, 100) <= percentTrue; } - public static int[][] RandomMatrix(int m, int n, int min, int max) + public static int[,] RandomMatrix(int rows, int columns, int min, int max) { - int[][] matrix = new int[m][]; - for (int i = 0; i < m; i++) + int[,] matrix = new int[rows, columns]; + for (int i = 0; i < rows; i++) { - matrix[i] = new int[n]; - for (int j = 0; j < n; j++) + for (int j = 0; j < columns; j++) { - matrix[i][j] = RandomIntInRange(min, max); + matrix[i, j] = RandomIntInRange(min, max); } } return matrix; @@ -127,59 +126,36 @@ public static string ToBaseNstring(int a, int baseN) return s; } - public static void PrintMatrix(int[][] matrix) + public static void PrintMatrix(int[,] matrix) { - for (int i = 0; i < matrix.Length; i++) + var height = matrix.GetLength(0); + var width = matrix.GetLength(1); + for (int i = 0; i < height; i++) { - for (int j = 0; j < matrix[i].Length; j++) + for (int j = 0; j < width; j++) { - if (matrix[i][j] < 10 && matrix[i][j] > -10) - { - Console.Write(" "); - } - if (matrix[i][j] < 100 && matrix[i][j] > -100) - { - Console.Write(" "); - } - if (matrix[i][j] >= 0) - { - Console.Write(" "); - } - Console.Write(" " + matrix[i][j]); + Console.Write($" {matrix[i, j],3}"); } Console.WriteLine(); } } - public static void PrintMatrix(bool[][] matrix) + public static void PrintMatrix(bool[,] matrix) { - for (int i = 0; i < matrix.Length; i++) + var height = matrix.GetLength(0); + var width = matrix.GetLength(1); + for (int i = 0; i < height; i++) { - for (int j = 0; j < matrix[i].Length; j++) + for (int j = 0; j < width; j++) { - if (matrix[i][j]) - { - Console.Write("1"); - } - else - { - Console.Write("0"); - } + Console.Write(matrix[i, j] ? "1" : "0"); } Console.WriteLine(); } } - public static void PrintIntArray(int[] array) - { - for (int i = 0; i < array.Length; i++) - { - Console.Write(array[i] + " "); - } - Console.WriteLine(""); - } - public static void PrintIntList(IEnumerable list) + public static void PrintList(IEnumerable list) { Console.Write("{"); foreach (var v in list) diff --git a/ctci.Library/ctci.Library.csproj b/ctci.Library/ctci.Library.csproj index b57c3c8..3cbda2f 100644 --- a/ctci.Library/ctci.Library.csproj +++ b/ctci.Library/ctci.Library.csproj @@ -1,7 +1,7 @@  - netcoreapp1.1 + netstandard2.0 diff --git a/ctci.sln b/ctci.sln index a5422bd..6ca5d15 100644 --- a/ctci.sln +++ b/ctci.sln @@ -3,29 +3,35 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26228.4 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ctci", "ctci\ctci.csproj", "{F7867FE4-E80B-44DD-9666-C82E6D401B8A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ctci", "ctci\ctci.csproj", "{F7867FE4-E80B-44DD-9666-C82E6D401B8A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 01. Arrays and Strings", "Ch 01. Arrays and Strings\Ch 01. Arrays and Strings.csproj", "{3F2DF0FA-D7A0-479B-B720-3AB7411E9539}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 01. Arrays and Strings", "Ch 01. Arrays and Strings\Ch 01. Arrays and Strings.csproj", "{3F2DF0FA-D7A0-479B-B720-3AB7411E9539}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ctci.Contracts", "ctci.Contracts\ctci.Contracts.csproj", "{D1B538B9-DE81-4FFF-93B2-A958ED2CE4DA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ctci.Contracts", "ctci.Contracts\ctci.Contracts.csproj", "{D1B538B9-DE81-4FFF-93B2-A958ED2CE4DA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ctci.Library", "ctci.Library\ctci.Library.csproj", "{8DC982E1-042C-4652-BBC4-CD375223C852}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ctci.Library", "ctci.Library\ctci.Library.csproj", "{8DC982E1-042C-4652-BBC4-CD375223C852}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Introduction", "Introduction\Introduction.csproj", "{AF745A68-20A9-4FF4-801C-86A3B0383C49}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Introduction", "Introduction\Introduction.csproj", "{AF745A68-20A9-4FF4-801C-86A3B0383C49}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Helpers", "Helpers", "{F9BD47E4-94CA-4823-B074-22E8ABE9B8C8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Questions", "Questions", "{8E563D72-A200-4BB7-8AB8-A93BEED3A5B7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 05. Bit Manipulation", "Ch 05. Bit Manipulation\Ch 05. Bit Manipulation.csproj", "{6515F74F-66C6-414D-B5EC-10260AAD473C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 05. Bit Manipulation", "Ch 05. Bit Manipulation\Ch 05. Bit Manipulation.csproj", "{6515F74F-66C6-414D-B5EC-10260AAD473C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 02. Linked Lists", "Ch 02. Linked Lists\Ch 02. Linked Lists.csproj", "{D1ACE913-9486-4CDC-B980-A5F848A67108}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 02. Linked Lists", "Ch 02. Linked Lists\Ch 02. Linked Lists.csproj", "{D1ACE913-9486-4CDC-B980-A5F848A67108}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 10. Sorting and Searching", "Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.csproj", "{90D56A57-CEAD-42F8-A978-BC2D33530E0E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 10. Sorting and Searching", "Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.csproj", "{90D56A57-CEAD-42F8-A978-BC2D33530E0E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 16. Moderate", "Ch 16. Moderate\Ch 16. Moderate.csproj", "{AE0D044B-1AE9-430F-B05D-ADAC72C407B8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 16. Moderate", "Ch 16. Moderate\Ch 16. Moderate.csproj", "{AE0D044B-1AE9-430F-B05D-ADAC72C407B8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 04. Trees", "Ch 04. Trees\Ch 04. Trees.csproj", "{AF583937-1A80-407F-B683-3FEED7F3BC16}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 04. Trees", "Ch 04. Trees\Ch 04. Trees.csproj", "{AF583937-1A80-407F-B683-3FEED7F3BC16}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 17. Hard", "Ch 17. Hard\Ch 17. Hard.csproj", "{9C214A20-E419-4731-9E19-302D9957AFF3}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0079C857-D80C-4FF5-8F48-206CB6D2F3E6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 01. Arrays and Strings Tests", "tests\Ch 01. Arrays and Strings Tests\Ch 01. Arrays and Strings Tests.csproj", "{072EBD5B-C529-4690-B5A3-9EAA4DB43202}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -73,6 +79,14 @@ Global {AF583937-1A80-407F-B683-3FEED7F3BC16}.Debug|Any CPU.Build.0 = Debug|Any CPU {AF583937-1A80-407F-B683-3FEED7F3BC16}.Release|Any CPU.ActiveCfg = Release|Any CPU {AF583937-1A80-407F-B683-3FEED7F3BC16}.Release|Any CPU.Build.0 = Release|Any CPU + {9C214A20-E419-4731-9E19-302D9957AFF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C214A20-E419-4731-9E19-302D9957AFF3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C214A20-E419-4731-9E19-302D9957AFF3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C214A20-E419-4731-9E19-302D9957AFF3}.Release|Any CPU.Build.0 = Release|Any CPU + {072EBD5B-C529-4690-B5A3-9EAA4DB43202}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {072EBD5B-C529-4690-B5A3-9EAA4DB43202}.Debug|Any CPU.Build.0 = Debug|Any CPU + {072EBD5B-C529-4690-B5A3-9EAA4DB43202}.Release|Any CPU.ActiveCfg = Release|Any CPU + {072EBD5B-C529-4690-B5A3-9EAA4DB43202}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -88,5 +102,10 @@ Global {90D56A57-CEAD-42F8-A978-BC2D33530E0E} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} {AE0D044B-1AE9-430F-B05D-ADAC72C407B8} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} {AF583937-1A80-407F-B683-3FEED7F3BC16} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} + {9C214A20-E419-4731-9E19-302D9957AFF3} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} + {072EBD5B-C529-4690-B5A3-9EAA4DB43202} = {0079C857-D80C-4FF5-8F48-206CB6D2F3E6} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E61B239A-17C9-48B6-9C08-0567ECB16243} EndGlobalSection EndGlobal diff --git a/ctci/Program.cs b/ctci/Program.cs index eb053fb..875aea0 100644 --- a/ctci/Program.cs +++ b/ctci/Program.cs @@ -4,6 +4,7 @@ using Chapter05; using Chapter10; using Chapter16; +using Chapter17; using ctci.Contracts; using Introduction; using System; @@ -26,7 +27,7 @@ private static void Main(string[] args) new Q1_02_Check_Permutation(), new Q1_03_URLify(), new Q1_04_Palindrome_Permutation(), - new Q1_05_One_Away_A(), + new Q1_05_One_Edit_Away(), new Q1_06_String_Compression(), new Q1_07_Rotate_Matrix(), new Q1_08_Zero_Matrix(), @@ -50,6 +51,7 @@ private static void Main(string[] args) new Q4_04_CheckBalanced(), new Q4_05_Validate_BST(), new Q4_06_Successor(), + new Q4_07_Build_Order(), new Q4_08_LowestCommonAncestorNotBST(), new Q4_09_BST_Sequence(), new Q4_10_Check_SubTree(), @@ -86,6 +88,11 @@ private static void Main(string[] args) new Q16_08_English_Int(), new Q16_19_Pond_Sizes(), new Boggle() + }, + + new Question[] + { + new Q17_12_BiNode(), } }; diff --git a/ctci/ctci.csproj b/ctci/ctci.csproj index 20fcae9..971c1fa 100644 --- a/ctci/ctci.csproj +++ b/ctci/ctci.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp1.1 + netcoreapp2.0 @@ -16,6 +16,7 @@ + diff --git a/tests/Ch 01. Arrays and Strings Tests/Ch 01. Arrays and Strings Tests.csproj b/tests/Ch 01. Arrays and Strings Tests/Ch 01. Arrays and Strings Tests.csproj new file mode 100644 index 0000000..3ac99a6 --- /dev/null +++ b/tests/Ch 01. Arrays and Strings Tests/Ch 01. Arrays and Strings Tests.csproj @@ -0,0 +1,14 @@ + + + + netcoreapp2.2 + Ch_01._Arrays_and_Strings_Tests + + false + + + + + + + diff --git a/tests/Ch 01. Arrays and Strings Tests/IsUniqueTests.cs b/tests/Ch 01. Arrays and Strings Tests/IsUniqueTests.cs new file mode 100644 index 0000000..a1415e3 --- /dev/null +++ b/tests/Ch 01. Arrays and Strings Tests/IsUniqueTests.cs @@ -0,0 +1,30 @@ +using Chapter01; +using FluentAssertions; +using Xunit; + +namespace Ch_01._Arrays_and_Strings_Tests +{ + public class IsUniqueTests + { + [Theory] + [InlineData("abcde")] + [InlineData("kite")] + [InlineData("padle")] + public void IsUnique_should_be_true(string word) + { + var unique = new Q1_01_Is_Unique(); + unique.IsUniqueChars(word).Should().BeTrue(); + unique.IsUniqueChars2(word).Should().BeTrue(); + } + + [Theory] + [InlineData("hello")] + [InlineData("apple")] + public void IsUnique_should_be_false(string word) + { + var unique = new Q1_01_Is_Unique(); + unique.IsUniqueChars(word).Should().BeFalse(); + unique.IsUniqueChars2(word).Should().BeFalse(); + } + } +} diff --git a/tests/directory.build.props b/tests/directory.build.props new file mode 100644 index 0000000..16e4a87 --- /dev/null +++ b/tests/directory.build.props @@ -0,0 +1,18 @@ + + + + + + PreserveNewest + false + + + + + + + + + + + \ No newline at end of file diff --git a/tests/xunit.runner.json b/tests/xunit.runner.json new file mode 100644 index 0000000..0d69ec4 --- /dev/null +++ b/tests/xunit.runner.json @@ -0,0 +1,4 @@ +{ + "methodDisplay": "method", + "methodDisplayOptions": "all" +} \ No newline at end of file