Skip to content

Commit 4c1e05d

Browse files
Merge pull request #57 from ErikSchierboom/matrix
Add matrix exercise
2 parents 596c45d + 548d87e commit 4c1e05d

File tree

5 files changed

+120
-9
lines changed

5 files changed

+120
-9
lines changed

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"palindrome-products",
5757
"binary-search-tree",
5858
"robot-simulator",
59-
"simple-linked-list"
59+
"simple-linked-list",
60+
"matrix"
6061
],
6162
"deprecated": [
6263

exercises/matrix/Example.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Linq;
2+
3+
public class Matrix
4+
{
5+
private readonly int[][] _rows;
6+
private readonly int[][] _cols;
7+
8+
public Matrix(string input)
9+
{
10+
_rows = ParseRows(input);
11+
_cols = ParseCols(_rows);
12+
}
13+
14+
public int Rows => _rows.Length;
15+
public int Cols => _cols.Length;
16+
17+
public int[] Row(int row) => _rows[row];
18+
public int[] Col(int col) => _cols[col];
19+
20+
private static int[][] ParseRows(string input)
21+
{
22+
return input.Split('\n')
23+
.Select(ParseRow)
24+
.ToArray();
25+
}
26+
27+
private static int[] ParseRow(string row)
28+
{
29+
return row.Split(' ')
30+
.Select(int.Parse)
31+
.ToArray();
32+
}
33+
34+
private static int[][] ParseCols(int[][] rows)
35+
{
36+
return Enumerable.Range(0, rows[0].Length)
37+
.Select(y => ParseCol(rows, y))
38+
.ToArray();
39+
}
40+
41+
private static int[] ParseCol(int[][] rows, int y)
42+
{
43+
return Enumerable.Range(0, rows.Length)
44+
.Select(x => rows[x][y])
45+
.ToArray();
46+
}
47+
}

exercises/matrix/MatrixTest.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using NUnit.Framework;
2+
3+
public class MatrixTest
4+
{
5+
[TestCase("1", ExpectedResult = new[] { 1 })]
6+
[TestCase("4 7", ExpectedResult = new[] { 4, 7 }, Ignore = "Remove to run test case")]
7+
[TestCase("1 2\n10 20", ExpectedResult = new[] { 1, 2 }, Ignore = "Remove to run test case")]
8+
[TestCase("9 7 6\n8 6 5\n5 3 2", ExpectedResult = new[] { 9, 7, 6 }, Ignore = "Remove to run test case")]
9+
public int[] Extract_first_row(string input)
10+
{
11+
var matrix = new Matrix(input);
12+
return matrix.Row(0);
13+
}
14+
15+
[TestCase("5", ExpectedResult = new[] { 5 }, Ignore = "Remove to run test case")]
16+
[TestCase("9 7", ExpectedResult = new[] { 9, 7 }, Ignore = "Remove to run test case")]
17+
[TestCase("9 8 7\n19 18 17", ExpectedResult = new[] { 19, 18, 17 }, Ignore = "Remove to run test case")]
18+
[TestCase("1 4 9\n16 25 36\n5 6 7", ExpectedResult = new[] { 5, 6, 7 }, Ignore = "Remove to run test case")]
19+
public int[] Extract_last_row(string input)
20+
{
21+
var matrix = new Matrix(input);
22+
return matrix.Row(matrix.Rows - 1);
23+
}
24+
25+
[TestCase("55 44", ExpectedResult = new[] { 55 }, Ignore = "Remove to run test case")]
26+
[TestCase("89 1903\n18 3", ExpectedResult = new[] { 89, 18 }, Ignore = "Remove to run test case")]
27+
[TestCase("1 2 3\n4 5 6\n7 8 9\n8 7 6", ExpectedResult = new[] { 1, 4, 7, 8 }, Ignore = "Remove to run test case")]
28+
public int[] Extract_first_column(string input)
29+
{
30+
var matrix = new Matrix(input);
31+
return matrix.Col(0);
32+
}
33+
34+
[TestCase("28", ExpectedResult = new[] { 28 }, Ignore = "Remove to run test case")]
35+
[TestCase("13\n16\n19", ExpectedResult = new[] { 13, 16, 19 }, Ignore = "Remove to run test case")]
36+
[TestCase("289 21903 23\n218 23 21", ExpectedResult = new[] { 23, 21 }, Ignore = "Remove to run test case")]
37+
[TestCase("11 12 13\n14 15 16\n17 18 19\n18 17 16", ExpectedResult = new[] { 13, 16, 19, 16 }, Ignore = "Remove to run test case")]
38+
public int[] Extract_last_column(string input)
39+
{
40+
var matrix = new Matrix(input);
41+
return matrix.Col(matrix.Cols - 1);
42+
}
43+
44+
[TestCase("28", ExpectedResult = 1, Ignore = "Remove to run test case")]
45+
[TestCase("13\n16", ExpectedResult = 2, Ignore = "Remove to run test case")]
46+
[TestCase("289 21903\n23 218\n23 21", ExpectedResult = 3, Ignore = "Remove to run test case")]
47+
[TestCase("11 12 13\n14 15 16\n17 18 19\n18 17 16", ExpectedResult = 4, Ignore = "Remove to run test case")]
48+
public int Number_of_rows(string input)
49+
{
50+
var matrix = new Matrix(input);
51+
return matrix.Rows;
52+
}
53+
54+
[TestCase("28", ExpectedResult = 1, Ignore = "Remove to run test case")]
55+
[TestCase("13 2\n16 3\n19 4", ExpectedResult = 2, Ignore = "Remove to run test case")]
56+
[TestCase("289 21903\n23 218\n23 21", ExpectedResult = 2, Ignore = "Remove to run test case")]
57+
[TestCase("11 12 13\n14 15 16\n17 18 19\n18 17 16", ExpectedResult = 3, Ignore = "Remove to run test case")]
58+
public int Number_of_columns(string input)
59+
{
60+
var matrix = new Matrix(input);
61+
return matrix.Cols;
62+
}
63+
}

exercises/saddle-points/Example.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
using System.Collections.Generic;
33
using System.Linq;
44

5-
public class Matrix
5+
public class SaddlePoints
66
{
77
private readonly int[,] values;
88
private readonly int[] maxRows;
99
private readonly int[] minCols;
1010

11-
public Matrix(int[,] values)
11+
public SaddlePoints(int[,] values)
1212
{
1313
this.values = values;
1414
this.maxRows = Rows().Select(r => r.Max()).ToArray();
1515
this.minCols = Columns().Select(r => r.Min()).ToArray();
1616
}
1717

18-
public IEnumerable<Tuple<int, int>> SaddlePoints()
18+
public IEnumerable<Tuple<int, int>> Calculate()
1919
{
2020
return Coordinates().Where(IsSaddlePoint);
2121
}

exercises/saddle-points/SaddlePointTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void Readme_example()
1212
{ 5, 3, 2 },
1313
{ 6, 6, 7 }
1414
};
15-
var actual = new Matrix(values).SaddlePoints();
15+
var actual = new SaddlePoints(values).Calculate();
1616
Assert.That(actual, Is.EqualTo(new [] { Tuple.Create(1, 0)}));
1717
}
1818

@@ -25,7 +25,7 @@ public void No_saddle_point()
2525
{ 2, 1 },
2626
{ 1, 2 }
2727
};
28-
var actual = new Matrix(values).SaddlePoints();
28+
var actual = new SaddlePoints(values).Calculate();
2929
Assert.That(actual, Is.Empty);
3030
}
3131

@@ -38,7 +38,7 @@ public void Saddle_point()
3838
{ 1, 2 },
3939
{ 3, 4 }
4040
};
41-
var actual = new Matrix(values).SaddlePoints();
41+
var actual = new SaddlePoints(values).Calculate();
4242
Assert.That(actual, Is.EqualTo(new[] { Tuple.Create(0, 1) }));
4343
}
4444

@@ -52,7 +52,7 @@ public void Another_saddle_point()
5252
{ 38, 10, 8, 77, 320 },
5353
{ 3, 4, 8, 6, 7 }
5454
};
55-
var actual = new Matrix(values).SaddlePoints();
55+
var actual = new SaddlePoints(values).Calculate();
5656
Assert.That(actual, Is.EqualTo(new[] { Tuple.Create(2, 2) }));
5757
}
5858

@@ -66,7 +66,7 @@ public void Multiple_saddle_points()
6666
{ 3, 5, 5 },
6767
{ 1, 5, 4 }
6868
};
69-
var actual = new Matrix(values).SaddlePoints();
69+
var actual = new SaddlePoints(values).Calculate();
7070
Assert.That(actual, Is.EqualTo(new[] { Tuple.Create(0, 1), Tuple.Create(1, 1), Tuple.Create(2, 1) }));
7171
}
7272
}

0 commit comments

Comments
 (0)