Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,39 @@
|
|||
import std.stdio, std.algorithm, std.conv;
|
||||
|
||||
string[] selfReferentialSeq(string n, string[] seen=[]) nothrow {
|
||||
__gshared static string[][string] cache;
|
||||
if (n in cache)
|
||||
return cache[n];
|
||||
if (seen.canFind(n))
|
||||
return [];
|
||||
|
||||
int[10] digit_count;
|
||||
foreach (immutable d; n)
|
||||
digit_count[d - '0']++;
|
||||
string term;
|
||||
foreach_reverse (immutable d; 0 .. 10)
|
||||
if (digit_count[d] > 0)
|
||||
term ~= text(digit_count[d], d);
|
||||
return cache[n] = [n] ~ selfReferentialSeq(term, [n] ~ seen);
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum int limit = 1_000_000;
|
||||
int max_len;
|
||||
int[] max_vals;
|
||||
|
||||
foreach (immutable n; 1 .. limit) {
|
||||
const seq = n.text().selfReferentialSeq();
|
||||
if (seq.length > max_len) {
|
||||
max_len = seq.length;
|
||||
max_vals = [n];
|
||||
} else if (seq.length == max_len)
|
||||
max_vals ~= n;
|
||||
}
|
||||
|
||||
writeln("values: ", max_vals);
|
||||
writeln("iterations: ", max_len);
|
||||
writeln("sequence:");
|
||||
foreach (const idx, const val; max_vals[0].text.selfReferentialSeq)
|
||||
writefln("%2d %s", idx + 1, val);
|
||||
}
|
||||
179
Task/Summarize-and-say-sequence/D/summarize-and-say-sequence-2.d
Normal file
179
Task/Summarize-and-say-sequence/D/summarize-and-say-sequence-2.d
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
import std.range, std.algorithm;
|
||||
|
||||
struct Permutations(bool doCopy=true, T) {
|
||||
T[] items;
|
||||
int r;
|
||||
bool stopped;
|
||||
int[] indices, cycles;
|
||||
static if (!doCopy)
|
||||
T[] result;
|
||||
|
||||
this(T)(T[] items, int r=-1) pure nothrow @safe {
|
||||
this.items = items;
|
||||
immutable int n = items.length;
|
||||
if (r < 0)
|
||||
r = n;
|
||||
this.r = r;
|
||||
immutable n_minus_r = n - r;
|
||||
if (n_minus_r < 0) {
|
||||
this.stopped = true;
|
||||
} else {
|
||||
this.stopped = false;
|
||||
this.indices = n.iota.array;
|
||||
//this.cycles = iota(n, n_minus_r, -1).array; // Not nothrow.
|
||||
this.cycles = iota(n_minus_r + 1, n + 1).retro.array;
|
||||
}
|
||||
|
||||
static if (!doCopy)
|
||||
result = new T[r];
|
||||
}
|
||||
|
||||
@property bool empty() const pure nothrow @safe @nogc {
|
||||
return this.stopped;
|
||||
}
|
||||
|
||||
static if (doCopy) {
|
||||
@property T[] front() const pure nothrow @safe {
|
||||
assert(!this.stopped);
|
||||
auto result = new T[r];
|
||||
foreach (immutable i, ref re; result)
|
||||
re = items[indices[i]];
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
@property T[] front() pure nothrow @safe @nogc {
|
||||
assert(!this.stopped);
|
||||
foreach (immutable i, ref re; this.result)
|
||||
re = items[indices[i]];
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
||||
void popFront() pure nothrow /*@safe*/ @nogc {
|
||||
assert(!this.stopped);
|
||||
int i = r - 1;
|
||||
while (i >= 0) {
|
||||
immutable int j = cycles[i] - 1;
|
||||
if (j > 0) {
|
||||
cycles[i] = j;
|
||||
indices[i].swap(indices[$ - j]);
|
||||
return;
|
||||
}
|
||||
cycles[i] = indices.length - i;
|
||||
immutable int n1 = indices.length - 1;
|
||||
assert(n1 >= 0);
|
||||
immutable int num = indices[i];
|
||||
|
||||
// copy isn't @safe.
|
||||
indices[i + 1 .. n1 + 1].copy(indices[i .. n1]);
|
||||
indices[n1] = num;
|
||||
i--;
|
||||
}
|
||||
|
||||
this.stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
Permutations!(doCopy, T) permutations(bool doCopy=true, T)
|
||||
(T[] items, int r=-1)
|
||||
pure nothrow @safe {
|
||||
return Permutations!(doCopy, T)(items, r);
|
||||
}
|
||||
|
||||
// ---------------------------------
|
||||
|
||||
import std.stdio, std.typecons, std.conv, std.algorithm, std.array,
|
||||
std.exception, std.string;
|
||||
|
||||
enum maxIters = 1_000_000;
|
||||
|
||||
string A036058(in string ns) pure nothrow @safe {
|
||||
return ns.representation.group.map!(t => t[1].text ~ char(t[0])).join;
|
||||
}
|
||||
|
||||
int A036058_length(bool doPrint=false)(string numberString="0") {
|
||||
int iterations = 1;
|
||||
int queueIndex;
|
||||
string[3] lastThree;
|
||||
|
||||
while (true) {
|
||||
static if (doPrint)
|
||||
writefln(" %2d %s", iterations, numberString);
|
||||
|
||||
numberString = numberString
|
||||
.dup
|
||||
.representation
|
||||
.sort()
|
||||
.release
|
||||
.assumeUTF;
|
||||
|
||||
if (lastThree[].canFind(numberString))
|
||||
break;
|
||||
assert(iterations < maxIters);
|
||||
lastThree[queueIndex] = numberString;
|
||||
numberString = numberString.A036058;
|
||||
iterations++;
|
||||
queueIndex++;
|
||||
queueIndex %= 3;
|
||||
}
|
||||
|
||||
return iterations;
|
||||
}
|
||||
|
||||
Tuple!(int,int[]) max_A036058_length(R)(R startRange = 11.iota) {
|
||||
bool[string] alreadyDone;
|
||||
auto max_len = tuple(-1, (int[]).init);
|
||||
|
||||
foreach (n; startRange) {
|
||||
immutable sns = n
|
||||
.to!(char[])
|
||||
.representation
|
||||
.sort()
|
||||
.release
|
||||
.assumeUTF;
|
||||
|
||||
if (sns !in alreadyDone) {
|
||||
alreadyDone[sns] = true;
|
||||
const size = sns.A036058_length;
|
||||
if (size > max_len[0])
|
||||
max_len = tuple(size, [n]);
|
||||
else if (size == max_len[0])
|
||||
max_len[1] ~= n;
|
||||
}
|
||||
}
|
||||
return max_len;
|
||||
}
|
||||
|
||||
void main() {
|
||||
//const (lenMax, starts) = maxIters.iota.max_A036058_length;
|
||||
const lenMax_starts = maxIters.iota.max_A036058_length;
|
||||
immutable lenMax = lenMax_starts[0];
|
||||
const starts = lenMax_starts[1];
|
||||
|
||||
// Expand:
|
||||
int[] allStarts;
|
||||
foreach (immutable n; starts) {
|
||||
bool[string] set;
|
||||
foreach (const k; permutations!false(n.to!(char[]), 4))
|
||||
if (k[0] != '0')
|
||||
set[k.idup] = true;
|
||||
//allStarts ~= set.byKey.to!(int[]);
|
||||
allStarts ~= set.byKey.map!(to!int).array;
|
||||
}
|
||||
|
||||
allStarts = allStarts.sort().filter!(x => x < maxIters).array;
|
||||
|
||||
writefln("The longest length, followed by the number(s) with the
|
||||
longest sequence length for starting sequence numbers below maxIters
|
||||
are:
|
||||
Iterations = %d and sequence-starts = %s.", lenMax, allStarts);
|
||||
|
||||
writeln("Note that only the first of any sequences with the same
|
||||
digits is printed below. (The others will differ only in their first
|
||||
term).");
|
||||
|
||||
foreach (immutable n; starts) {
|
||||
writeln;
|
||||
A036058_length!true(n.text);
|
||||
}
|
||||
}
|
||||
134
Task/Summarize-and-say-sequence/D/summarize-and-say-sequence-3.d
Normal file
134
Task/Summarize-and-say-sequence/D/summarize-and-say-sequence-3.d
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
import core.stdc.stdio, core.stdc.stdlib;
|
||||
|
||||
struct MemoryPool(T, int MAX_BLOCK_BYTES = 1 << 17) {
|
||||
static assert(!is(T == class),
|
||||
"MemoryPool is designed for native data.");
|
||||
static assert(MAX_BLOCK_BYTES >= 1,
|
||||
"MemoryPool: MAX_BLOCK_BYTES must be >= 1 bytes.");
|
||||
static assert(MAX_BLOCK_BYTES >= T.sizeof,
|
||||
"MemoryPool: MAX_BLOCK_BYTES must be" ~
|
||||
" bigger than a T.");
|
||||
static if (T.sizeof * 5 > MAX_BLOCK_BYTES)
|
||||
pragma(msg, "MemoryPool: Block is very small.");
|
||||
|
||||
alias Block = T[MAX_BLOCK_BYTES / T.sizeof];
|
||||
static __gshared Block*[] blocks;
|
||||
static __gshared T* nextFree, lastFree;
|
||||
|
||||
static T* newItem() nothrow {
|
||||
if (nextFree >= lastFree) {
|
||||
blocks ~= cast(Block*)calloc(1, Block.sizeof);
|
||||
if (blocks[$ - 1] == null)
|
||||
exit(1);
|
||||
nextFree = blocks[$ - 1].ptr;
|
||||
lastFree = nextFree + Block.length;
|
||||
}
|
||||
|
||||
return nextFree++;
|
||||
}
|
||||
|
||||
// static void freeAll() nothrow {
|
||||
// foreach (blockPtr; blocks)
|
||||
// free(blockPtr);
|
||||
// blocks.length = 0;
|
||||
// nextFree = null;
|
||||
// lastFree = null;
|
||||
// }
|
||||
}
|
||||
|
||||
struct Rec { // Tree node
|
||||
int length;
|
||||
Rec*[10] p;
|
||||
}
|
||||
|
||||
__gshared int nNodes;
|
||||
__gshared Rec* rec_root;
|
||||
__gshared MemoryPool!Rec recPool;
|
||||
|
||||
Rec* findRec(char* s, Rec* root) nothrow {
|
||||
int c;
|
||||
Rec* next;
|
||||
|
||||
while (true) {
|
||||
c = *s;
|
||||
s++;
|
||||
if (!c)
|
||||
break;
|
||||
c -= '0';
|
||||
next = root.p[c];
|
||||
if (!next) {
|
||||
nNodes++;
|
||||
next = recPool.newItem;
|
||||
root.p[c] = next;
|
||||
}
|
||||
root = next;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void nextNum(char* s) nothrow @nogc {
|
||||
int[10] cnt;
|
||||
for (int i = 0; s[i]; i++)
|
||||
cnt[s[i] - '0']++;
|
||||
|
||||
foreach_reverse (i; 0 .. 10) {
|
||||
if (!cnt[i])
|
||||
continue;
|
||||
s += sprintf(s, "%d%c", cnt[i], i + '0');
|
||||
}
|
||||
}
|
||||
|
||||
int getLen(char* s, int depth) nothrow {
|
||||
auto r = findRec(s, rec_root);
|
||||
if (r.length > 0)
|
||||
return r.length;
|
||||
|
||||
depth++;
|
||||
if (!r.length)
|
||||
r.length = -depth;
|
||||
else
|
||||
r.length += depth;
|
||||
|
||||
nextNum(s);
|
||||
depth = 1 + getLen(s, depth);
|
||||
|
||||
if (r.length <= 0)
|
||||
r.length = depth;
|
||||
return r.length;
|
||||
}
|
||||
|
||||
void main() nothrow {
|
||||
enum MAXN = 1_000_000;
|
||||
|
||||
int[100] longest;
|
||||
int nLongest, ml;
|
||||
char[32] buf;
|
||||
rec_root = recPool.newItem();
|
||||
|
||||
foreach (immutable i; 0 .. MAXN) {
|
||||
sprintf(buf.ptr, "%d", i);
|
||||
int l = getLen(buf.ptr, 0);
|
||||
if (l < ml)
|
||||
continue;
|
||||
if (l > ml) {
|
||||
nLongest = 0;
|
||||
ml = l;
|
||||
}
|
||||
longest[nLongest] = i;
|
||||
nLongest++;
|
||||
}
|
||||
|
||||
printf("seq leng: %d\n\n", ml);
|
||||
foreach (immutable i; 0 .. nLongest) {
|
||||
sprintf(buf.ptr, "%d", longest[i]);
|
||||
// print len+1 so we know repeating starts from when
|
||||
foreach (immutable l; 0 .. ml + 1) {
|
||||
printf("%2d: %s\n", getLen(buf.ptr, 0), buf.ptr);
|
||||
nextNum(buf.ptr);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("Allocated %d Rec tree nodes.\n", nNodes);
|
||||
//recPool.freeAll;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue