Best shuffle: Difference between revisions
Content added Content deleted
(Updates D entries, tags) |
|||
Line 1,051:
=={{header|D}}==
===Simpler Version===
Translation of [[Best_shuffle#Icon_and_Unicon|Icon]] via [[Best_shuffle#AWK|AWK]]
<lang d>import std.stdio, std.random, std.algorithm, std.conv, std.range,
std.typecons;
auto bestShuffle(in dstring o) {
auto s = o.dup;
s.randomShuffle(
foreach (i, ref ci; s) {
if (ci != o[i])
continue;
foreach (j, ref cj; s)
if (ci != cj && ci != o[j] && cj != o[i]) {
swap(ci, cj);
Line 1,070:
}
return tuple(s, zip(s, o).count!q{ a[0] == a[1] }(
} unittest {▼
▲unittest {
assert(bestShuffle("abracadabra"d)[1] == 0);
assert(bestShuffle("immediately"d)[1] == 0);
Line 1,084 ⟶ 1,082:
}
void main(/*in*/ string[] args) {
if (args.length > 1) {
string entry =
auto res =
writefln("%s : %s (%
}
}</lang>
===
<lang d>import std.stdio, std.algorithm, std.range;
extern(C) pure nothrow void* alloca(in size_t size);
// Assume alloca to be pure.
//extern(C) pure nothrow void* alloca(in size_t size);
enum size_t NCHAR = cast(size_t)char.max + 1;
enum size_t MAX_VLA_SIZE = 1024;
Line 1,102:
if (len == 0)
return;
// txt and result must have the same length
// allocate only when necessary
if (result.length != len)
result.length = len;
// how many of each character?
size_t[NCHAR] counts;
Line 1,117:
}
assert(fmax > 0 && fmax <= len);
// all character positions, grouped by character
size_t
▲ {
// If alloca() has failed, or the memory needed is too much
if (counts[ch])▼
// large, then allocate
ndx1 = (ptr1 == null) ? new size_t[len]
}
ndx1[i] = j;▼
{
i++;▼
int pos =
foreach (size_t ch; 0 .. NCHAR)
▲ if (counts[ch])
foreach (j, char c; txt)
if (c == ch) {
pos++;
}
// regroup them for cycles
size_t
{
ndx2 = (ptr2 == null) ? new size_t[
size_t
foreach (size_t i; 0
ndx2[i] = ndx1[n];
if (n >= len) {
m++;
n = m;
}
}
}
// how long can our cyclic groups be?
immutable size_t grp = 1 + (len - 1) / fmax;
// how many of them are full length?
immutable size_t lng = 1 + (len - 1) % fmax;
// rotate each group
{
▲ for (size_t i = 0, j = 0; i < fmax; i++) {
ndx1[j + k - 1] =
ndx1[j + glen - 1] = first;
j += glen;
}
}
// result is original permuted according to our cyclic groups
foreach (size_t i; 0 .. len)
result[ndx2[i]] = txt[ndx1[i]];
}
void main() {
auto data = ["abracadabra", "seesaw", "elk", "grrrrrr",
"up", "a", "aabbbbaa", "", "xxxxx"];
foreach (txt; data) {
auto
bestShuffle(txt,
immutable
writefln("%s, %s, (%d)", txt,
}
}</lang>
{{out}}
<pre>abracadabra, brabacadaar, (0)
seesaw, wssaee, (0)
|