 artemkorsakov's solution

to Saddle Points in the C# Track

Published at Apr 21 2019 · 0 comments
Instructions
Test suite
Solution

Detect saddle points in a matrix.

So say you have a matrix like so:

1  2  3
|---------
1 | 9  8  7
2 | 5  3  2     <--- saddle point at column 1, row 2, with value 5
3 | 6  6  7

It has a saddle point at column 1, row 2.

It's called a "saddle point" because it is greater than or equal to every element in its row and less than or equal to every element in its column.

A matrix may have zero or more saddle points.

Your code should be able to provide the (possibly empty) list of all the saddle points for any given matrix.

The matrix can have a different number of rows and columns (Non square).

Note that you may find other definitions of matrix saddle points online, but the tests for this exercise follow the above unambiguous definition.

Hints

For this exercise, you will need to create a set of factors using tuples. For more information on tuples, see this link.

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

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

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

using System;
using Xunit;

{
[Fact]
{
var matrix = new[,]
{
{ 9, 8, 7 },
{ 5, 3, 2 },
{ 6, 6, 7 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (2, 1) };
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new int[,] { };
var actual = SaddlePoints.Calculate(matrix);
Assert.Empty(actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 1, 2, 3 },
{ 3, 1, 2 },
{ 2, 3, 1 }
};
var actual = SaddlePoints.Calculate(matrix);
Assert.Empty(actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 4, 5, 4 },
{ 3, 5, 5 },
{ 1, 5, 4 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (1, 2), (2, 2), (3, 2) };
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 6, 7, 8 },
{ 5, 5, 5 },
{ 7, 5, 6 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (2, 1), (2, 2), (2, 3) };
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 8, 7, 9 },
{ 6, 7, 6 },
{ 3, 2, 5 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (3, 3) };
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 3, 1, 3 },
{ 3, 2, 4 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (1, 1), (1, 3) };
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 2 },
{ 1 },
{ 4 },
{ 1 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (2, 1), (4, 1) };
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var matrix = new[,]
{
{ 2, 5, 3, 5 }
};
var actual = SaddlePoints.Calculate(matrix);
var expected = new[] { (1, 2), (1, 4) };
Assert.Equal(expected, actual);
}
}
using System.Collections.Generic;

public static class SaddlePoints
{
public static IEnumerable<(int, int)> Calculate(int[,] matrix)
{
var result = new List<(int, int)>();

for (int i = 0; i < matrix.GetLength(0); i++)
{
int max = matrix[i, 0];
var indexs = new List<int> { 0 };
for (int j = 1; j < matrix.GetLength(1); j++)
{
if (matrix[i, j] > max)
{
max = matrix[i, j];
indexs = new List<int> { j };
}
else if (matrix[i, j] == max)
{
}
}

foreach (var idx in indexs)
{
bool isMin = true;
for (int k = 0; k < matrix.GetLength(0); k++)
{
if (matrix[k, idx] < matrix[i, idx])
{
isMin = false;
break;
}
}

if (isMin)
{
result.Add((i + 1, idx + 1));
}
}
}

return result;
}
}