 # Mittmich's solution

## to Secret Handshake in the C Track

Published at Sep 01 2019 · 0 comments
Instructions
Test suite
Solution

There are 10 types of people in the world: Those who understand binary, and those who don't.

You and your fellow cohort of those in the "know" when it comes to binary decide to come up with a secret "handshake".

``````1 = wink
1000 = jump

10000 = Reverse the order of the operations in the secret handshake.
``````

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the input 3, the function would return the array ["wink", "double blink"] because 3 is 11 in binary.

Given the input 19, the function would return the array ["double blink", "wink"] because 19 is 10011 in binary. Notice that the addition of 16 (10000 in binary) has caused the array to be reversed.

## Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

Bert, in Mary Poppins http://www.imdb.com/title/tt0058331/quotes/qt0437047

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_secret_handshake.c

``````#include "vendor/unity.h"
#include "../src/secret_handshake.h"
#include <stdlib.h>

#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr))

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_commands_wink_for_1(void)
{
const char *expected[] = { "wink" };
const char **actual = commands(1);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

{
TEST_IGNORE();               // delete this line to run test
const char *expected[] = { "double blink" };
const char **actual = commands(2);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_close_your_eyes_for_100(void)
{
TEST_IGNORE();
const char *expected[] = { "close your eyes" };
const char **actual = commands(4);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_jump_for_1000(void)
{
TEST_IGNORE();
const char *expected[] = { "jump" };
const char **actual = commands(8);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_combine_two_actions(void)
{
TEST_IGNORE();
const char *expected[] = { "wink", "double blink" };
const char **actual = commands(3);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_reverse_two_actions(void)
{
TEST_IGNORE();
const char *expected[] = { "double blink", "wink" };
const char **actual = commands(19);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_reversing_one_action_gives_the_same_action(void)
{
TEST_IGNORE();
const char *expected[] = { "jump" };
const char **actual = commands(24);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_reversing_no_actions_still_gives_no_actions(void)
{
TEST_IGNORE();
const char *expected[] = { NULL };
const char **actual = commands(16);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_all_possible_actions(void)
{
TEST_IGNORE();
const char *expected[] =
const char **actual = commands(15);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_reverse_all_possible_actions(void)
{
TEST_IGNORE();
const char *expected[] =
const char **actual = commands(31);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

static void test_commands_do_nothing_for_zero(void)
{
TEST_IGNORE();
const char *expected[] = { NULL };
const char **actual = commands(0);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
free(actual);
}

int main(void)
{
UnityBegin("test/test_secret_handshake.c");

RUN_TEST(test_commands_wink_for_1);
RUN_TEST(test_commands_close_your_eyes_for_100);
RUN_TEST(test_commands_jump_for_1000);
RUN_TEST(test_commands_combine_two_actions);
RUN_TEST(test_commands_reverse_two_actions);
RUN_TEST(test_commands_reversing_one_action_gives_the_same_action);
RUN_TEST(test_commands_reversing_no_actions_still_gives_no_actions);
RUN_TEST(test_commands_all_possible_actions);
RUN_TEST(test_commands_reverse_all_possible_actions);
RUN_TEST(test_commands_do_nothing_for_zero);

return UnityEnd();
}``````

### src/secret_handshake.c

``````#include "secret_handshake.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "math.h"

const char **commands(size_t number){
// allocate target array
const char **output = (const char**) malloc(sizeof(char*) * MAXWORDS);
for(unsigned i = 0; i < MAXWORDS; i++){
output[i] = (const char*) malloc(sizeof(char) * MAXWORDLEN);
}
// check if it worked
if(! output){
return NULL;
}
if((number == 16) || (number == 0)){
output = NULL;
return output;
}
// declare array that holds words at indices that correspond to bit index from the right
char *words[] = { "wink" ,
"jump"};
// set reversed flag
int reversed = 0;
// go through number and chek whether the bits are set
// get bitlength
// define index into output
unsigned index = 0;
size_t bitlength = floor(log2(number)) + 1;
for(unsigned i = 0; i < bitlength; i++){
if( number & 01 ){
if( i == 4){
reversed = 1;
}else{
output[index] = words[i];
index++;
}
}
number >>= 1;
}
// reverse if flag is set
if(reversed){
for(unsigned i = 0; i <= (int) floor((index - 1)/2); i++){
const char *temp = output[i];
output[i] = output[(index)-(i + 1)];
output[(index)-(i + 1)] = temp;
}
return output;
}
return output;
}``````

### src/secret_handshake.h

``````#ifndef SECRET_HANDSHAKE_H
#define SECRET_HANDSHAKE_H

#include <stddef.h>

#define MAXWORDLEN 100
#define MAXWORDS 10

const char **commands(size_t number);

#endif``````