Avatar of jrbarbati

jrbarbati's solution

to Secret Handshake in the C Track

Published at Aug 22 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
10 = double blink
100 = close your eyes
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[0]))

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);
}

static void test_commands_double_blink_for_10(void)
{
   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[] =
       { "wink", "double blink", "close your eyes", "jump" };
   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[] =
       { "jump", "close your eyes", "double blink", "wink" };
   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_double_blink_for_10);
   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.h

#ifndef SECRET_HANDSHAKE_H
#define SECRET_HANDSHAKE_H

#include <stddef.h>

const char **commands(size_t number);

#endif

src/secret_handshake.c

#include "secret_handshake.h"
#include <stdlib.h>

size_t calc_needed_size(size_t number);
const char **reverse(const char **string_array);
void swap(const char **string_array, int index1, int index2);

const int values[4] = {1, 2, 4, 8};
const char *moves[4] = {"wink", "double blink", "close your eyes", "jump"};

const char **commands(size_t number)
{
	const char **secret_handshake = calloc(calc_needed_size(number), sizeof(char *));
	int moveCounter = 0, i;

	for (i = 0; i < 4; i++)
		if ((number & values[i]) > 0)
			secret_handshake[moveCounter++] = moves[i];

	if ((number & 16) > 0) 
		return reverse(secret_handshake);

	return secret_handshake;
}

size_t calc_needed_size(size_t number)
{
	return (number & 1) + ((number >> 1) & 1) + ((number >> 2) & 1) + ((number >> 3) & 1);
}

const char **reverse(const char **string_array)
{
	int i, j;

	for (i = 0, j = 3; i < j; i++, j--)
		if (string_array[j])
			swap(string_array, i, j);
		else
			i--;

	return string_array;
}

void swap(const char **string_array, int index1, int index2)
{
	const char *temp = string_array[index1];
	string_array[index1] = string_array[index2];
	string_array[index2] = temp;
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?