-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyfns.lua
More file actions
212 lines (166 loc) · 3.71 KB
/
Copy pathmyfns.lua
File metadata and controls
212 lines (166 loc) · 3.71 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
-- module setup
local M = {}
-- Import Section
local huge=math.huge
local maxi=math.max
local mini=math.min
local sqrt=math.sqrt
local concat=table.concat
local pairs=pairs
local print=print
local next=next
p=require'points'
local slurp=p.slurp
-- Local Variables for module-only access
-- No more external access after this point
_ENV = nil -- or M
-- Function definitions
local function maximal(points)
local max = -huge
local ind = -1
local max2 = -huge
for k,v in pairs(points) do
if v>=max2 then
max2=v
if v>max then
max2=max
max=v
ind=k
end
end
end
return max,ind,max2
end
--local pts={}
--assert(maximal(pts) == -huge,-1,-huge)
--pts={9}
--assert(maximal(pts) == 9,1,-inf)
--pts={9,9}
--assert(maximal(pts) == 9,1,9)
--pts={10,4,6,2,9,1,2,4,5,6,8,2,19}
--assert(maximal(pts) == 19,13,10)
--pts={4,6,2,9,1,2,4,5,6,8,2,9}
--assert(maximal(pts) == 9,4,9)
--pts={10,4,6,2,9,1,2,4,5,6,8,2,19}
--assert(maximal(pts) == 19,13,10)
local function addition(points,max,ind,max2)
local ret = {}
for k,v in pairs(points) do
ret[k] = v + (k~=ind and max or max2)
end
return ret
end
local function positivity(points,except)
local ps = {}
local sum = 0
local sum2 = 0
for k,v in pairs(points) do
local p = k~=except and maxi(v,0) or v
ps[k] = p
sum = sum + p
end
for _,v in pairs(ps) do
sum2 = sum2+v
end
print('Result:',concat(ps,', '))
print('Sums:',sum,sum2)
return ps,sum,sum2
end
local function same(x,y)
local ans = true
local cx,cy = 0,0
--for _ in pairs(y) do
-- cy = cy + 1
--end
--
--for _ in pairs(x) do
-- cx= cx + 1
--end
--
--ans = cx==cy
if ans then
for i,_ in pairs(x) do
if not(y[i]) then
ans = false
break
end
end
end
return ans
end
local function unique(histos)
local ret = {}
local uqs = {}
local all = {}
for i,k in pairs(histos) do
ret[i] = k
end
local i,h = next(ret)
while h do
uqs[#uqs+1] = i
all[i] = i
ret[i] = nil
for j,v in pairs(ret) do
if same(h,v) then
ret[j] = nil
all[j] = i
end
end
i,h = next(ret)
end
return uqs, all
end
local function bhistogram(points)
local ret = {}
for i,kk in pairs(points) do
local h = {}
for _,k in pairs(kk) do
h[k] = true
end
ret[i] = h
end
return ret
end
function M.histogram(points)
local ret = {}
for i,kk in pairs(points) do
local h = {}
for _,k in pairs(kk) do
h[k] = (h[k] or 0) + 1
end
ret[i] = h
end
return ret
end
function M.stats(points)
local ret = {}
for ii,v in pairs(points) do
local cnt = #v
local sum = 0
local sumsq = 0
for _,x in pairs(v) do
sum = sum + x
sumsq = sumsq + x*x
end
local mean = sum/cnt
ret[ii] = {mean, sqrt(sumsq/cnt-mean*mean)}
end
return ret
end
function M.concise(path, ret)
local data = slurp(path,'-?%d+')
local hists = bhistogram(data)
local uqs = unique(hists)
local ret = ret or {}
for _,ii in pairs(uqs) do
ret[#ret+1]=data[ii]
end
return ret
end
M.unique=unique
M.bhistogram=bhistogram
M.same=same
M.maximal=maximal
M.addition=addition
M.positivity=positivity
return M