SRL Archive - FAQ

Simba code equiv. to AMin


Cstrike

2010-08-27 22:25:15

In scar we have:


// SCAR
program MyScript;
{
function AMin(Data: TIntegerArray): Integer;
Returns the smallest integer value of all values in Data.
}
var arr : TIntegerArray;
i : Integer;
begin
arr := [1, 5, 8, 123, 58];
i := AMin (arr);
writeln (inttostr(i));
end.


Is there an equivalent of this in Simba? I tried looking for it through the WizzyPlugin and the math functions, but no success.

I created a function for it:

// SIMBA
program MyScript;
{$i SRL/SRL.scar}

function FindMin (TIntArray : TIntegerArray) : Integer;
var i, j : Integer;
minFound : Boolean;
begin
minFound := false;
for i:=0 to high(TIntArray)do // This finds the mode and returns it's value
begin
//writeln ('ATTEMPTING i = '+inttostr(i));
for j:=0 to high(TIntArray) do
begin
//writeln ('j = '+inttostr(j));
if TIntArray[j] < TIntArray[i] then
begin
//writeln ('Found smaller number, '+inttostr(TIntArray[j])+' < '+ inttostr(TIntArray[i]));
Break;
end;
if (j = high(TIntArray)) and not minFound then
begin
minFound := true;
//writeln ('Minimum: ' + inttostr(TIntArray[i]));
result := TIntArray[i];
end;
end;
if minFound then break;
end;
end;

var arr : TIntegerArray;
k : Integer;
begin
SetupSRL;
cleardebug;
arr := [324, 34994, 3933, 134, 5030304, 3949, 9994, 5934, 111111];
k := FindMin (arr);
writeln (inttostr(k));
end.


But as you can see, if there is a built in Simba function that would save on the code above :)

Is there a function that I'm missing in Simba? I tried using AMin but it doesn't work as expected [or am I expecting wrong? or did I screw up?] If it doesn't exist, I assume I shall have to use the code I made above?
Thanks


EDIT: for those you who want to see if the scar script won't work can test it with this:
program MyScript;
{$i SRL/SRL.scar}

var arr : TIntegerArray;
k : Integer;
begin
arr := [5, 6, 9, 2, 4, 1, 10];
k := AMin (arr);
writeln (inttostr(k));
end;

i luffs yeww

2010-08-27 22:39:25

Here's a smaller AMin function.. :p


function AMin(Numbers: TIntegerArray): Integer;
var
arr: TIntegerArray;
begin
SetArrayLength(arr, Length(Numbers));
arr := Numbers;
QuickSort(arr);
Result := arr[0];
end;

You can use that. There's really no need for it to be in Simba, imo. Simple enough to make, but whatever.

For lulz and what not.


function AMax(Numbers: TIntegerArray): Integer;
var
arr: TIntegerArray;
begin
SetArrayLength(arr, Length(Numbers));
arr := Numbers;
QuickSort(arr);
Result := arr[Length(arr) - 1];
end;

Nava2

2010-08-28 03:11:13

Using QuickSort is so unnecessary.

function AMin(x: TIntegerArray): Integer;
var
i, h, vMin: Integer;
begin
Result := -1;
h := high(x);
if (h > -1) then
begin
Result := h;
vMin := x[h];
end;


for i := h-1 downto 0 do
if (x[i] < vMin) then
begin
Result := i;
vMin := x[i];
end;
end;

Wizzup?

2010-08-28 05:13:03

The QuickSort would probably be faster since it's machine code, while the script code is interpreted. (I think it is probably 1000x times slower)

Can you file a bug report for the functions missing?