Computer Science Canada

Mode of chracters

Author:  eNc [ Sun Feb 06, 2005 12:45 pm ]
Post subject:  Mode of chracters

Hi, umm i was wondering how i would go about finding the mode of chracters in string

ie -- INPUT = 139523442133
code:

for  i  : 1..length(InputString)
        do something to find the mode (most freqent)
        character
        put mode
        %this would output 3
end for

Author:  TheZsterBunny [ Sun Feb 06, 2005 4:36 pm ]
Post subject: 

hrm. it would make for difficulty if you were using more than one digit numbers. however;

code:

var numstring := "139523442133"
var numcount : array 0 .. 9 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for i : 1 .. length (numstring)
    numcount (strint (numstring (i))) += 1
end for
var mode, modemax : int := 0
for i : 0 .. 9
    if numcount (i) > modemax then
        modemax := numcount (i)
        mode := i
    end if
end for
put "The number ", mode, " occurred ", modemax, " times in ", numstring, "."

would give you the mode for single-digit numbers

-Z

lol. while writing the code, i kept having to delete the java statements and replace them with turing.


: