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 (numlisttbl) -- returns table of modes and count
if assert(type(numlisttbl) ~== 'table' then return numlist end)
local setscounts = { }
for _, val in pairs(tbl) do
local mode
-- see http://lua-users.org/wiki/TernaryOperator
local modeValue = 0
counts[val] = counts[val] and counts[val] + 1 or 1
table.foreach(numlist,function(i,v) if sets[v] then sets[v] = sets[v] + 1 else sets[v] = 1 end end)
end
for i,v in next,sets do
local modes = { }
if v > modeValue then
local modeValuemodeCount = 0
modeValue = v
for key, val in pairs(counts) do
mode = i
if val > modeCount then
else
modeCount = val
if v == modeValue then
modes = {key}
if type(mode) == 'table' then
elseif val == modeCount then
table.insert(mode,i)
table.insert(modes, key)
else
end
mode = {mode,i}
end
end
return modes, modeCount
end
end
end
return mode
end
 
resultmodes, count = mode({1,3,6,6,6,6,7,7,12,12,17})
for i_,v val in next,resultpairs(modes) do io.write(vval..' ') end
print(result)
print("occur(s) ", count, " times")
result = mode({1, 1, 2, 4, 4})
 
if type(result) == 'table' then
resultmodes, count = mode({1'a', 1'a', 2'b', 4'd', 4'd'})
for i,v in next,result do io.write(v..' ') end
for _, val in pairs(modes) do io.write(val..' ') end
print ()
print("occur(s) ", count, " times")
end</lang>
 
=={{header|Maple}}==
31

edits