Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[csharp/matrix] Basic csharp mentoring notes for the matrix problem #2213

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions tracks/csharp/exercises/matrix/mentoring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
### Reasonable solutions:

Solution without Linq:
```csharp
Comment on lines +3 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Solution without Linq:
```csharp
Solution without Linq:
```csharp

using System.Collections.Generic;

public class Matrix
{
private readonly int[][] matrix;

public Matrix(string input)
{
var output = new List<int[]>();

foreach(var rowString in input.Split("\n")) {
var row = new List<int>();
foreach(var v in rowString.Split(" ")) {
row.Add(int.Parse(v));
}
output.Add(row.ToArray());
}

matrix = output.ToArray();
}

public IEnumerable<int> Row(int row)
=> matrix[row-1];

public IEnumerable<int> Column(int col)
{
foreach(var row in matrix) {
yield return row[col-1];
}
}
}
```

Solution with Linq:
```csharp
Comment on lines +38 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Solution with Linq:
```csharp
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<int> Row(int row)
=> matrix[row-1];

public IEnumerable<int> Column(int col)
=> matrix.Select(x => x[col-1]);
}
```

### Common suggestions:
*Good suggestions specific to this exercise. Good lessons that emerge from it.*
Comment on lines +63 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Common suggestions:
*Good suggestions specific to this exercise. Good lessons that emerge from it.*
### Common suggestions


- 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.*

- The differences between multidimensional and jagged arrays in general and in C#.
Comment on lines +69 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Talking points:
*Questions to challenge more advance learners with.*
- The differences between multidimensional and jagged arrays in general and in C#.
### Talking points
- The differences between multidimensional and jagged arrays in general and in C#.

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.
Comment on lines +73 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
In this case the data is dense so there are no memory efficiencies to talk about.
However, the choice influences 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.