Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TIAMode(TIA: TIntegerArray): Integer;
- var
- mn, mx, x, y, t: Integer;
- b: TIntegerArray;
- begin
- y := High(TIA);
- if (y > -1) then
- begin
- mn := TIA[0];
- mx := TIA[0];
- if (y > 0) then
- for x := 1 to y do
- if (TIA[x] < mn) then
- mn := TIA[x]
- else
- if (TIA[x] > mx) then
- mx := TIA[x];
- {$IFNDEF Lape}
- SetLength(b, (IAbs(mn - mx) + 1));
- {$ELSE}
- SetLength(b, (Abs(mn - mx) + 1));
- {$ENDIF}
- for x := 0 to (mx - mn) do
- b[x] := 0;
- for x := 0 to y do
- Inc(b[(TIA[x] - mn)]);
- t := 0;
- if ((mx - mn) > 0) then
- for x := 0 to (mx - mn) do
- if (b[x] > b[t]) then
- t := x;
- Result := (t + mn);
- SetLength(b, 0);
- end else
- Result := -1;
- end;
- function TIAValueCounts(TIA: TIntegerArray): T2DIntegerArray;
- var
- mn, mx, x, y, t, r: Integer;
- b: TIntegerArray;
- begin
- y := High(TIA);
- r := 0;
- if (y > -1) then
- begin
- SetLength(Result, (y + 1));
- mn := TIA[0];
- mx := TIA[0];
- if (y > 0) then
- for x := 1 to y do
- if (TIA[x] < mn) then
- mn := TIA[x]
- else
- if (TIA[x] > mx) then
- mx := TIA[x];
- {$IFNDEF Lape}
- SetLength(b, (IAbs(mn - mx) + 1));
- {$ELSE}
- SetLength(b, (Abs(mn - mx) + 1));
- {$ENDIF}
- for x := 0 to (mx - mn) do
- b[x] := 0;
- for x := 0 to y do
- Inc(b[(TIA[x] - mn)]);
- for x := 0 to (mx - mn) do
- if (b[x] > 0) then
- begin
- SetLength(Result[r], 2);
- Result[r][0] := (x + mn);
- Result[r][1] := b[x];
- Inc(r);
- end;
- SetLength(b, 0);
- end;
- SetLength(Result, r);
- end;
- function TIAValuePositions(TIA: TIntegerArray): T2DIntegerArray;
- var
- mn, mx, x, y, l, r: Integer;
- b: T2DIntegerArray;
- begin
- y := High(TIA);
- r := 0;
- if (y > -1) then
- begin
- SetLength(Result, (y + 1));
- mn := TIA[0];
- mx := TIA[0];
- if (y > 0) then
- for x := 1 to y do
- if (TIA[x] < mn) then
- mn := TIA[x]
- else
- if (TIA[x] > mx) then
- mx := TIA[x];
- {$IFNDEF Lape}
- SetLength(b, (IAbs(mn - mx) + 1));
- {$ELSE}
- SetLength(b, (Abs(mn - mx) + 1));
- {$ENDIF}
- for x := 0 to y do
- begin
- l := Length(b[(TIA[x] - mn)]);
- SetLength(b[(TIA[x] - mn)], (l + 1));
- b[(TIA[x] - mn)][l] := x;
- end;
- for x := 0 to (mx - mn) do
- begin
- l := Length(b[x]);
- if (l > 0) then
- begin
- SetLength(Result[r], (l + 1));
- Result[r][0] := (x + mn);
- for y := 0 to (l - 1) do
- Result[r][(y + 1)] := b[x][y];
- Inc(r);
- end;
- end;
- SetLength(b, 0);
- end;
- SetLength(Result, r);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [-5, -1, -1, 3, 3, 5, 5, 3, 3, 5, 5, 5, 5, 5, -1, 1, -1, -6, 2, 3, -1, -9, 3, -1, 4, -1, 5, 3, 3, 3, -1, 5, -1];
- WriteLn('MOST COMMON VALUE: ' + #13#10 + ToStr(TIAMode(TIA)));
- WriteLn('COUNTS: ' + #13#10 + ToStr(TIAValueCounts(TIA)));
- WriteLn('POSITIONS: ' + #13#10 + ToStr(TIAValuePositions(TIA)));
- end.
Advertisement
Add Comment
Please, Sign In to add comment