Skip to content

Commit 33567ea

Browse files
committed
Added class and test method for code challenge Matrix Determinant.
1 parent b8ebbca commit 33567ea

File tree

4 files changed

+180
-8
lines changed

4 files changed

+180
-8
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Coderbyte_CSharp.Hard_Challenges
8+
{
9+
class Determinant
10+
{
11+
// Have the function MatrixDeterminant(strArr) read strArr which will be an
12+
// array of integers represented as strings.Within the array there will also
13+
// be "<>" elements which represent break points.The array will make up a
14+
// matrix where the(number of break points + 1) represents the number of
15+
// rows.Here is an example of how strArr may look : ["1", "2", "<>", "3", "4"].
16+
//
17+
// The contents of this array are row1 = [1 2] and row2 = [3 4]. Your program
18+
// should take the given array of elements, create the proper matrix, and then
19+
// calculate the determinant. For the example above, your program should return -2.
20+
// If the matrix is not a square matrix, return -1. The maximum size of strArr
21+
// will be a 6x6 matrix.The determinant will always be an integer.
22+
public int MatrixDeterminant(string[] strArr, int size)
23+
{
24+
int determinant = 0;
25+
26+
List<List<int>> matrix = new List<List<int>>();
27+
28+
ParseMatrix(strArr, size, ref matrix);
29+
30+
31+
if (IsSquareMatrix(matrix))
32+
{
33+
determinant = ComputeDeterminant(matrix);
34+
}
35+
36+
else
37+
{
38+
Console.WriteLine("matrix is not square");
39+
}
40+
41+
42+
return determinant;
43+
}
44+
45+
protected bool IsSquareMatrix(List<List<int>> matrix)
46+
{
47+
bool isSquare = true;
48+
49+
// checking if input matrix is square
50+
for (int row = 0; row < matrix.Count; row++)
51+
{
52+
if (matrix[row].Count != matrix.Count)
53+
{
54+
isSquare = false;
55+
break;
56+
}
57+
}
58+
59+
return isSquare;
60+
}
61+
62+
protected void ParseMatrix(string[] strArr, int size, ref List<List<int>> matrix)
63+
{
64+
List<int> rowData = new List<int>();
65+
66+
for (int x = 0; x < size; x++)
67+
{
68+
// condition to check for breakpoints
69+
if (strArr[x] == "<>")
70+
{
71+
// Create a separate copy of rowData for matrix
72+
matrix.Add(new List<int>(rowData));
73+
rowData.Clear();
74+
}
75+
else
76+
{
77+
// converting to int
78+
int value = 0;
79+
80+
// convert to snippet
81+
try
82+
{
83+
value = Int32.Parse(strArr[x]);
84+
}
85+
catch (FormatException e)
86+
{
87+
Console.WriteLine("Unable to parse {0}", strArr[x]);
88+
Console.Error.Write(e.Message);
89+
}
90+
91+
// adding to our row
92+
rowData.Add(value);
93+
}
94+
}
95+
// adding last row
96+
// Create a separate copy of rowData for matrix
97+
matrix.Add(new List<int>(rowData));
98+
}
99+
100+
protected int ComputeDeterminant(List<List<int>> matrix)
101+
{
102+
int determinant = 0;
103+
104+
if (matrix.Count == 1)
105+
{
106+
determinant = matrix[0][0];
107+
}
108+
// check for 2-by-2 matrix perform det calculation
109+
else if (matrix.Count == 2)
110+
{
111+
determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
112+
}
113+
114+
// otherwise determine cofactor, submatrix and recursively compute determinant
115+
else
116+
{
117+
int cofactor = 0;
118+
// iterate across first row to set cofactor
119+
for (int p = 0; p < matrix[0].Count; p++)
120+
{
121+
// create submatrix
122+
List<List<int>> subMatrix = new List<List<int>>();
123+
int matrixSize = matrix.Count;
124+
125+
for (int i = 1; i < matrixSize; i++)
126+
{
127+
// iteration will start from row one cancelling the first row values
128+
List<int> tempRow = new List<int>();
129+
130+
for (int j = 0; j < matrixSize; j++)
131+
{
132+
// iteration will pass all cells of the i row excluding the j
133+
//value that match p column
134+
if (j != p)
135+
{
136+
tempRow.Add(matrix[i][j]);//add current cell to TempRow
137+
}
138+
}
139+
140+
// Adding each row to submatrix
141+
if (tempRow.Count > 0)
142+
{
143+
subMatrix.Add(tempRow);
144+
}
145+
}
146+
147+
// recursively calculate the determinant value
148+
cofactor = matrix[0][p];
149+
determinant += (int)(Math.Pow(-1, p)) * cofactor * ComputeDeterminant(subMatrix);
150+
}
151+
}
152+
153+
return determinant;
154+
}
155+
156+
}
157+
}

