SRL Archive - OSR Help

Finding mode in an array


purerange86

2014-12-30 13:31:52

How might I go about finding the mode in an array? I'm trying to find the most common color in an area, say the minimap.

Thanks :)

Turpinator

2014-12-30 13:35:03

You would write a mode finding function…

(i have one somewhere, let me find it)
function mode(arr: TIntegerArray): integer;
var
dif: TIntegerArray;
dcount, maxloc, finalloc, L: Integer;
begin
SetLength(dif, length(arr));
for L := 0 to length(arr) - 1 do
dif[L] := 1;
QuickSort(arr);
dcount := 0;
for L := 0 to length(arr) - 2 do
if arr[L] = arr[L + 1] then
inc(dif[dcount])
else
inc(dcount);
maxloc := 0;
for L := 0 to length(dif) - 1 do
if (dif[L] > dif[maxloc]) then
maxloc := L;
finalloc := 0;
for L := 0 to maxloc - 1 do
finalloc := finalloc + dif[L];
result := arr[finalloc];
end;

writeln(mode([6,6,4,7,2,54,65,3,4,6,5,4,6]));
returns... 6

purerange86

2014-12-30 13:45:27

Ah yes! Thank you so much, I started writing it and got stuck, being unfamiliar with array functions and what to do with them.

The Mayor

2014-12-30 19:30:30

From: Turpinator
You would write a mode finding function…

(i have one somewhere, let me find it)


From: purerange86
Ah yes! Thank you so much, I started writing it and got stuck, being unfamiliar with array functions and what to do with them.


I've got an easier way


program template;
{$I SRL-6/SRL.Simba}

function mode(numbers: TIntegerArray): Integer;
begin
result := numbers.getMostCommon();
end

begin
writeLn(mode([1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 10, 11, 11, 13]));
end.

E: NVM just saw this was for OSR, so you could just use the original function:


function mode(numbers: TIntegerArray): Integer;
var
i, cur, count, highCount: longInt;
begin
quickSort(numbers);

cur := numbers[0] - 1;
count := 0;
highCount := 0;

for i := 0 to high(numbers) do
begin
if (cur <> numbers[I]) then
begin
if (count > highCount) then
begin
highCount := count;
result := cur;
end;

count := 0;
cur := numbers[I];
end;

Inc(Count);
end;
end;

begin
writeLn(mode([1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 10, 11, 11, 13]));
end.

slacky

2014-12-30 20:56:12

From: The Mayor
…
E: NVM just saw this was for OSR, so you could just use the original function:


function mode(numbers: TIntegerArray): Integer;
var
i, cur, count, highCount: longInt;
begin
quickSort(numbers);

cur := numbers[0] - 1;
count := 0;
highCount := 0;

for i := 0 to high(numbers) do
begin
if (cur <> numbers[I]) then
begin
if (count > highCount) then
begin
highCount := count;
result := cur;
end;

count := 0;
cur := numbers[I];
end;

Inc(Count);
end;
end;

begin
writeLn(mode([1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 10, 11, 11, 13]));
end.

Misses handling when the mode is the largest value (last in the array after sorting). So you need to handle that:

function mode(numbers: TIntegerArray): Integer;
var
i, cur, count, highCount: longInt;
begin
quickSort(numbers);

cur := numbers[0] - 1;
count := 0;
highCount := 0;

for i := 0 to high(numbers) do
begin
if (cur <> numbers[I]) then
begin
if (count > highCount) then
begin
highCount := count;
result := cur;
end;

count := 0;
cur := numbers[I];
end;
Inc(Count);
end;
if (count > highCount) then
Result := cur;
end;

begin
WriteLn(mode([5,5,5,1,3,4]));
end.

Turpinator

2014-12-31 01:43:17

From: The Mayor
I've got an easier way


program template;
{$I SRL-6/SRL.Simba}

function mode(numbers: TIntegerArray): Integer;
begin
result := numbers.getMostCommon();
end

begin
writeLn(mode([1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 10, 11, 11, 13]));
end.

E: NVM just saw this was for OSR, so you could just use the original function:


function mode(numbers: TIntegerArray): Integer;
var
i, cur, count, highCount: longInt;
begin
quickSort(numbers);

cur := numbers[0] - 1;
count := 0;
highCount := 0;

for i := 0 to high(numbers) do
begin
if (cur <> numbers[I]) then
begin
if (count > highCount) then
begin
highCount := count;
result := cur;
end;

count := 0;
cur := numbers[I];
end;

Inc(Count);
end;
end;

begin
writeLn(mode([1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 10, 11, 11, 13]));
end.


Well… i didnt realize that was in SRL-6. at the time i wrote that for @elfyyy;'s pest control script (it seems to have more than it did initially, but idk. that was like 6+ months ago and i cant find the original code.
that code was also one i dreamt up by myself, so i didnt pussy out and copy the pseudo code. :D