Discussion
Loading...

Post

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Kevin Lyda
@lyda@mastodon.ie  ·  activity timestamp 3 months ago

This was fun - missing.c:

#include
#include

void die(int err, char *msg) {
err? fprintf(stderr, "%s\n", msg), exit(1): 0;
}

int main(int argc, char *argv[]) {
int n, answer = 1, val;

die(argc != 2 || (n = atoi(argv[1])) < 2, "Usage: missing n");
for (; n > 1; n--) {
die(scanf("%d", &val) != 1, "Error reading input");
answer ^= val ^ n;
}
printf("%d\n", answer);
}

  • Copy link
  • Flag this post
  • Block
Kevin Lyda
@lyda@mastodon.ie replied  ·  activity timestamp 3 months ago

Test with:

seq 1 100 | shuf > /tmp/missing.dat
cc -o missing missing.c
&& for i in $(seq 1 100); do
if [[ $(sed -n ${i}p /tmp/missing.dat) != $(sed ${i}d /tmp/missing.dat | ./missing 100) ]]; then
echo -n "[$i wrong]"
else
echo -n .
fi
done
echo

  • Copy link
  • Flag this comment
  • Block
Kevin Lyda
@lyda@mastodon.ie replied  ·  activity timestamp 3 months ago

And yes, I'm insecurely using atoi and scanf. The alternatives used too many characters for a mastodon post.

  • Copy link
  • Flag this comment
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.0-rc.3.5 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login