Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,33 @@
|
|||
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
|
||||
|
||||
immutable NCSubSeq{T<:Integer}
|
||||
n::T
|
||||
end
|
||||
|
||||
type NCSubState{T<:Integer}
|
||||
m::T
|
||||
m2s::Function
|
||||
end
|
||||
|
||||
Base.length(a::NCSubSeq) = 2^a.n - div(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
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
n = 4
|
||||
print("Testing NCSubSeq for ", n, " items:\n ")
|
||||
for a in NCSubSeq(n)
|
||||
print(" ", a)
|
||||
end
|
||||
println()
|
||||
|
||||
s = "Rosetta"
|
||||
cs = split(s, "")
|
||||
m = 10
|
||||
n = length(NCSubSeq(length(s))) - m
|
||||
println()
|
||||
println("The 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
|
||||
|
||||
t = {}
|
||||
append!(t, collect(1:10))
|
||||
append!(t, collect(20:10:40))
|
||||
append!(t, big(50):50:200)
|
||||
println()
|
||||
println("Numbers of NC sub-sequences of a given length:")
|
||||
for i in t
|
||||
println(@sprintf("%7d => ", i), length(NCSubSeq(i)))
|
||||
end
|
||||
|
|
@ -1,11 +1,7 @@
|
|||
sub non_continuous_subsequences ( *@list ) {
|
||||
powerset(@list).grep: { 1 != all( .[ 0 ^.. .end] Z- .[0 ..^ .end] ) }
|
||||
@list.combinations.grep: { 1 != all( .[ 0 ^.. .end] Z- .[0 ..^ .end] ) }
|
||||
}
|
||||
|
||||
sub powerset ( *@list ) {
|
||||
reduce( -> @L, $n { [ @L, @L.map: {[ .list, $n ]} ] }, [[]], @list );
|
||||
}
|
||||
|
||||
say ~ non_continuous_subsequences( 1..3 )».perl;
|
||||
say ~ non_continuous_subsequences( 1..4 )».perl;
|
||||
say ~ non_continuous_subsequences( ^4 ).map: {[<a b c d>[.list]].perl};
|
||||
say non_continuous_subsequences( 1..3 )».gist;
|
||||
say non_continuous_subsequences( 1..4 )».gist;
|
||||
say non_continuous_subsequences( ^4 ).map: {[<a b c d>[.list]].gist};
|
||||
|
|
|
|||
|
|
@ -1,33 +1,33 @@
|
|||
/*REXX program to list non-continuous subsequences (NCS), given a seq.*/
|
||||
parse arg list /*the the list from the CL.*/
|
||||
if list='' then list=1 2 3 4 5 /*Specified? Use default. */
|
||||
say 'list=' space(list); say /*show list to the terminal*/
|
||||
w=words(list) ; #=0 /*# words in list; # of NCS*/
|
||||
$=left(123456789,w) /*build a string of digits.*/
|
||||
tail=right($,max(0,w-2)) /*construct a "fast" tail. */
|
||||
/*REXX program lists non-continuous subsequences (NCS), given a sequence. */
|
||||
parse arg list /*obtain the list from the C.L. */
|
||||
if list='' then list=1 2 3 4 5 /*Not specified? Use the default*/
|
||||
say 'list=' space(list); say /*display the list to terminal. */
|
||||
w=words(list) ; #=0 /*W: words in list; # of NCS. */
|
||||
$=left(123456789,w) /*build a string of decimal digs.*/
|
||||
tail=right($,max(0,w-2)) /*construct a "fast" tail. */
|
||||
|
||||
do j=13 to left($,1) || tail /*step through the list. */
|
||||
if verify(j,$)\==0 then iterate /*Not one of the chosen? */
|
||||
f=left(j,1) /*the first digit of j. */
|
||||
NCS=0 /*not non-continuous subseq*/
|
||||
do k=2 to length(j); _=substr(j,k,1) /*pick off a single digit. */
|
||||
if _ <= f then iterate j /*if next digit ≤ then skip*/
|
||||
if _ \== f+1 then NCS=1 /*it's OK as of now. */
|
||||
f=_ /*we now got a new next dig*/
|
||||
do j=13 to left($,1) || tail /*step through the list. */
|
||||
if verify(j,$)\==0 then iterate /*Not one of the chosen? */
|
||||
f=left(j,1) /*use the 1st decimal digit of J.*/
|
||||
NCS=0 /*not non-continuous subsequence.*/
|
||||
do k=2 to length(j); _=substr(j,k,1) /*pick off a single decimal digit*/
|
||||
if _ <= f then iterate j /*if next digit ≤, then skip it.*/
|
||||
if _ \== f+1 then NCS=1 /*it's OK as of now. */
|
||||
f=_ /*now have a new next decimal dig*/
|
||||
end /*k*/
|
||||
|
||||
if \NCS then iterate /*¬OK? Then skip this num.*/
|
||||
#=#+1 /*Eureka! We found one. */
|
||||
x= /*the beginning of the NCS.*/
|
||||
do m=1 for length(j) /*build a thingy to display*/
|
||||
x=x word(list,substr(j,m,1)) /*pick off a number to show*/
|
||||
if \NCS then iterate /*not OK? Then skip this number.*/
|
||||
#=#+1 /*Eureka! We found onea digit.*/
|
||||
x= /*the beginning of the NCS. */
|
||||
do m=1 for length(j) /*build a sequence string to show*/
|
||||
x=x word(list,substr(j,m,1)) /*pick off a number to display. */
|
||||
end /*m*/
|
||||
|
||||
say 'a non-continuous subsequence: ' x /*show a non-cont. subseq. */
|
||||
end /*j*/
|
||||
say 'a non-continuous subsequence: ' x /*show non─continous subsequence.*/
|
||||
end /*j*/
|
||||
|
||||
if #==0 then #='no' /*make it more gooder Eng. */
|
||||
if #==0 then #='no' /*make it more gooder Anglesh. */
|
||||
say; say # "non-continuous subsequence"s(#) 'were found.'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────S subroutine───────────────────────*/
|
||||
s: if arg(1)==1 then return ''; return word(arg(2) 's',1) /*plurals.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
s: if arg(1)==1 then return ''; return word(arg(2) 's',1) /*plurals.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue