Janilabo

asd

Aug 2nd, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.10 KB | None | 0 0
  1. function TIAMode(TIA: TIntegerArray): Integer;
  2. var
  3.   mn, mx, x, y, t: Integer;
  4.   b: TIntegerArray;
  5. begin
  6.   y := High(TIA);
  7.   if (y > -1) then
  8.   begin
  9.     mn := TIA[0];
  10.     mx := TIA[0];
  11.     if (y > 0) then
  12.     for x := 1 to y do
  13.       if (TIA[x] < mn) then
  14.         mn := TIA[x]
  15.       else
  16.         if (TIA[x] > mx) then
  17.           mx := TIA[x];
  18.     {$IFNDEF Lape}
  19.       SetLength(b, (IAbs(mn - mx) + 1));
  20.     {$ELSE}
  21.       SetLength(b, (Abs(mn - mx) + 1));
  22.     {$ENDIF}
  23.     for x := 0 to (mx - mn) do
  24.       b[x] := 0;
  25.     for x := 0 to y do
  26.       Inc(b[(TIA[x] - mn)]);
  27.     t := 0;
  28.     if ((mx - mn) > 0) then
  29.     for x := 0 to (mx - mn) do
  30.       if (b[x] > b[t]) then
  31.         t := x;
  32.     Result := (t + mn);
  33.     SetLength(b, 0);
  34.   end else
  35.     Result := -1;
  36. end;
  37.  
  38. function TIAValueCounts(TIA: TIntegerArray): T2DIntegerArray;
  39. var
  40.   mn, mx, x, y, t, r: Integer;
  41.   b: TIntegerArray;
  42. begin
  43.   y := High(TIA);
  44.   r := 0;
  45.   if (y > -1) then
  46.   begin
  47.     SetLength(Result, (y + 1));
  48.     mn := TIA[0];
  49.     mx := TIA[0];
  50.     if (y > 0) then
  51.     for x := 1 to y do
  52.       if (TIA[x] < mn) then
  53.         mn := TIA[x]
  54.       else
  55.         if (TIA[x] > mx) then
  56.           mx := TIA[x];
  57.     {$IFNDEF Lape}
  58.       SetLength(b, (IAbs(mn - mx) + 1));
  59.     {$ELSE}
  60.       SetLength(b, (Abs(mn - mx) + 1));
  61.     {$ENDIF}
  62.     for x := 0 to (mx - mn) do
  63.       b[x] := 0;
  64.     for x := 0 to y do
  65.       Inc(b[(TIA[x] - mn)]);
  66.     for x := 0 to (mx - mn) do
  67.       if (b[x] > 0) then
  68.       begin
  69.         SetLength(Result[r], 2);
  70.         Result[r][0] := (x + mn);
  71.         Result[r][1] := b[x];
  72.         Inc(r);
  73.       end;
  74.     SetLength(b, 0);
  75.   end;
  76.   SetLength(Result, r);
  77. end;
  78.  
  79. function TIAValuePositions(TIA: TIntegerArray): T2DIntegerArray;
  80. var
  81.   mn, mx, x, y, l, r: Integer;
  82.   b: T2DIntegerArray;
  83. begin
  84.   y := High(TIA);
  85.   r := 0;
  86.   if (y > -1) then
  87.   begin
  88.     SetLength(Result, (y + 1));
  89.     mn := TIA[0];
  90.     mx := TIA[0];
  91.     if (y > 0) then
  92.     for x := 1 to y do
  93.       if (TIA[x] < mn) then
  94.         mn := TIA[x]
  95.       else
  96.         if (TIA[x] > mx) then
  97.           mx := TIA[x];
  98.     {$IFNDEF Lape}
  99.       SetLength(b, (IAbs(mn - mx) + 1));
  100.     {$ELSE}
  101.       SetLength(b, (Abs(mn - mx) + 1));
  102.     {$ENDIF}
  103.     for x := 0 to y do
  104.     begin
  105.       l := Length(b[(TIA[x] - mn)]);
  106.       SetLength(b[(TIA[x] - mn)], (l + 1));
  107.       b[(TIA[x] - mn)][l] := x;
  108.     end;
  109.     for x := 0 to (mx - mn) do
  110.     begin
  111.       l := Length(b[x]);
  112.       if (l > 0) then
  113.       begin
  114.         SetLength(Result[r], (l + 1));
  115.         Result[r][0] := (x + mn);
  116.         for y := 0 to (l - 1) do
  117.           Result[r][(y + 1)] := b[x][y];
  118.         Inc(r);
  119.       end;
  120.     end;
  121.     SetLength(b, 0);
  122.   end;
  123.   SetLength(Result, r);
  124. end;
  125.  
  126. var
  127.   TIA: TIntegerArray;
  128.  
  129. begin
  130.   ClearDebug;
  131.   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];
  132.   WriteLn('MOST COMMON VALUE: ' + #13#10 + ToStr(TIAMode(TIA)));
  133.   WriteLn('COUNTS: ' + #13#10 + ToStr(TIAValueCounts(TIA)));
  134.   WriteLn('POSITIONS: ' + #13#10 + ToStr(TIAValuePositions(TIA)));
  135. end.
Advertisement
Add Comment
Please, Sign In to add comment