Averages/Mode: Difference between revisions
→{{header|Lua}}: Variable names are more accurate; returns the count in addition to the mode, example uses chracters, more succinct
m (→version 1: added/changed comments, indentations, and whitespace.) |
(→{{header|Lua}}: Variable names are more accurate; returns the count in addition to the mode, example uses chracters, more succinct) |
||
Line 1,436:
=={{header|Lua}}==
<lang lua>function mode
for _, val in pairs(tbl) do
-- see http://lua-users.org/wiki/TernaryOperator
local modeValue = 0▼
counts[val] = counts[val] and counts[val] + 1 or 1
local modes = { }
for key, val in pairs(counts) do
if val > modeCount then
modeCount = val
modes = {key}
elseif val == modeCount then
table.insert(modes, key)
end
end
return modes, modeCount
▲ end
end
print("occur(s) ", count, " times")
result = mode({1, 1, 2, 4, 4})▼
▲ for i,v in next,result do io.write(v..' ') end
for _, val in pairs(modes) do io.write(val..' ') end
print("occur(s) ", count, " times")
=={{header|Maple}}==
|