u/MadCamel - 2 Archived Voat Posts in v/programming
u/MadCamel
  • home
  • search

u/MadCamel

0 posts · 2 comments · 2 total

Active in: v/programming (2)

  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››
Comment on: We need more programming challenges. We should start off small: First non-repeating character of a string. Any language you like.

Thanks for giving it a try! I thought I was completely buried down here. That's proper behaviour given the specification as I understand it.

If I were asked to re-phrase the specification: Print the first character that isn't a repeat of the last.

AAAAABC - A is repeated, B is first non-repeating char
ABA - A doesn't repeat, B is first non-repeating char
ABBBBBBBBC - A doesn't repeat. B is the first non-repeating char
A - A doesn't repeat..
1 16 Apr 2016 15:20 u/MadCamel in v/programming
Comment on: We need more programming challenges. We should start off small: First non-repeating character of a string. Any language you like.

Perl:

#!/usr/bin/perl -n
s/(.)(\1)+//; printf("%s\n", (split(//))[0]);

C. Pointer math is fun.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
  char buf[512], *p, fc;
  while (read(0, &buf, 512) > 0) {
    for (p=buf, fc = *p; *p == fc && *p != 0; p++);
    if (p-1 == buf) // Single character edge-case
      printf("%c\n", fc);
    else
      printf("%c\n", *p);
  }
}
4 16 Apr 2016 07:20 u/MadCamel in v/programming
  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››

archive has 9,592 posts and 65,719 comments. source code.