This commit is contained in:
Ingy döt Net 2013-04-10 15:42:53 -07:00
parent 051504d65b
commit 0457928c3e
295 changed files with 3588 additions and 3 deletions

View file

@ -0,0 +1,34 @@
/*REXX program finds the mode (most occurring element) of a vector. */
/*────────vector────────── ───show vector─── ────show result────── */
v='1 8 6 0 1 9 4 6 1 9 9 9' ; say 'vector='v; say 'mode='mode(v); say
v='1 2 3 4 5 6 7 8 9 10 11' ; say 'vector='v; say 'mode='mode(v); say
v='8 8 8 2 2 2' ; say 'vector='v; say 'mode='mode(v); say
v='cat kat Cat emu emu Kat' ; say 'vector='v; say 'mode='mode(v); say
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MAKEARRAY subroutine────────────────*/
makeArray: procedure expose @.; parse arg v; @.0=words(v) /*make array*/
do k=1 for @.0; @.k=word(v,k); end /*k*/
return
/*──────────────────────────────────ESORT subroutine────────────────────*/
esort: procedure expose @.; h=@.0 /*exchange sort.*/
do while h>1; h=h%2
do i=1 for @.0-h; j=i; k=h+i
do while @.k<@.j;t=@.j;@.j=@.k;@.k=t;if h>=j then leave;j=j-h;k=k-h;end
end /*i*/
end /*while h>1*/
return
/*──────────────────────────────────MODE subroutine─────────────────────*/
mode: procedure expose @.; parse arg x /*finds the MODE of a vector. */
call makeArray x /*make an array out of the vector*/
call esort @.0 /*sort the array elements. */
?=@.1 /*assume 1st element is the mode.*/
freq=1 /*the frequency of the occurance.*/
do j=1 for @.0; _=j-freq
if @.j==@._ then do
freq=freq+1 /*bump the frequency count. */
?=@.j /*this is the one. */
end
end /*j*/
return ?

View file

@ -0,0 +1,92 @@
/* Rexx */
-- ~~ main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call run_samples
return
exit
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- returns a comma separated string of mode values from a comma separated input vector string
mode:
procedure
parse arg lvector
drop vector.
vector. = ''
call makeStem lvector -- this call creates the "vector." stem from the input string
seen. = 0
modes. = ''
modeMax = 0
do v_ = 1 to vector.0
mv = vector.v_
seen.mv = seen.mv + 1
select
when seen.mv > modeMax then do
modeMax = seen.mv
drop modes.
modes. = ''
nx = 1
modes.0 = nx
modes.nx = mv
end
when seen.mv = modeMax then do
nx = modes.0 + 1
modes.0 = nx
modes.nx = mv
end
otherwise do
nop
end
end
end v_
lmodes = ''
do e_ = 1 to modes.0
lmodes = lmodes modes.e_
end e_
lmodes = strip(space(lmodes, 1, ','))
return lmodes
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- pretty-print
show_mode:
procedure
parse arg lvector
lmodes = mode(lvector)
say 'Vector: ['space(lvector, 0)'], Mode(s): ['space(lmodes, 0)']'
return modes
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- load the "vector." stem from the comma separated input vector string
makeStem:
procedure expose vector.
vector.0 = 0
parse arg lvector
do v_ = 1 while lvector \= ''
parse var lvector val ',' lvector
vector.0 = v_
vector.v_ = strip(val)
vector = strip(lvector)
end v_
return vector.0
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
run_samples:
procedure
call show_mode '10, 9, 8, 7, 6, 5, 4, 3, 2, 1' -- 10 9 8 7 6 5 4 3 2 1
call show_mode '10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0.11' -- 0
call show_mode '30, 10, 20, 30, 40, 50, -100, 4.7, -11e+2' -- 30
call show_mode '30, 10, 20, 30, 40, 50, -100, 4.7, -11e+2, -11e+2' -- 30 -11e2
call show_mode '1, 8, 6, 0, 1, 9, 4, 6, 1, 9, 9, 9' -- 9
call show_mode '1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11' -- 1 2 3 4 5 6 7 8 9 10 11
call show_mode '8, 8, 8, 2, 2, 2' -- 8 2
call show_mode 'cat, kat, Cat, emu, emu, Kat' -- emu
call show_mode '0, 1, 2, 3, 3, 3, 4, 4, 4, 4, 1, 0' -- 4
call show_mode '2, 7, 1, 8, 2' -- 2
call show_mode '2, 7, 1, 8, 2, 8' -- 8 2
call show_mode 'E, n, z, y, k, l, o, p, ä, d, i, e' -- E n z y k l o p ä d i e
call show_mode 'E, n, z, y, k, l, o, p, ä, d, i, e, ä' -- ä
call show_mode '3, 1, 4, 1, 5, 9, 7, 6' -- 1
call show_mode '3, 1, 4, 1, 5, 9, 7, 6, 3' -- 3, 1
call show_mode '1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17' -- 6
call show_mode '1, 1, 2, 4, 4' -- 4 1
return