Sha256: 79d529ce4fc4d1f81a42c289fb74c1c6aee567ceca3150e1a18a15e534394ff8

Contents?: true

Size: 1.03 KB

Versions: 356

Compression:

Stored size: 1.03 KB

Contents

using System.Linq;

public class Matrix
{
    private readonly int[][] _rows;
    private readonly int[][] _cols;

    public Matrix(string input)
    {
        _rows = ParseRows(input);
        _cols = ParseCols(_rows);
    }

    public int Rows => _rows.Length;
    public int Cols => _cols.Length;

    public int[] Row(int row) => _rows[row];
    public int[] Col(int col) => _cols[col];

    private static int[][] ParseRows(string input)
    {
        return input.Split('\n')
            .Select(ParseRow)
            .ToArray();
    }

    private static int[] ParseRow(string row)
    {
        return row.Split(' ')
            .Select(int.Parse)
            .ToArray();
    }

    private static int[][] ParseCols(int[][] rows)
    {
        return Enumerable.Range(0, rows[0].Length)
            .Select(y => ParseCol(rows, y))
            .ToArray();
    }

    private static int[] ParseCol(int[][] rows, int y)
    {
        return Enumerable.Range(0, rows.Length)
            .Select(x => rows[x][y])
            .ToArray();
    }
}

Version data entries

356 entries across 356 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.138 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.137 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.136 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.135 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.134 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.133 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.132 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.131 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.130 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.129 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.128 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.127 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.126 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.125 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.124 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.123 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.122 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.121 tracks/csharp/exercises/matrix/Example.cs
trackler-2.2.1.120 tracks/csharp/exercises/matrix/Example.cs