Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 3, 2017 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/78f8b215a636ff365b2b416f5e03f628 to your computer and use it in GitHub Desktop.
Save vznvzn/78f8b215a636ff365b2b416f5e03f628 to your computer and use it in GitHub Desktop.
def f2(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def adv(x)
x['n'] = x['nb'].to_i(2)
n1 = n = x['n']
l = [n]
while (n >= n1 && n != 1)
n = f2(n)
l << n
end
x['l'] = l
x['ls'] = l.size
x['ns'] = x['n'].to_s(2).length
x['h'] = x['ls'].to_f / x['ns']
x['h2'] = (x['h'] * 50).to_i
# out(x, ['h2', 'ns', 'ls']) if (x['h2'] > 20)
$h[x['h2']] = [] if (!$h.member?(x['h2']))
$h[x['h2']] << x if ($h[x['h2']].size < $c)
return x
end
def out1(a, l)
if ($f.nil?) then
$f = File.open('out.txt', 'w')
$f.puts(l.join("\t"))
end
$f.puts(a.values_at(*l).join("\t"))
$f.flush
end
def stat(l)
l = [0] if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return a, sd, l.max.to_f, l.min.to_f
end
def stat2(l, t)
return Hash[[['a', 'sd', 'mx' , 'mn'], stat(l).map { |x| x / t }].transpose]
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def data(x)
n = x['n']
ns = n.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0..m]
nsl = ns[m..-1]
asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl)
l1 = ns.split(/1+/)
l1.shift
asdm0 = stat2(l1.map { |x| x.length }, nl)
return {'nl' => nl, 'ls' => x['ls'], 'h2' => x['h2'], 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1)
end
def out(fn, l)
f = File.open("#{fn}.txt", 'w')
f.puts(l[0].keys.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.close
$stdout.puts("#{l.size} pts")
end
w = ARGV[0].to_i
$stdout.puts("#{w} width")
l = []
$h = {}
$c = 10
20.times { l << adv({'nb' => (0...w).map { rand(2).to_s }.join}) }
c = 50000
c.times \
{
|i|
l.sort_by! { |x| -x['ls'] }
t = l.size / 10
case i % 3
when 0
x = l[rand(t)]
s = x['nb']
r = rand(s.length)
s[r, 1] = (s[r, 1].to_i ^ 1).to_s
when 1
x = l[rand(t)]
y = l[rand(t)]
sx = x['nb']
sy = y['nb']
s = ''
w.times \
{
|j|
s[j, 1] = (rand(2) == 0) ? sx[j, 1] : sy[j, 1]
}
when 2
x = l[rand(t)]
y = l[rand(t)]
sx = x['nb']
sy = y['nb']
s = ''
r = rand(w + 1)
w.times \
{
|j|
s[j, 1] = (j < r) ? sx[j, 1] : sy[j, 1]
}
end
l.pop if (l.size > 500)
l << adv({'nb' => s})
}
l = []
$h.sort.select { |k, v| v.size == $c }.each \
{
|k, v|
v.each { |x| l << data(x) }
}
out('data', l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment