From 884e56fcf19043d410025e636c2b180fa5c86b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Sat, 13 Aug 2022 20:59:51 +0100 Subject: [PATCH 1/2] Basic mentoring notes for the matrix problem The problem can be solve in a number of ways, and it is good to have the examples ready to guide the students. The alternative solution with multidimensional tables gets unnecessarily complex quickly so is not included. --- tracks/csharp/exercises/matrix/mentoring.md | 71 +++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tracks/csharp/exercises/matrix/mentoring.md diff --git a/tracks/csharp/exercises/matrix/mentoring.md b/tracks/csharp/exercises/matrix/mentoring.md new file mode 100644 index 000000000..c392f3d79 --- /dev/null +++ b/tracks/csharp/exercises/matrix/mentoring.md @@ -0,0 +1,71 @@ +### Reasonable solutions: + +Solution without Linq: +```csharp +using System.Collections.Generic; + +public class Matrix +{ + private readonly int[][] matrix; + + public Matrix(string input) + { + var output = new List(); + + foreach(var rowString in input.Split("\n")) { + var row = new List(); + foreach(var v in rowString.Split(" ")) { + row.Add(int.Parse(v)); + } + output.Add(row.ToArray()); + } + + matrix = output.ToArray(); + } + + public IEnumerable Row(int row) + => matrix[row-1]; + + public IEnumerable Column(int col) + { + foreach(var row in matrix) { + yield return row[col-1]; + } + } +} +``` + +Solution with Linq: +```csharp +using System.Linq; +using System.Collections.Generic; + +public class Matrix +{ + private readonly int[][] matrix; + + public Matrix(string input) + { + matrix = input + .Split("\n") + .Select(r => r.Split(" ").Select(x => int.Parse(x)).ToArray()) + .ToArray(); + } + + public IEnumerable Row(int row) + => matrix[row-1]; + + public IEnumerable Column(int col) + => matrix.Select(x => x[col-1]); +} +``` + +### Common suggestions: Good suggestions specific to this exercise. Good lessons that emerge from it. + +- While it is possible to solve the problem using multidimensional arrays the jagged array is easier to work with. + +### Talking points: Questions to challenge more advance learners with. + +- The differences between multidimensional and jagged arrays in general and in C#. +- There are two problems: paring of input and responding to queries. +- The problem can be solved with iteration using loops and using Linq. What's the difference? From aca4c29012e04e0eeacbcecf0378130e37cdcfd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 13 Sep 2022 13:09:48 +0100 Subject: [PATCH 2/2] Improvements to formatting It is based on the discussion from PR #2212 --- tracks/csharp/exercises/matrix/mentoring.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tracks/csharp/exercises/matrix/mentoring.md b/tracks/csharp/exercises/matrix/mentoring.md index c392f3d79..f662459e6 100644 --- a/tracks/csharp/exercises/matrix/mentoring.md +++ b/tracks/csharp/exercises/matrix/mentoring.md @@ -60,12 +60,20 @@ public class Matrix } ``` -### Common suggestions: Good suggestions specific to this exercise. Good lessons that emerge from it. +### Common suggestions: +*Good suggestions specific to this exercise. Good lessons that emerge from it.* -- While it is possible to solve the problem using multidimensional arrays the jagged array is easier to work with. +- While it is possible to solve the problem using multidimensional arrays the jagged array is easier to work with. +This is because it is possible to return a single row simply as `matrix[r]`. -### Talking points: Questions to challenge more advance learners with. +### Talking points: +*Questions to challenge more advance learners with.* - The differences between multidimensional and jagged arrays in general and in C#. -- There are two problems: paring of input and responding to queries. +In this case the data is dense so there are no memory efficiencies to talk about. +However, the choice influeces the type of code we can write. +- There are two problems: parsing of input and responding to queries. +Both are relatively simple and there is no need for input checking. - The problem can be solved with iteration using loops and using Linq. What's the difference? +Both the parsing and access to column data change. +The change in parsing is more obvious between very imperative use of two loops vs more declarative expression using linq.