diff --git a/solutions/c/secret-handshake/1/secret_handshake.c b/solutions/c/secret-handshake/1/secret_handshake.c new file mode 100644 index 0000000..485e6ac --- /dev/null +++ b/solutions/c/secret-handshake/1/secret_handshake.c @@ -0,0 +1,38 @@ +#include "secret_handshake.h" +#include +#include +#include + +#define MAX_COMMAND 4 + +static const char *all_commands[] = {"wink", "double blink", "close your eyes", "jump"}; + +const char **commands(size_t number) { + int binary_seq[MAX_COMMAND + 1]; + int binary_count = 0; + int reverse_seq = 0; + while (number > 0) { + binary_seq[binary_count++] = number % 2; + number /= 2; + } + if (binary_count > MAX_COMMAND) { + reverse_seq = 1; + binary_count--; + } + const char **final_commands = calloc(MAX_COMMAND, sizeof(char*)); + int command_count = 0; + if (reverse_seq) { + for (int i = binary_count-1; i >= 0; i--) { + if (binary_seq[i]) { + final_commands[command_count++] = all_commands[i]; + } + } + } else { + for (int i = 0; i < binary_count; i++) { + if (binary_seq[i]) { + final_commands[command_count++] = all_commands[i]; + } + } + } + return final_commands; +} \ No newline at end of file diff --git a/solutions/c/secret-handshake/1/secret_handshake.h b/solutions/c/secret-handshake/1/secret_handshake.h new file mode 100644 index 0000000..29f8e88 --- /dev/null +++ b/solutions/c/secret-handshake/1/secret_handshake.h @@ -0,0 +1,8 @@ +#ifndef SECRET_HANDSHAKE_H +#define SECRET_HANDSHAKE_H + +#include + +const char **commands(size_t number); + +#endif