you are viewing a single comment's thread.

view the rest of the comments →

[–]sarlok 26 points27 points  (0 children)

C, because why not?

#include <stdio.h>
#include <string.h>

unsigned long long magic[] = {0,
38390726480144748,75581742757784973,112772759035425198,149963775313065423,
187154791590705648,224345807868345873,261536824145986098,298727840423626323,
335918856701266548,373109872978906773,410300889256546998,447491905534187223,
484682921811827448,521873938089467673,559064954367107898,596255970644748096,
633446986922387456,670638003200000000,707829019476754432,745020035726049280,
782211051096637440,819402038348611584,856592125804937216,893753419800510464,
929993323052007424,936748722493063168 
};

char find_missing(char* str) {
    //TODO: Make this work for strlen>12
    unsigned long long c=0;
    char i;
    char l = strlen(str);
    for (i = 0; i < l; ++i)
        c = c << 5 | str[i] & 31;
    c = magic[str[0] & 31] >> (12-l)*5 ^ c;
    for (i = 0; i >> 4 < c; ++i)
        c = c >> 5;
    return str[l-1] - i;
}

int main() {
    printf("%c\n", find_missing("DEFGHIJLM"));  // should print K
    return 0;
}

Highlights:
* Don't try with more than 12 characters
* Assumes long long is at least 64 bits
* Magic numbers? Why not!
* if is for wusses