September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,15 @@
/*
* Nigel Galloway, July 19th., 2017 - Yes well is this any better?
*/
class N{
uint n,i,g,e,l;
public:
N(uint n): n(n-1),i{},g{},e(1),l(n-1){}
bool hasNext(){
g=(1<<n)+e;for(i=l;i<n;++i) g+=1<<i;
if (l==2) {l=--n; e=1; return true;}
if (e<((1<<(l-1))-1)) {++e; return true;}
e=1; --l; return (l>0);
}
uint next() {return g;}
};

View file

@ -0,0 +1,4 @@
int main(){
N n(4);
while (n.hasNext()) std::cout << n.next() << "\t* " << std::bitset<4>(n.next()) << std::endl;
}

View file

@ -0,0 +1,4 @@
int main(){
N n(31);
int z{};for (;n.hasNext();++z); std::cout << z << std::endl;
}

View file

@ -1,57 +0,0 @@
/*
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

@ -1,57 +0,0 @@
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,16 +0,0 @@
> coffee non_contig_subseq.coffee
[ [ 1, 3 ],
[ 1, 4 ],
[ 2, 4 ],
[ 1, 2, 4 ],
[ 1, 3, 4 ] ]
for n=1 there are 0 solutions
for n=2 there are 0 solutions
for n=3 there are 1 solutions
for n=4 there are 5 solutions
for n=5 there are 16 solutions
for n=6 there are 42 solutions
for n=7 there are 99 solutions
for n=8 there are 219 solutions
for n=9 there are 466 solutions
for n=10 there are 968 solutions

View file

@ -0,0 +1,33 @@
// version 1.1.2
fun <T> ncs(a: Array<T>) {
fun generate(m: Int, k: Int, c: IntArray) {
if (k == m) {
if (c[m - 1] != c[0] + m - 1) {
for (i in 0 until m) print("${a[c[i]]} ")
println()
}
}
else {
for (j in 0 until a.size) {
if (k == 0 || j > c[k - 1]) {
c[k] = j
generate(m, k + 1, c)
}
}
}
}
for (m in 2 until a.size) {
val c = IntArray(m)
generate(m, 0, c)
}
}
fun main(args: Array<String>) {
val a = arrayOf(1, 2, 3, 4)
ncs(a)
println()
val ca = arrayOf('a', 'b', 'c', 'd', 'e')
ncs(ca)
}

View file

@ -0,0 +1,33 @@
bool countonly = false
integer count = 0
procedure ncs(sequence rest, integer ri=0, sequence taken={}, bool contig=false, bool gap=false)
if ri>=length(rest) then
if contig then
if countonly then
count += 1
else
?taken
end if
end if
else
ri += 1
ncs(rest,ri,taken&rest[ri],gap,gap)
ncs(rest,ri,taken,contig,length(taken)!=0)
end if
end procedure
ncs({1,2,3})
?"==="
ncs({1,2,3,4})
?"==="
countonly = true
atom t0 = time()
sequence s = {}
for i=1 to 20 do
count = 0
ncs(tagset(i))
s = append(s,count)
end for
?time()-t0
?s

View file

@ -1,24 +0,0 @@
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

@ -1,2 +0,0 @@
[[1 3] [1 4] [2 4] [1 2 4] [1 3 4] [1 5] [2 5] [1 2 5] [3 5] [1 3 5]
[2 3 5] [1 2 3 5] [1 4 5] [2 4 5] [1 2 4 5] [1 3 4 5]]

View file

@ -0,0 +1,9 @@
fcn non_continuous_subsequences(ary){
pwerSet(ary).filter(fcn(list){(not isContinuous(list)) })
}
fcn isContinuous(ary){
if(ary.len()<2) return(True);
foreach n in (ary.len()-1){ if(1+ary[n]!=ary[n+1]) return(False); }
return(True);
}
non_continuous_subsequences(T(1,2,3,4)).println();

View file

@ -0,0 +1,4 @@
fcn pwerSet(list){
(0).pump(list.len(),List,List,Utils.Helpers.pickNFrom.fp1(list),
T(T,Void.Write,Void.Write) ) .append(list)
}

View file

@ -0,0 +1,5 @@
fcn brokenSubsequences(str){
pwerSet(str.split("")).apply("concat")
.filter('wrap(substr){ (not str.holds(substr)) })
}
brokenSubsequences("1234").println();