Sha256: 0e8a3204ff17fbc89c45a3ce6720573a0eb103ca4152176036661b90aa6adc65

Contents?: true

Size: 1.41 KB

Versions: 396

Compression:

Stored size: 1.41 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public class SaddlePoints
{
    private readonly int[,] values;
    private readonly int[] maxRows;
    private readonly int[] minCols;

    public SaddlePoints(int[,] values)
    {
        this.values = values;
        this.maxRows = Rows().Select(r => r.Max()).ToArray();
        this.minCols = Columns().Select(r => r.Min()).ToArray();
    }

    public IEnumerable<Tuple<int, int>> Calculate()
    {
        return Coordinates().Where(IsSaddlePoint);
    }

    private bool IsSaddlePoint(Tuple<int, int> coordinate)
    {
        return maxRows[coordinate.Item1] == values[coordinate.Item1, coordinate.Item2] &&
                minCols[coordinate.Item2] == values[coordinate.Item1, coordinate.Item2];
    }

    private IEnumerable<Tuple<int, int>> Coordinates()
    {
        return Enumerable.Range(0, RowCount).SelectMany(x => Enumerable.Range(0, ColumnCount).Select(y => Tuple.Create(x, y)));
    }

    private IEnumerable<IEnumerable<int>> Rows()
    {
        return Enumerable.Range(0, RowCount).Select(x => Enumerable.Range(0, ColumnCount).Select(y => values[x, y]));
    }

    private IEnumerable<IEnumerable<int>> Columns()
    {
        return Enumerable.Range(0, ColumnCount).Select(y => Enumerable.Range(0, RowCount).Select(x => values[x, y]));
    }

    private int ColumnCount => values.GetLength(1);

    private int RowCount => values.GetLength(0);
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/saddle-points/Example.cs