Skip to content

Instantly share code, notes, and snippets.

@Mitame
Created March 25, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mitame/1c47759a680a54759039 to your computer and use it in GitHub Desktop.
Save Mitame/1c47759a680a54759039 to your computer and use it in GitHub Desktop.
// challenge: https://s.mita.me/baag
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int conv_from_nos(const char nos_num[]) {
// nos_num: the NOS encoded number ending with a null byte
size_t nos_len = strlen(nos_num);
int result = 0;
for (unsigned int i = 0; i < nos_len; i++) {
if (nos_num[i] == '0')
result += i + 1;
else if (nos_num[i] == '1')
result -= i + 1;
else if (nos_num[i] == '2')
result *= i + 1;
else {
printf("%c\n", nos_num[i]);
printf("%s is not a valid NOS encoded number.\n", nos_num);
return -1;
}
}
return result;
}
int main(int argc, char const *argv[]) {
if (argc != 2){
printf("Syntax: `%s <NOS encoded number>'\n", argv[0]);
exit(-1);
}
int result = conv_from_nos(argv[1]);
printf("Result: %i\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment