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.98 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.97 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.96 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.95 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.94 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.93 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.92 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.91 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.90 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.89 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.88 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.87 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.86 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.85 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.84 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.83 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.82 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.81 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.80 tracks/csharp/exercises/saddle-points/Example.cs
trackler-2.2.1.79 tracks/csharp/exercises/saddle-points/Example.cs