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

rpottsoh's solution

to Saddle Points in the Delphi Pascal Track

Published at Aug 27 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Detect saddle points in a matrix.

So say you have a matrix like so:

    0  1  2
  |---------
0 | 9  8  7
1 | 5  3  2     <--- saddle point at (1,0)
2 | 6  6  7

It has a saddle point at (1, 0).

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.

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 can see how others have completed the exercise.

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.
//
// For more guidance as you work on this exercise, see
// GETTING_STARTED.md.
//******************************************************************

unit uSaddlePointsTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.1.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;

    [Test]
    [Ignore]
    procedure Can_identify_saddle_point_in_bottom_right_corner;
  end;

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

implementation
uses uSaddlePoints;

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

procedure TSaddlePointTests.Can_identify_single_saddle_point;
var SaddlePoints: ISaddlePoints;
    values: TArray<TArray<integer>>;
    expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, 3, 3);
  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(1,0);
  Assert.AreEqual(expected,SaddlePoints.Calculate);
end;

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

  SaddlePoints := newSaddlePoints(values);

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

procedure TSaddlePointTests.Can_identify_lack_of_saddle_points_when_there_are_none;
var SaddlePoints: ISaddlePoints;
    values: TArray<TArray<integer>>;
    expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, 3, 3);
  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;
var SaddlePoints: ISaddlePoints;
    values: TArray<TArray<integer>>;
    expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, 3, 3);
  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(0,1);
  expected[1] := TTuple<integer,integer>.Create(1,1);
  expected[2] := TTuple<integer,integer>.Create(2,1);
  Assert.AreEqual(expected,SaddlePoints.Calculate);
end;

procedure TSaddlePointTests.Can_identify_saddle_point_in_bottom_right_corner;
var SaddlePoints: ISaddlePoints;
    values: TArray<TArray<integer>>;
    expected: TArray<TTuple<integer,integer>>;
begin
  SetLength(values, 3, 3);
  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(2,2);
  Assert.AreEqual(expected,SaddlePoints.Calculate);
end;

initialization
  TDUnitX.RegisterTestFixture(TSaddlePointTests);
end.
//========================================================================
// For this example solution I utilized Stefen Glienke's
// open-source code library titled Spring4D.  Latest
// version is available here: https://bitbucket.org/sglienke/spring4d
// Spring4D is not absolutely necessary, but it does greatly help
// facilitate a much simpler solution.
//
// Spring4D does include a Tuple record type that I have purposely
// not used in favor of my own, simple, TTuple that I declared in the
// test runner, in order to remove Spring4D dependency for test execution
//========================================================================
unit uSaddlePoints;

interface
uses uSaddlePointsTests;

type
  ISaddlePoints = interface(IInvokable)
  ['{8C41B1A2-335D-4C6D-AF55-9F307F358227}']
    function Calculate: TArray<TTuple<integer, integer>>;
  end;

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

implementation
uses SysUtils, Spring.Collections;

type
  TSaddlePoints = class(TInterfacedObject, ISaddlePoints)
  private
    fValues: TArray<TArray<integer>>;
    fmaxRows: TArray<integer>;
    fminCols: TArray<integer>;
    function getMaxRows: TArray<integer>;
    function getMinColumns: TArray<integer>;
    function Coordinates: IList<TTuple<integer, integer>>;
    function ColumnCount: integer;
    function RowCount: integer;
    function IsSaddlePoint(const coordinate: TTuple<integer, integer>): boolean;
  public
    constructor create(aValues: TArray<TArray<integer>>);
    function Calculate: TArray<TTuple<integer, integer>>;
  end;

function newSaddlePoints(aValues: TArray<TArray<integer>>): ISaddlePoints;
begin
  result := TSaddlePoints.create(aValues);
end;

constructor TSaddlePoints.create(aValues: TArray<TArray<Integer>>);
begin
  fValues := copy(aValues);
  fmaxRows := getMaxRows;
  fminCols := getMinColumns;
end;

function TSaddlePoints.getMaxRows: TArray<integer>;
var
  Row: integer;
  Col: integer;
  lRow: IList<integer>;
begin
  lRow := TCollections.CreateList<integer>;
  SetLength(result, RowCount);
  for Row := 0 to RowCount - 1 do
  begin
    for Col := 0 to ColumnCount - 1 do
      lRow.Add(fValues[Row,Col]);
    result[Row] := lRow.Max;
    lRow.Clear;
  end;
end;

function TSaddlePoints.getMinColumns: TArray<integer>;
var
  Col: integer;
  Row: integer;
  lColumn: IList<integer>;
begin
  lColumn := TCollections.CreateList<integer>;
  SetLength(result, ColumnCount);
  for Col := 0 to ColumnCount - 1 do
  begin
    for Row := 0 to RowCount - 1 do
      lColumn.Add(fValues[Row,Col]);
    result[Col] := lColumn.Min;
    lColumn.Clear;
  end;
end;

function TSaddlePoints.Coordinates: IList<TTuple<integer, integer>>;
var
  Row, Col: integer;
begin
  result := TCollections.CreateList<TTuple<integer, integer>>;
  for Row := 0 to RowCount - 1 do
    for Col := 0 to ColumnCount - 1 do
      result.Add(TTuple<integer, integer>.Create(Row,Col));
end;

function TSaddlePoints.Calculate: TArray<TTuple<integer, integer>>;
begin
  result := Coordinates.Where(IsSaddlePoint).ToArray;
end;

function TSaddlePoints.ColumnCount: integer;
begin
  if RowCount > 0 then
    result := length(fValues[0])
  else
    result := 0;
end;

function TSaddlePoints.RowCount: integer;
begin
  result := length(fValues);
end;

function TSaddlePoints.IsSaddlePoint(const coordinate: TTuple<integer, integer>): boolean;
var
  row, col: integer;
begin
  row := coordinate.Value1;
  col := coordinate.Value2;
  result := (fmaxRows[row] = fValues[row, col]) and
            (fminCols[col] = fValues[row, col]);
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?