Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,10 +1,14 @@
|
|||
using Printf, IterTools
|
||||
|
||||
import Base.IteratorSize, Base.iterate, Base.length
|
||||
|
||||
iscontseq(n::Integer) = count_zeros(n) == leading_zeros(n) + trailing_zeros(n)
|
||||
iscontseq(n::BigInt) = !ismatch(r"0", rstrip(bin(n), '0'))
|
||||
|
||||
function makeint2seq(n::Integer)
|
||||
const idex = collect(1:n)
|
||||
idex = collect(1:n)
|
||||
function int2seq(m::Integer)
|
||||
d = digits(m, 2, n)
|
||||
d = digits(m, base=2, pad=n)
|
||||
idex[d .== 1]
|
||||
end
|
||||
return int2seq
|
||||
|
|
@ -19,12 +23,12 @@ mutable struct NCSubState{T<:Integer}
|
|||
m2s::Function
|
||||
end
|
||||
|
||||
Base.iteratorsize(::NCSubSeq) = Base.HasLength()
|
||||
Base.IteratorSize(::NCSubSeq) = Base.HasLength()
|
||||
Base.length(a::NCSubSeq) = 2 ^ a.n - a.n * (a.n + 1) ÷ 2 - 1
|
||||
|
||||
Base.start(a::NCSubSeq) = NCSubState(5, makeint2seq(a.n))
|
||||
Base.done(a::NCSubSeq, as::NCSubState) = 2 ^ a.n - 3 < as.m
|
||||
function Base.next(a::NCSubSeq, as::NCSubState)
|
||||
function Base.iterate(a::NCSubSeq, as::NCSubState=NCSubState(5, makeint2seq(a.n)))
|
||||
if 2 ^ a.n - 3 < as.m
|
||||
return nothing
|
||||
end
|
||||
s = as.m2s(as.m)
|
||||
as.m += 1
|
||||
while iscontseq(as.m)
|
||||
|
|
@ -39,18 +43,16 @@ println("Testing NCSubSeq for ", n, " items:\n ", join(NCSubSeq(n), " "))
|
|||
s = "Rosetta"
|
||||
cs = split(s, "")
|
||||
m = 10
|
||||
n = length(NCSubSeq(length(s))) - m
|
||||
n = length(NCSubSeq(length(cs))) - m
|
||||
println("\nThe first and last ", m, " NC sub-sequences of \"", s, "\":")
|
||||
for (i, a) in enumerate(NCSubSeq(length(cs)))
|
||||
for (i, a) in enumerate(NCSubSeq(length(s)))
|
||||
i <= m || n < i || continue
|
||||
println(@sprintf "%6d %s" i join(cs[a], ""))
|
||||
i == m || continue
|
||||
println(" .. ......")
|
||||
end
|
||||
|
||||
using IterTools.chain
|
||||
|
||||
println("\nThe first and last ", m, " NC sub-sequences of \"", s, "\"")
|
||||
for x in IterTools.chain(1:10, 20:10:40, big.(50:50:200))
|
||||
for x in IterTools.Iterators.flatten([1:10; 20:10:40; big.(50:50:200)])
|
||||
@printf "%7d → %d\n" x length(NCSubSeq(x))
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue