Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 11, 2020 04:40
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/9b4015d5e3d43d4f9328aab70e0976f1 to your computer and use it in GitHub Desktop.
Save vznvzn/9b4015d5e3d43d4f9328aab70e0976f1 to your computer and use it in GitHub Desktop.
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s)
end
def len(ns, p)
l = ns.split(p)
l = [] if (l.nil?)
l.shift if (l[0] == '')
return l.map { |x| x.length }
end
def len1(ns)
return len(ns, /0+/)
end
def len0(ns)
return len(ns, /1+/)
end
def len01(ns)
return len1(ns), len0(ns)
end
def hist(l)
a = {}
l.each { |x| a[x] = a.fetch(x, 0) + 1 }
return a
end
def hdiff(h1, h2)
d = 0
(h1.keys | h2.keys).each { |x| d += (h1.fetch(x, 0) - h2.fetch(x, 0)).abs }
return d
end
def h2(ns)
l1, l0 = len01(ns)
return hist(l0), hist(l1)
end
def hdiff2(h2a, h2b)
return hdiff(h2a[0], h2b[0]) + hdiff(h2a[1], h2b[1])
end
def hdiff1(ns, i)
h2a = h2(ns)
t = c = 0
h1 = nil
loop \
{
ns2 = dense(ns.length, 0.5)
h2b = h2(ns2)
d = hdiff2(h2a, h2b)
t += d
c += 1
h = t.to_f / c
e = h1.nil? ? nil : (h - h1) / h
puts([c, h, i].join("\t"))
h1 = h
break if (c == 20)
}
puts
end
50.times \
{
|i|
hdiff1(dense(100 + 10 * i, 0.5), i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment