Avatar of grimord

grimord's solution

to Matrix in the C# Track

Published at Sep 07 2019 · 0 comments
Instructions
Test suite
Solution

Given a string representing a matrix of numbers, return the rows and columns of that matrix.

So given a string with embedded newlines like:

9 8 7
5 3 2
6 6 7

representing this matrix:

    1  2  3
  |---------
1 | 9  8  7
2 | 5  3  2
3 | 6  6  7

your code should be able to spit out:

  • A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
  • A list of the columns, reading each column top-to-bottom while moving from left-to-right.

The rows for our example matrix:

  • 9, 8, 7
  • 5, 3, 2
  • 6, 6, 7

And its columns:

  • 9, 5, 6
  • 8, 3, 6
  • 7, 2, 7

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Matrix.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Warmup to the saddle-points warmup. http://jumpstartlab.com

MatrixTest.cs

// This file was auto-generated based on version 1.3.0 of the canonical data.

using Xunit;

public class MatrixTest
{
    [Fact]
    public void Extract_row_from_one_number_matrix()
    {
        var sut = new Matrix("1");
        Assert.Equal(new[] { 1 }, sut.Row(1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_extract_row()
    {
        var sut = new Matrix("1 2\n3 4");
        Assert.Equal(new[] { 3, 4 }, sut.Row(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Extract_row_where_numbers_have_different_widths()
    {
        var sut = new Matrix("1 2\n10 20");
        Assert.Equal(new[] { 10, 20 }, sut.Row(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_extract_row_from_non_square_matrix_with_no_corresponding_column()
    {
        var sut = new Matrix("1 2 3\n4 5 6\n7 8 9\n8 7 6");
        Assert.Equal(new[] { 8, 7, 6 }, sut.Row(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Extract_column_from_one_number_matrix()
    {
        var sut = new Matrix("1");
        Assert.Equal(new[] { 1 }, sut.Column(1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_extract_column()
    {
        var sut = new Matrix("1 2 3\n4 5 6\n7 8 9");
        Assert.Equal(new[] { 3, 6, 9 }, sut.Column(3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_extract_column_from_non_square_matrix_with_no_corresponding_row()
    {
        var sut = new Matrix("1 2 3 4\n5 6 7 8\n9 8 7 6");
        Assert.Equal(new[] { 4, 8, 6 }, sut.Column(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Extract_column_where_numbers_have_different_widths()
    {
        var sut = new Matrix("89 1903 3\n18 3 1\n9 4 800");
        Assert.Equal(new[] { 1903, 3, 4 }, sut.Column(2));
    }
}
using System;
using System.Collections.Generic;
using System.Linq;

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

    private enum VectorType
    {
        Row,
        Column
    }

    public Matrix(string input)
    {
        Rows = input.Count(x => x == '\n') + 1;
        Cols = (input.Count(y => y == ' ') / Rows) + 1;

        matrix = new int[Rows, Cols];

        var lines = input.Split('\n');
        for (var i = 0; i < lines.Length; i++)
        {
            var numbers = lines[i].Split(' ');
            for (var j = 0; j < numbers.Length; j++)
                matrix[i, j] = int.Parse(numbers[j]);
        }

    }

    public int Rows { get; }

    public int Cols { get; }

    public int[] Row(int row) => GetVector(row, Cols, VectorType.Row);

    public int[] Column(int col) => GetVector(col, Rows, VectorType.Column);

    private int[] GetVector(int index, int size, VectorType type)
    {
        var aux = new int[size];

        for (int i = 0; i < size; i++)
            aux[i] = type == VectorType.Row ? matrix[index - 1, i] : matrix[i, index - 1];

        return aux;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?