 shmibs's solution

to Raindrops in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a number to a string, the contents of which depend on the number's factors.

• If the number has 3 as a factor, output 'Pling'.
• If the number has 5 as a factor, output 'Plang'.
• If the number has 7 as a factor, output 'Plong'.
• If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

Examples

• 28's factors are 1, 2, 4, 7, 14, 28.
• In raindrop-speak, this would be a simple "Plong".
• 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
• In raindrop-speak, this would be a "PlingPlang".
• 34 has four factors: 1, 2, 17, and 34.
• In raindrop-speak, this would be "34".

Getting Started

Make sure you have read the C page 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

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

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

test_raindrops.c

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

#define BUFFER_LENGTH 16

void setUp(void)
{
}

void tearDown(void)
{
}

void convert_drops(int drops, char *expected)
{
char result[BUFFER_LENGTH] = { 0 };
convert(result, drops);
TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_one_yields_itself(void)
{
convert_drops(1, "1");
}

void test_three_yields_pling(void)
{
TEST_IGNORE();               // delete this line to run test
convert_drops(3, "Pling");
}

void test_five_yields_plang(void)
{
TEST_IGNORE();
convert_drops(5, "Plang");
}

void test_seven_yields_plong(void)
{
TEST_IGNORE();
convert_drops(7, "Plong");
}

void test_six_yields_pling(void)
{
TEST_IGNORE();
convert_drops(6, "Pling");
}

void test_nine_yields_pling(void)
{
TEST_IGNORE();
convert_drops(9, "Pling");
}

void test_ten_yields_plang(void)
{
TEST_IGNORE();
convert_drops(10, "Plang");
}

void test_fourteen_yields_plong(void)
{
TEST_IGNORE();
convert_drops(14, "Plong");
}

void test_fifteen_yields_plingplang(void)
{
TEST_IGNORE();
convert_drops(15, "PlingPlang");
}

void test_twenty_one_yields_plingplong(void)
{
TEST_IGNORE();
convert_drops(21, "PlingPlong");
}

void test_twenty_five_yields_plang(void)
{
TEST_IGNORE();
convert_drops(25, "Plang");
}

void test_thirty_five_yields_plangplong(void)
{
TEST_IGNORE();
convert_drops(35, "PlangPlong");
}

void test_forty_nine_yields_plong(void)
{
TEST_IGNORE();
convert_drops(49, "Plong");
}

void test_fifty_two_yields_itself(void)
{
TEST_IGNORE();
convert_drops(52, "52");
}

void test_one_hundred_five_yields_plingplangplong(void)
{
TEST_IGNORE();
convert_drops(105, "PlingPlangPlong");
}

void test_big_prime_yields_itself(void)
{
TEST_IGNORE();
convert_drops(12121, "12121");
}

int main(void)
{
UnityBegin("raindrops.c");

RUN_TEST(test_one_yields_itself);
RUN_TEST(test_three_yields_pling);
RUN_TEST(test_five_yields_plang);
RUN_TEST(test_seven_yields_plong);
RUN_TEST(test_six_yields_pling);
RUN_TEST(test_nine_yields_pling);
RUN_TEST(test_ten_yields_plang);
RUN_TEST(test_fourteen_yields_plong);
RUN_TEST(test_fifteen_yields_plingplang);
RUN_TEST(test_twenty_one_yields_plingplong);
RUN_TEST(test_twenty_five_yields_plang);
RUN_TEST(test_thirty_five_yields_plangplong);
RUN_TEST(test_forty_nine_yields_plong);
RUN_TEST(test_fifty_two_yields_itself);
RUN_TEST(test_one_hundred_five_yields_plingplangplong);
RUN_TEST(test_big_prime_yields_itself);

UnityEnd();
return 0;
}
#include <stdio.h>

#include "raindrops.h"

#define TRY_INSERT(SN, N) \
do { \
if(n % (N) == 0) { \
if(i + sizeof((SN))/sizeof(char) > blen) \
return NULL; \
sprintf(b + i, "%s", (SN)); \
i += sizeof((SN))/sizeof(char) - 1; \
} \
} while(0)

char* convert(char* b, unsigned blen, unsigned n)
{
const char s1[] = "Pling";
const char s2[] = "Plang";
const char s3[] = "Plong";

unsigned i;
unsigned nlen;

i = 0;
b = '\0';

TRY_INSERT(s1, 3);
TRY_INSERT(s2, 5);
TRY_INSERT(s3, 7);

if(b == '\0') {
i = n;
nlen = 1;
while(i /= 10) nlen++;

if(nlen >= blen)
return NULL;

sprintf(b, "%u\0", n);
}

return b;
}