Best shuffle: Difference between revisions

Content added Content deleted
(Removed stack overflow from C version)
(Removed division by zero bug from C version)
Line 128: Line 128:
void best_shuffle(const unsigned char* txt, unsigned char* result) {
void best_shuffle(const unsigned char* txt, unsigned char* result) {
const int len = (int)strlen((char*)txt);
const int len = (int)strlen((char*)txt);
if (len == 0)
return;


#ifdef DEBUG
#ifdef DEBUG
Line 144: Line 146:
fmax = fnew;
fmax = fnew;
}
}
assert(fmax > 0 && fmax <= len);


// how long can our cyclic groups be?
// how long can our cyclic groups be?
Line 207: Line 210:
int main() {
int main() {
char* data[] = {"abracadabra", "seesaw", "elk", "grrrrrr",
char* data[] = {"abracadabra", "seesaw", "elk", "grrrrrr",
"up", "a", "aabbbbaa"};
"up", "a", "aabbbbaa", "", "xxxxx"};
const int data_len = sizeof(data) / sizeof(data[0]);
const int data_len = sizeof(data) / sizeof(data[0]);
for (int i = 0; i < data_len; i++) {
for (int i = 0; i < data_len; i++) {
const int shuf_len = (int)strlen(data[i]) + 1;
const unsigned int shuf_len = strlen(data[i]) + 1;
unsigned char shuf[shuf_len];
unsigned char shuf[shuf_len];


Line 231: Line 234:
up, pu, (0)
up, pu, (0)
a, a, (1)
a, a, (1)
aabbbbaa, bbaaaabb, (0)</pre>
aabbbbaa, bbaaaabb, (0)
, , (0)
xxxxx, xxxxx, (5)</pre>


=={{header|Clojure}}==
=={{header|Clojure}}==