🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of dgeiger

dgeiger's solution

to Saddle Points in the Delphi Pascal Track

Published at Sep 02 2020 · 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.

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

Source

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may request help from a mentor.

uSaddlePointsTests.pas

//******************************************************************
// Hint: You will be using TTuple in your solution.  TTuple is
// declared in this unit.  It will be necessary for you to add
// a uses statement in the interface section of uSaddlePoints.pas.
//******************************************************************

unit uSaddlePointsTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.5.0';

type

  [TestFixture]
  TSaddlePointTests = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure Can_identify_single_saddle_point;

    [Test]
    [Ignore]
    procedure Can_identify_that_empty_matrix_has_no_saddle_points;

    [Test]
    [Ignore]
    procedure Can_identify_lack_of_saddle_points_when_there_are_none;

    [Test]
    [Ignore]
    procedure Can_identify_multiple_saddle_points_in_a_column;

    [Test]
    [Ignore]
    procedure Can_identify_multiple_saddle_points_in_a_row;

    [Test]
    [Ignore]
    procedure Can_identify_saddle_point_in_bottom_right_corner;

    [Test]
    [Ignore]
    procedure Can_identify_saddle_points_in_a_non_square_matrix;

    [Test]
    [Ignore]
    procedure Can_identify_that_saddle_points_in_a_single_column_matrix_are_those_with_the_minimum_value;

    [Test]
    [Ignore]
    procedure Can_identify_that_saddle_points_in_a_single_row_matrix_are_those_with_the_maximum_value;
  end;

  TTuple<T1, T2> = record
  private
    fValue1: T1;
    fValue2: T2;
  public
    constructor Create(aValue1: T1; aValue2: T2);
    property Value1 : T1 read fValue1;
    property Value2 : T2 read fValue2;
  end;

implementation
uses uSaddlePoints;

constructor TTuple<T1, T2>.Create(aValue1: T1; aValue2: T2);
begin
  fValue1 := aValue1;
  fValue2 := aValue2;
end;

procedure TSaddlePointTests.Can_identify_single_saddle_point;
const
  rows = 3;
  columns = 3;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 9; values[0,1] := 8; values[0,2] := 7;
  values[1,0] := 5; values[1,1] := 3; values[1,2] := 2;
  values[2,0] := 6; values[2,1] := 6; values[2,2] := 7;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 1);
  expected[0] := TTuple<integer,integer>.Create(2,1);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_that_empty_matrix_has_no_saddle_points;
const
  rows = 0;
  columns = 0;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 0);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_that_saddle_points_in_a_single_column_matrix_are_those_with_the_minimum_value;
const
  rows = 4;
  columns = 1;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 2;
  values[1,0] := 1;
  values[2,0] := 4;
  values[3,0] := 1;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 2);
  expected[0] := TTuple<integer,integer>.Create(2,1);
  expected[1] := TTuple<integer,integer>.Create(4,1);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_that_saddle_points_in_a_single_row_matrix_are_those_with_the_maximum_value;
const
  rows = 1;
  columns = 4;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 2; values[0,1] := 5; values[0,2] := 3; values[0,3] := 5;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 2);
  expected[0] := TTuple<integer,integer>.Create(1,2);
  expected[1] := TTuple<integer,integer>.Create(1,4);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_lack_of_saddle_points_when_there_are_none;
const
  rows = 3;
  columns = 3;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 1; values[0,1] := 2; values[0,2] := 3;
  values[1,0] := 3; values[1,1] := 1; values[1,2] := 2;
  values[2,0] := 2; values[2,1] := 3; values[2,2] := 1;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 0);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_multiple_saddle_points_in_a_column;
const
  rows = 3;
  columns = 3;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 4; values[0,1] := 5; values[0,2] := 4;
  values[1,0] := 3; values[1,1] := 5; values[1,2] := 5;
  values[2,0] := 1; values[2,1] := 5; values[2,2] := 4;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 3);
  expected[0] := TTuple<integer,integer>.Create(1,2);
  expected[1] := TTuple<integer,integer>.Create(2,2);
  expected[2] := TTuple<integer,integer>.Create(3,2);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_multiple_saddle_points_in_a_row;
const
  rows = 3;
  columns = 3;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 6; values[0,1] := 7; values[0,2] := 8;
  values[1,0] := 5; values[1,1] := 5; values[1,2] := 5;
  values[2,0] := 7; values[2,1] := 5; values[2,2] := 6;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 3);
  expected[0] := TTuple<integer,integer>.Create(2,1);
  expected[1] := TTuple<integer,integer>.Create(2,2);
  expected[2] := TTuple<integer,integer>.Create(2,3);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_saddle_points_in_a_non_square_matrix;
const
  rows = 2;
  columns = 3;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 3; values[0,1] := 1; values[0,2] := 3;
  values[1,0] := 3; values[1,1] := 2; values[1,2] := 4;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 2);
  expected[0] := TTuple<integer,integer>.Create(1,1);
  expected[1] := TTuple<integer,integer>.Create(1,3);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_saddle_point_in_bottom_right_corner;
