June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,56 @@
|
|||
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)
|
||||
function int2seq(m::Integer)
|
||||
d = digits(m, 2, n)
|
||||
idex[d .== 1]
|
||||
end
|
||||
return int2seq
|
||||
end
|
||||
|
||||
struct NCSubSeq{T<:Integer}
|
||||
n::T
|
||||
end
|
||||
|
||||
mutable struct NCSubState{T<:Integer}
|
||||
m::T
|
||||
m2s::Function
|
||||
end
|
||||
|
||||
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)
|
||||
s = as.m2s(as.m)
|
||||
as.m += 1
|
||||
while iscontseq(as.m)
|
||||
as.m += 1
|
||||
end
|
||||
return (s, as)
|
||||
end
|
||||
|
||||
n = 4
|
||||
println("Testing NCSubSeq for ", n, " items:\n ", join(NCSubSeq(n), " "))
|
||||
|
||||
s = "Rosetta"
|
||||
cs = split(s, "")
|
||||
m = 10
|
||||
n = length(NCSubSeq(length(s))) - m
|
||||
println("\nThe first and last ", m, " NC sub-sequences of \"", s, "\":")
|
||||
for (i, a) in enumerate(NCSubSeq(length(cs)))
|
||||
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))
|
||||
@printf "%7d → %d\n" x length(NCSubSeq(x))
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue