This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,24 @@
-module(rosetta).
-export([ncs/1]).
masks(N) ->
MaxMask = trunc(math:pow(2, N)),
Total = lists:map(fun(X) -> integer_to_list(X, 2) end,
lists:seq(3, MaxMask)),
Filtered = lists:filter(fun(X) -> contains_noncont(X) end, Total),
lists:map(fun(X) -> string:right(X, N, $0) end, Filtered). % padding
contains_noncont(N) ->
case re:run(N, "10+1") of
{match, _} -> true;
nomatch -> false
end.
apply_mask_to_list(Mask, List) ->
Zipped = lists:zip(Mask, List),
Filtered = lists:filter(fun({Include, _}) -> Include > 48 end, Zipped),
lists:map(fun({_, Value}) -> Value end, Filtered).
ncs(List) ->
lists:map(fun(Mask) -> apply_mask_to_list(Mask, List) end,
masks(length(List))).

View file

@ -0,0 +1,10 @@
noncontig(n)=n>>=valuation(n,2);n++;n>>=valuation(n,2);n>1;
nonContigSubseq(v)={
for(i=5,2^#v-1,
if(noncontig(i),
print(vecextract(v,i))
)
)
};
nonContigSubseq([1,2,3])
nonContigSubseq(["a","b","c","d","e"])

View file

@ -0,0 +1,22 @@
class Array
def func_power_set
inject([[]]) { |ps,item| # for each item in the Array
ps + # take the powerset up to now and add
ps.map { |e| e + [item] } # it again, with the item appended to each element
}
end
def non_continuous_subsequences
func_power_set.reject {|seq| continuous?(seq)}
end
def continuous?(seq)
seq.each_cons(2) {|a, b| return false if a.succ != b}
true
end
end
p (1..3).to_a.non_continuous_subsequences
p (1..4).to_a.non_continuous_subsequences
p (1..5).to_a.non_continuous_subsequences
p ("a".."d").to_a.non_continuous_subsequences

View file

@ -0,0 +1,8 @@
class Array
def continuous?(seq)
seq.each_cons(2) {|a, b| return false if index(a)+1 != index(b)}
true
end
end
p %w(a e i o u).non_continuous_subsequences