const
  rows = 3;
  columns = 3;
var
  SaddlePoints: ISaddlePoints;
  values: TArray<TArray<integer>>;
  expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, rows, columns);
  values[0,0] := 8; values[0,1] := 7; values[0,2] := 9;
  values[1,0] := 6; values[1,1] := 7; values[1,2] := 6;
  values[2,0] := 3; values[2,1] := 2; values[2,2] := 5;

  SaddlePoints := newSaddlePoints(values);

  SetLength(expected, 1);
  expected[0] := TTuple<integer,integer>.Create(3,3);
  Assert.AreEqual(expected, SaddlePoints.Calculate);
end;

initialization
  TDUnitX.RegisterTestFixture(TSaddlePointTests);
end.
unit uSaddlePoints;

interface

uses
  uSaddlePointsTests;

type
  Matrix = TArray<TArray<integer>>;

  PointsArray = TArray<TTuple<integer,integer>>;

  ISaddlePoints = interface['{4E8A018B-6A4E-4B26-898A-CBB313FEEE98}']
      function Calculate: TArray<TTuple<integer,integer>>;

  end;

  TSaddlePoints = class(TInterfacedObject, ISaddlePoints)
    private
      FMatrix: Matrix;
      FRow: Integer;
      FRows: Integer;
      FColumn: Integer;
      FColumns: Integer;

      function RowMax: Integer;
      function ColumnMin: Integer;

    public
      constructor Create(values: TArray<TArray<integer>>);

      function Calculate: PointsArray;

  end;

  function newSaddlePoints(values: TArray<TArray<integer>>): ISaddlePoints;

implementation

function newSaddlePoints(values: TArray<TArray<integer>>): ISaddlePoints;
begin
  // Here, we just create a new TSaddlePoints object and return it's interface.
  Result := TSaddlePoints.Create(Values);
end;

{ TSaddlePoints }

function TSaddlePoints.RowMax: Integer;
var
  Col: Integer;
begin
  // Since we know that this is a valid array, and that this guarantees at
  // least one element, we set the Result to the value of that element.
  Result := FMatrix[FRow, 0];

  // Now, we scan the rest of the array (if any)
  for Col := 1 to FColumns - 1 do
    // Is this element the new maximum?
    if FMatrix[FRow, Col] > Result then
      // Update the result
      Result := FMatrix[FRow, Col];
end;

function TSaddlePoints.ColumnMin: Integer;
var
  Row: Integer;
begin
  // Since we know that this is a valid array, and that this guarantees at

  // least one element, we set the Result to the value of that element.
  Result := FMatrix[0, FColumn];

  // Now, we scan the rest of the array (if any)
  for Row := 1 to FRows - 1 do
    // Is this element the new minimum?
    if FMatrix[Row, FColumn] < Result then
      // Update the result
      Result := FMatrix[Row, FColumn];
end;

function TSaddlePoints.Calculate: PointsArray;
var
  Saddles: PointsArray;
  Min: Integer;
  Max: Integer;
  Row: Integer;
  Column: Integer;
  SaddleCount: Integer;
  TestValue: Integer;
begin
  // First, we'll create a new PointsArray, and set the count of
  // saddles points found to zero
  Saddles := PointsArray.Create();
  SetLength(Saddles, 0);
  SaddleCount := 0;

  // We need to scan each row
  for Row := 0 to FRows - 1 do
    begin
      // Update the FRow field, so we don't have to keep passing the row.
      FRow := Row;

      // Get the maximum value for this row
      Max := RowMax;

      // Now, we need to scan each column in this row
      for Column := 0 to FColumns - 1 do
        begin
          // Update the FColumn field, so we don't have to keep passing the row.
          FColumn := Column;

          // Get the minimum value for this column
          Min := ColumnMin;

          // Next, we get the value to test
          TestValue := FMatrix[Row, Column];

          // It's a saddle point only if it's the maximum value in the row
          // and the minimum value in the column
          if (TestValue = Min) and (TestValue = Max) then
            begin
              // Add a new saddle to the PointsArray,
              SetLength(Saddles, SaddleCount + 1);

              // set the points for this saddle,
              Saddles[SaddleCount].Create(Row + 1, Column + 1);

              // and update the count of saddle points.
              Inc(SaddleCount);
            end;
        end;
    end;

  // Finally, we return the PointsArray containing the saddles
  Result := Saddles;
end;

constructor TSaddlePoints.Create(Values: TArray<TArray<integer>>);
begin
  // First, we need to create the Matrix we store the points in
  FMatrix := Values;

  // Next we get the number of rows in the Matrix
  FRows := Length(FMatrix);

  // Are there any rows in the Matrix?
  if FRows = 0 then
    // No, so this means there can be no columns either.
    FColumns := 0
  else
    // Otherwise, get the number of columns in the Matrix
    FColumns := Length(FMatrix[0]);
end;

end.

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?