2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,29 +1,26 @@
|
|||
/*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 11 10 ; 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.*/
|
||||
/*──────────────────────────────────ESORT subroutine────────────────────*/
|
||||
Esort: procedure expose @.; h=@.0 /* [↓] exchange sort. */
|
||||
do while h>1; h=h%2 /*% is integer divide.*/
|
||||
do i=1 for @.0-h; j=i; k=h+i /* [↓] perform exchange*/
|
||||
do while @.k<@.j & h<j; _=@.j; @.j=@.k; @.k=_; 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. */
|
||||
@.0=words(x) /* [↓] make an array from vector.*/
|
||||
do k=1 for @.0; @.k=word(x,k); end /*k*/
|
||||
call Esort @.0 /*sort the elements in the array.*/
|
||||
?=@.1 /*assume 1st element is the mode.*/
|
||||
freq=1 /*the frequency of the occurrence*/
|
||||
do j=1 for @.0; _=j-freq /*traipse through the elements. */
|
||||
if @.j==@._ then do /*this element same as previous? */
|
||||
freq=freq+1 /*bump the frequency counter. */
|
||||
?=@.j /*this element is the mode,so far*/
|
||||
end
|
||||
end /*j*/
|
||||
return ? /*return the node to the invoker.*/
|
||||
/*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 11 10 ; 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 all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sort: procedure expose @.; parse arg # 1 h /* [↓] this is an exchange sort. */
|
||||
do while h>1; h=h%2 /*In REXX, % is an integer divide.*/
|
||||
do i=1 for #-h; j=i; k=h+i /* [↓] perform exchange for elements. */
|
||||
do while @.k<@.j & h<j; _=@.j; @.j=@.k; @.k=_; j=j-h; k=k-h; end
|
||||
end /*i*/
|
||||
end /*while h>1*/; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mode: procedure expose @.; parse arg x; freq=1 /*function finds the MODE of a vector*/
|
||||
#=words(x) /*#: the number of elements in vector.*/
|
||||
do k=1 for #; @.k=word(x,k); end /* ◄──── make an array from the vector.*/
|
||||
call Sort # /*sort the elements in the array. */
|
||||
?=@.1 /*assume the first element is the mode.*/
|
||||
do j=1 for #; _=j-freq /*traipse through the elements in array*/
|
||||
if @.j==@._ then do; freq=freq+1 /*is this element the same as previous?*/
|
||||
?=@.j /*this element is the mode (···so far).*/
|
||||
end
|
||||
end /*j*/
|
||||
return ? /*return the mode of vector to invoker.*/
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
/* Rexx */
|
||||
-- ~~ main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
/*-- ~~ main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
call run_samples
|
||||
return
|
||||
exit
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
-- returns a comma separated string of mode values from a comma separated input vector string
|
||||
/*-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
/*-- 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
|
||||
call makeStem lvector /*-- this call creates the "vector." stem from the input string */
|
||||
seen. = 0
|
||||
modes. = ''
|
||||
modeMax = 0
|
||||
|
|
@ -46,8 +46,8 @@ mode:
|
|||
|
||||
return lmodes
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
-- pretty-print
|
||||
/*-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
/*-- pretty-print */
|
||||
show_mode:
|
||||
procedure
|
||||
parse arg lvector
|
||||
|
|
@ -55,8 +55,8 @@ show_mode:
|
|||
say 'Vector: ['space(lvector, 0)'], Mode(s): ['space(lmodes, 0)']'
|
||||
return modes
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
-- load the "vector." stem from the comma separated input vector string
|
||||
/*-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
/*-- load the "vector." stem from the comma separated input vector string */
|
||||
makeStem:
|
||||
procedure expose vector.
|
||||
vector.0 = 0
|
||||
|
|
@ -69,7 +69,7 @@ makeStem:
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue