Here is Karthur's code:
Code:
class Karthur {
#include <stdio.h>;
int test_main ( void )
{
originalNecklace(1,0,6); printf("should return \"w\"\n");
originalNecklace(0,1,6); printf("should return \"g\"\n");
originalNecklace(5,4,4); printf("should return \"wwggwwwgg\"\n");
originalNecklace(4,3,1); printf("should return \"gggwwww\"\n");
originalNecklace(0,7,25); printf("should return \"ggggggg\"\n");
originalNecklace(9,13,11); printf("should return \"ggggwgwgwwggwggwwwwggg\"\n");
originalNecklace(21,31,4);
originalNecklace(333,667,23);
originalNecklace(500,500,23);
originalNecklace(500,500,22);
originalNecklace(666,334,23);
originalNecklace(23,29,101);
printf("Completed.");
return 0;
}
struct _bead {
struct _bead* next;
struct _bead* prev;
int color;
} BEAD;
#define WHITE 0
#define GOLD 1
void originalNecklace ( int white, int gold, int jump )
{
BEAD* first;
BEAD* added;
BEAD* latest;
int i = 0;
int j = 0;
int k = 0;
// printf("originalNecklace(%d,%d,%d)\n",white,gold,jump);
// if ((white < 1 && gold < 1) || jump < 1) return;
try {
for (; i < white ;i++) { // BUILD ALL-WHITE CHAIN
added = (BEAD*)malloc(sizeof(BEAD);
if (!added) throw("Out of Memory");
added->color = WHITE;
if (i) { // TACK ONTO END
latest->next = added;
added->prev = latest;
} else { // or PICK UP FIRST BEAD (WHITE)
first = added;
added->next = added;
}
latest = added;
}
if (white > 0) { // TIE ENDS TOGETHER
latest->next = first;
first->prev = latest;
}
} catch (Exception e) {
printf("WHITE %s\n",e);
}
try {
for (j = 0; j < gold ;j++) { // INSERT GOLD BEADS
added = (BEAD*)malloc(sizeof(BEAD);
if (!added) throw("Out of Memory");
added.color = GOLD;
if (i+j >= 1) { // INSERT BEFORE CURRENT
latest->next->prev = added;
added->next = latest->next;
latest->next = added;
added->prev = latest;
} else { // or PICK UP FIRST BEAD (GOLD)
first = added;
added->next = added;
}
latest = added;
if (i) {
for (k = 0; k < jump ;k++) { // JUMP BACK TO NEXT INSERTION POINT
latest = latest->prev;
}
}
if (white == 0) { // TIE ENDS TOGETHER
latest->next = first;
first->prev = latest;
}
}
} catch (Exception e) {
printf("GOLD %s\n",e);
}
try {
first = latest->next;
latest->next = null; // DISCONNECT CHAIN FOR SIMPLE TERMINATION
for (latest=first; latest ;latest=latest->next)
printf("%c",(latest->color == WHITE) ? 'W' : 'G');
printf("\n");
} catch (Exception e) {
printf("PRINTING %s\n",e);
}
/*************** NOW TRY TO REMOVE THE GOLD ONES ***************/
try {
first->prev->next = first; // TIE ENDS BACK TOGETHER
latest = first->prev;
while (gold--) {
for (int n = 0; n < k ;n++) // COUNT TO NEXT BEAD
latest = latest->next;
if (latest->color != GOLD) { // MSG & QUIT ON ERROR
printf("** error **\n");
break;
}
latest->prev->next = latest->next; // REMOVE GOLD BEAD
latest->next->prev = latest->prev;
}
// Note: if all-gold, last one still remains as "first" and "latest"
// latest = latest->next;
// if (!white)
// latest = null;
// first = latest;
} catch (Exception e) {
printf("CHECKING %s\n",e);
}
/************************************************** *************/
}
};
Unfortunately I couldn't get it to compile, even after I tried changing a few things and the logic of his code eluded me. Perhaps you can explain it to us Karthur?