C, 65 57 bytes
but this solution works correctly with any data only at single function call (when the global variable r
is initialized to zero), to work in a multiple the initialization should be added (extending code to 60 bytes):
r;f(char*s){for(r=0;*++s&&(r=~r&1<<(*s>*(s-1))););return r;}
But in any case this is shorter than previous solution (65 bytes) due to use of for
instead of while
. But previous (the following) is a little easier to understand:
#include <stdio.h>
#include <string.h>
r;f(char*s){for(;*++s&&(r=~r&1<<(*s>*(s-1))););return r;}
int main(void)
{
char * Truthy[] = { "ABA",
"ABB",
"BAB",
"BUMP",
"BALD",
"BALDY",
"UPWARD",
"EXAMINATION",
"AZBYCXDWEVFUGTHSIRJQKPLOMN" };
char * Falsey[] = { "AAA",
"BBA",
"ACE",
"THE",
"BUMPY",
"BALDING",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
int posTestNum = sizeof(Truthy) / sizeof(char *);
int negTestNum = sizeof(Falsey) / sizeof(char *);
int i;
int rate = 0;
int tests = 0;
int res = 0;
printf("Truthy (%d tests):\n", posTestNum);
for (i = 0; i < posTestNum; i++)
{
tests++;
r = 0;
printf("%s - %s\n", Truthy[i], f(Truthy[i]) ? (rate++, "OK") : "Fail");
r = 0;
}
printf("\nFalsey (%d tests):\n", negTestNum);
for (i = 0; i < negTestNum; i++)
{
tests++;
r = 0;
printf("%s - %s\n", Falsey[i], f(Falsey[i]) ? "Fail" : (rate++, "OK"));
r = 0;
}
printf("\n%d of %d tests passed\n", rate, tests);
return 0;
}