Averages/Mode: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
(RPL: add section) |
||
Line 3,010: | Line 3,010: | ||
mode(s) of b() = |
mode(s) of b() = |
||
1 4 |
1 4 |
||
</pre> |
|||
=={{header|RPL}}== |
|||
∑VAL and ∑CNT are global lists used to store rep. values and counters. |
|||
Using the stack to handle potentially big amounts of data would slow down execution. |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ |
|||
'''IF ∑VAL''' OVER POS |
|||
'''THEN '∑CNT'''' LAST ∑'''CNT''' OVER GET 1 + PUT DROP |
|||
'''ELSE ∑VAL''' SWAP + ''''∑VAL'''' STO '''∑CNT''' 1 + ''''∑CNT'''' STO '''END''' |
|||
≫ ''''∑ADD'''' STO |
|||
≪ → data |
|||
≪ { } DUP ''''∑VAL'''' STO ''''∑CNT'''' STO |
|||
1 data SIZE '''FOR''' j |
|||
data j GET '''∑ADD NEXT''' |
|||
'''∑CNT''' LIST→ { } + →ARRY RNRM { } |
|||
1 '''∑VAL''' SIZE '''FOR''' j |
|||
'''IF''' OVER ''''∑CNT'''' j GET == |
|||
'''THEN '∑VAL'''' j GET |
|||
'''IF''' DUP2 POS NOT '''THEN''' + '''END''' |
|||
'''END NEXT''' |
|||
≫ ''''MODE'''' STO |
|||
| |
|||
'''∑ADD''' ''( n -- )'' // update ∑VAL and ∑CNT |
|||
if n already in ∑VAL |
|||
then increment corresponding position in ∑CNT |
|||
else create new in ∑VAL / ∑CNT entries |
|||
'''∑ADD''' ''( { data } -- { modal value(s) } )'' |
|||
initialize ∑VAL and ∑CNT |
|||
for all input values |
|||
count occurrences |
|||
get max value in ∑CNT |
|||
for all distinct values |
|||
if count = max |
|||
then get value |
|||
and add it to output list if not already in |
|||
|} |
|||
{ 1 3 6 6 6 6 7 7 12 12 17 } '''MODE''' |
|||
{ 1 1 2 4 4 } '''MODE''' |
|||
{{out}} |
|||
<pre> |
|||
2: { 6 } |
|||
1: { 1 4 } |
|||
</pre> |
</pre> |
||