Greplin Programming Challenge spoilers below the cut.
Now I feel a little bit dumb. I spent like half an hour writing (and debugging, yes) this C for level 1 of the
Greplin Programming Challenge:
#include
#include
int main ()
{
char *s = "Fourscore...earth";
int longestLen = 0;
char *longestSeq = NULL;
char *si = s;
char *last = s;
while (*last != '\0') last++;
while (*si != '\0') {
for (int len = last - si; len > 0; len--) {
char *seqStart = si;
char *rsi = si + len;
char *fsi = si;
while ((*rsi == *fsi) && (rsi > fsi)) {
rsi--;
fsi++;
}
if (rsi <= fsi && len > longestLen) {
longestLen = len;
longestSeq = seqStart;
}
}
si++;
}
for (char *c = longestSeq; c <= longestSeq + longestLen; c++) {
printf ("%c", *c);
}
puts ("");
}
After doing the other two problems, I go to wash the dishes... and then slap my forehead, come back, and mash out the obvious Haskell solution:
maximumBy (comparing length) . filter (\s -> s == reverse s) . concatMap tails . inits
Problem #2 was pretty trivial, especially when you can't be bothered to implement a primeness predicate and just pick one up from code you'd written for Project Euler. #3 ended up really simple in Haskell too, though I ended up spending like ten minutes looking for bugs in it... only to find out I'd messed up copypasting the input *facepalm*:
let accept xs = case sortBy (flip compare) xs of [] -> False; (l:sxs) -> l == sum sxs
in length . filter accept . filterM (const [True, False])