Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -4,7 +4,8 @@ A ''subsequence'' contains some subset of the elements of this sequence, in the
A ''continuous'' subsequence is one in which no elements are missing between the first and last elements of the subsequence.
Note: Subsequences are defined ''structurally'', not by their contents. So a sequence ''a,b,c,d'' will always have the same subsequences and continuous subsequences, no matter which values are substituted; it may even be the same value.
Note: Subsequences are defined ''structurally'', not by their contents.
So a sequence ''a,b,c,d'' will always have the same subsequences and continuous subsequences, no matter which values are substituted; it may even be the same value.
'''Task''': Find all non-continuous subsequences for a given sequence. Example: For the sequence ''1,2,3,4'', there are five non-continuous subsequences, namely ''1,3''; ''1,4''; ''2,4''; ''1,3,4'' and ''1,2,4''.

View file

@ -0,0 +1,57 @@
/*
Not best code, wrote it really quick. Will add updated code using more C++11 features soon!
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main() {
std::vector<int> sequence = { 0, 1, 2, 3, 4 };
std::vector<int> work = { 0 };
std::vector<std::vector<int>> results;
while (work != sequence) {
bool zeroed = false;
size_t index_zero_started = 0;
for (size_t i = 0; i < work.size(); ++i) {
if (work[i] >= sequence.size()) {
if (i == 0) {
work[i] = 0;
work.push_back(0);
index_zero_started = i;
} else {
++work[i - 1];
for (size_t j = i; j < work.size(); ++j) {
work[j] = 0;
}
index_zero_started = i - 1;
}
zeroed = true;
break;
}
}
if (zeroed) {
for (size_t i = index_zero_started + 1; i < work.size(); ++i) {
work[i] = work[i - 1] + 1;
}
} else {
std::vector<int> temp_differences;
std::adjacent_difference(std::begin(work), std::end(work), std::back_inserter(temp_differences));
if (std::find_if(std::begin(temp_differences) + 1, std::end(temp_differences),
[](const int& n) { return n > 1; }) != std::end(temp_differences)) {
results.push_back(work);
}
++work.back();
}
}
std::cout << "Non-continuous subsequences of ";
std::copy(std::begin(sequence), std::end(sequence), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
for (auto& e: results) {
std::cout << "- ";
std::copy(std::begin(e), std::end(e), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
return 0;
}

View file

@ -0,0 +1,57 @@
is_contigous_binary = (n) ->
# return true if binary representation of n is
# of the form 1+0+
# examples:
# 0 true
# 1 true
# 100 true
# 110 true
# 1001 false
# 1010 false
# special case zero, or you'll get an infinite loop later
return true if n == 0
# first remove 0s from end
while n % 2 == 0
n = n / 2
# next, take advantage of the fact that a continuous
# run of 1s would be of the form 2^n - 1
is_power_of_two(n + 1)
is_power_of_two = (m) ->
while m % 2 == 0
m = m / 2
m == 1
seq_from_bitmap = (arr, n) ->
# grabs elements from array according to a bitmap
# e.g. if n == 13 (1101), and arr = ['a', 'b', 'c', 'd'],
# then return ['a', 'c', 'd'] (flipping bits to 1011, so
# that least significant bit comes first)
i = 0
new_arr = []
while n > 0
if n % 2 == 1
new_arr.push arr[i]
n -= 1
n /= 2
i += 1
new_arr
non_contig_subsequences = (arr) ->
# Return all subsqeuences from an array that have a "hole" in
# them. The order of the subsequences is not specified here.
# This algorithm uses binary counting, so it is limited to
# small lists, but large lists would be unwieldy regardless.
bitmasks = [0...Math.pow(2, arr.length)]
(seq_from_bitmap arr, n for n in bitmasks when !is_contigous_binary n)
arr = [1,2,3,4]
console.log non_contig_subsequences arr
for n in [1..10]
arr = [1..n]
num_solutions = non_contig_subsequences(arr).length
console.log "for n=#{n} there are #{num_solutions} solutions"

View file

@ -1,18 +1,18 @@
import std.stdio;
T[][] ncsub(T)(in T[] seq, in int s=0) pure nothrow {
T[][] ncsub(T)(in T[] seq, in uint s=0) pure nothrow @safe {
if (seq.length) {
T[][] aux;
foreach (ys; ncsub(seq[1..$], s + !(s % 2)))
typeof(return) aux;
foreach (ys; ncsub(seq[1 .. $], s + !(s % 2)))
aux ~= seq[0] ~ ys;
return aux ~ ncsub(seq[1..$], s + s % 2);
return aux ~ ncsub(seq[1 .. $], s + s % 2);
} else
return new T[][](s >= 3, 0);
return new typeof(return)(s >= 3, 0);
}
void main() {
writeln(ncsub([1, 2, 3]));
writeln(ncsub([1, 2, 3, 4]));
foreach (nc; ncsub([1, 2, 3, 4, 5]))
writeln(nc);
void main() @safe {
import std.stdio;
[1, 2, 3].ncsub.writeln;
[1, 2, 3, 4].ncsub.writeln;
foreach (const nc; [1, 2, 3, 4, 5].ncsub)
nc.writeln;
}

View file

@ -1,29 +1,28 @@
struct Ncsub(T) {
T[] seq;
int opApply(int delegate(ref int[]) dg) const {
immutable int n = seq.length;
int opApply(int delegate(ref T[]) dg) const {
immutable n = seq.length;
int result;
auto S = new int[n];
auto S = new T[n];
FOR_I:
foreach (i; 1 .. 1 << seq.length) {
int len_S;
OUTER: foreach (immutable i; 1 .. 1 << n) {
uint lenS;
bool nc = false;
foreach (j; 0 .. seq.length + 1) {
immutable int k = i >> j;
foreach (immutable j; 0 .. n + 1) {
immutable k = i >> j;
if (k == 0) {
if (nc) {
auto auxS = S[0 .. len_S];
auto auxS = S[0 .. lenS];
result = dg(auxS);
if (result)
break FOR_I;
break OUTER;
}
break;
} else if (k % 2) {
S[len_S] = seq[j];
len_S++;
} else if (len_S)
S[lenS] = seq[j];
lenS++;
} else if (lenS)
nc = true;
}
}
@ -34,9 +33,10 @@ struct Ncsub(T) {
void main() {
import std.array, std.range;
//assert(iota(24).array().Ncsub!int().walkLength() == 16_776_915);
auto r = array(iota(24));
int counter;
//assert(24.iota.array.Ncsub!int.walkLength == 16_776_915);
auto r = 24.iota.array;
uint counter = 0;
foreach (s; Ncsub!int(r))
counter++;
assert(counter == 16_776_915);

View file

@ -0,0 +1,34 @@
import std.stdio, std.array, std.range, std.concurrency;
Generator!(T[]) ncsub(T)(in T[] seq) {
return new typeof(return)({
immutable n = seq.length;
auto S = new T[n];
foreach (immutable i; 1 .. 1 << n) {
uint lenS = 0;
bool nc = false;
foreach (immutable j; 0 .. n + 1) {
immutable k = i >> j;
if (k == 0) {
if (nc)
yield(S[0 .. lenS]);
break;
} else if (k % 2) {
S[lenS] = seq[j];
lenS++;
} else if (lenS)
nc = true;
}
}
});
}
void main() {
assert(24.iota.array.ncsub.walkLength == 16_776_915);
[1, 2, 3].ncsub.writeln;
[1, 2, 3, 4].ncsub.writeln;
foreach (const nc; [1, 2, 3, 4, 5].ncsub)
nc.writeln;
}

View file

@ -0,0 +1,16 @@
public class NonContinuousSubsequences {
public static void main(String args[]) {
seqR("1234", "", 0, 0);
}
private static void seqR(String s, String c, int i, int added) {
if (i == s.length()) {
if (c.trim().length() > added)
System.out.println(c);
} else {
seqR(s, c + s.charAt(i), i + 1, added + 1);
seqR(s, c + ' ', i + 1, added);
}
}
}

View file

@ -0,0 +1,24 @@
define ncsubseq(l);
lvars acc = [], gap_started = false, is_continuous = true;
define do_it(l1, l2);
dlocal gap_started;
lvars el, save_is_continuous = is_continuous;
if l2 = [] then
if not(is_continuous) then
cons(l1, acc) -> acc;
endif;
else
front(l2) -> el;
back(l2) -> l2;
not(gap_started) and is_continuous -> is_continuous;
do_it(cons(el, l1), l2);
save_is_continuous -> is_continuous;
not(l1 = []) or gap_started -> gap_started;
do_it(l1, l2);
endif;
enddefine;
do_it([], rev(l));
acc;
enddefine;
ncsubseq([1 2 3 4 5]) =>

View file

@ -0,0 +1,30 @@
$ include "seed7_05.s7i";
const func array bitset: ncsub (in bitset: seq, in integer: s) is func
result
var array bitset: subseq is 0 times {};
local
var bitset: x is {};
var bitset: xs is {};
var bitset: ys is {};
begin
if seq <> {} then
x := {min(seq)};
xs := seq - x;
for ys range ncsub(xs, s + 1 - s rem 2) do
subseq &:= x | ys;
end for;
subseq &:= ncsub(xs, s + s rem 2);
elsif s >= 3 then
subseq &:= {};
end if;
end func;
const proc: main is func
local
var bitset: seq is {};
begin
for seq range ncsub({1, 2, 3, 4}, 0) do
writeln(seq);
end for;
end func;