Best shuffle: Difference between revisions

Content added Content deleted
(Removed division by zero bug from C version)
(Removed division by zero bug from first D version)
Line 306: Line 306:
enum int MAX_VLA_SIZE = 1024;
enum int MAX_VLA_SIZE = 1024;
const int len = txt.length;
const int len = txt.length;
if (len == 0)
return;


// txt and result must have the same length
// txt and result must have the same length
Line 322: Line 324:
fmax = counts[c];
fmax = counts[c];
}
}
assert(fmax > 0 && fmax <= len);


// how long can our cyclic groups be?
// how long can our cyclic groups be?
Line 372: Line 375:


void main() {
void main() {
auto data = ["abracadabra", "seesaw", "elk",
auto data = ["abracadabra", "seesaw", "elk", "grrrrrr",
"grrrrrr", "up", "a", "aabbbbaa"];
"up", "a", "aabbbbaa", "", "xxxxx"];
foreach (txt; data) {
foreach (txt; data) {
auto shuf = txt.dup;
auto shuf = txt.dup;
Line 388: Line 391:
up, pu, (0)
up, pu, (0)
a, a, (1)
a, a, (1)
aabbbbaa, bbaaaabb, (0)</pre>
aabbbbaa, bbaaaabb, (0)
, , (0)
xxxxx, xxxxx, (5)</pre>
Using idea from [http://rosettacode.org/wiki/Talk:Best_shuffle#J_implementation_notes J implementation notes] at discussion page.
Using idea from [http://rosettacode.org/wiki/Talk:Best_shuffle#J_implementation_notes J implementation notes] at discussion page.
{{works with|D|2.051}}
{{works with|D|2.051}}