Coderbyte_CSharp/Coderbyte_CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="2 Medium Challenges\StringUniqueSubstring.cs" />
5959
<Compile Include="2 Medium Challenges\TreeGraphs.cs" />
6060
<Compile Include="3 Hard Challenges\Kaprekar.cs" />
61+
<Compile Include="3 Hard Challenges\MatrixDeterminant.cs" />
6162
<Compile Include="Program.cs" />
6263
<Compile Include="Properties\AssemblyInfo.cs" />
6364
<Compile Include="TestChallenge.cs" />

Coderbyte_CSharp/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ static void Main(string[] args)
3131
tc.Test_MinWindowSubstring();
3232
tc.Test_RunLength();
3333
tc.Test_StringReduction();
34-
tc.Test_TreeConstructor();
34+
//tc.Test_TreeConstructor();
3535

3636
// Hard
3737
tc.Test_KaprekarsConstant();
38+
tc.Test_Determinant();
3839
}
3940
}
4041
}

Coderbyte_CSharp/TestChallenge.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Coderbyte_CSharp
1313
class TestChallenge
1414
{
1515
#region Support Methods
16-
protected void printArray(int[] arr, int length)
16+
protected void PrintArray(int[] arr, int length)
1717
{
1818

1919
List<int> data = arr.ToList<int>();
@@ -34,7 +34,7 @@ protected void printArray(int[] arr, int length)
3434
Console.WriteLine();
3535
}
3636

37-
protected void printArray(string[] arr, int length)
37+
protected void PrintArray(string[] arr, int length)
3838
{
3939

4040
List<string> data = arr.ToList<string>();
@@ -88,7 +88,7 @@ public void Test_ArithGeoSequence()
8888
MathSequence sequence = new MathSequence(); ;
8989

9090
Console.WriteLine("ArithGeo Sequence:");
91-
printArray(arr, length);
91+
PrintArray(arr, length);
9292
Console.WriteLine("Output: {0}", sequence.ArithGeo(arr, length));
9393
Console.WriteLine();
9494

@@ -167,7 +167,7 @@ public void Test_Consecutive()
167167

168168
Console.WriteLine("ArithGeo Sequence:");
169169

170-
printArray(arr, arr.Length);
170+
PrintArray(arr, arr.Length);
171171
Console.WriteLine("Output: {0}", numbers.Consecutive(arr, arr.Length));
172172
Console.WriteLine();
173173

@@ -294,17 +294,17 @@ public void Test_TreeConstructor()
294294
string[] strArr = new string[] { "(1,2)", "(2,4)", "(7,2)" };
295295

296296
Console.WriteLine("Tree Constructor:");
297-
printArray(strArr, strArr.Length);
297+
PrintArray(strArr, strArr.Length);
298298
Console.WriteLine("Output: {0}", tree.TreeConstructor(strArr, strArr.Length));
299299
Console.WriteLine();
300300

301301
string[] strArr2 = new string[] { "(1,2)", "(9,2)", "(2,4)", "(7,2)" };
302-
printArray(strArr2, strArr2.Length);
302+
PrintArray(strArr2, strArr2.Length);
303303
Console.WriteLine("Output: {0}", tree.TreeConstructor(strArr2, strArr2.Length));
304304
Console.WriteLine();
305305

306306
string[] strArr3 = new string[] { "(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)" };
307-
printArray(strArr3, strArr3.Length);
307+
PrintArray(strArr3, strArr3.Length);
308308
Console.WriteLine("Output: {0}", tree.TreeConstructor(strArr3, strArr3.Length));
309309
Console.WriteLine();
310310
}
@@ -320,6 +320,19 @@ public void Test_KaprekarsConstant()
320320
Console.WriteLine("Input: {0} ", value);
321321
Console.WriteLine("Output: {0}", k.KaprekarsConstant(value));
322322
Console.WriteLine();
323+
}
324+
325+
public void Test_Determinant()
326+
{
327+
Determinant d = new Determinant();
328+
329+
string[] array = new string[] { "1", "4", "3", "<>", "2", "3", "0", "<>", "5", "-3", "4" };
330+
int det;
331+
332+
det = d.MatrixDeterminant(array, array.Length);
333+
PrintArray(array, array.Length);
334+
Console.WriteLine("Output: {0}", d.MatrixDeterminant(array, array.Length));
335+
Console.WriteLine();
323336

324337
}
325338
#endregion

0 commit comments

Comments
 (0)