-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.lua
More file actions
69 lines (69 loc) · 1.38 KB
/
arithmetic.lua
File metadata and controls
69 lines (69 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
local arith = {}
function arith.clamp(x,min,max)
if x > max then
x = max
end
if x < min then
x = min
end
end
function arith.mean(table)
assert(type(table)=="table","\n\n\nInvalid Type\nArgument must be table\n\n")
local val = #table
local val2 = 0
for i = 1,val do
val2 = val2 + table[i]
end
return val2/val
end
function arith.mode( t )
local counts={}
for k, v in pairs( t ) do
if counts[v] == nil then
counts[v] = 1
else
counts[v] = counts[v] + 1
end
end
local biggestCount = 0
for k, v in pairs( counts ) do
if v > biggestCount then
biggestCount = v
end
end
local temp={}
for k,v in pairs( counts ) do
if v == biggestCount then
table.insert( temp, k )
end
end
return temp
end
function arith.median( t )
local temp={}
for k,v in pairs(t) do
if type(v) == 'number' then
table.insert( temp, v )
end
end
table.sort( temp )
if math.fmod(#temp,2) == 0 then
return ( temp[#temp/2] + temp[(#temp/2)+1] ) / 2
else
return temp[math.ceil(#temp/2)]
end
end
function arith.range(arg)
assert(type(table)=="table","\n\n\nInvalid Type\nArgument must be table\n\n")
table.sort(arg)
return arg[#arg] - arg[1]
end
function arith.factor(n)
assert(type(n)=="number","\n\n\nInvalid Type\nArgument must be a number")
if (n == 0) then
return 1
else
return n * arith.factor(n - 1)
end
end
return arith