Created
March 25, 2016 19:25
-
-
Save Mitame/1c47759a680a54759039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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