Avatar of jrbarbati

jrbarbati's solution

to Raindrops in the C Track

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

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 "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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

int main(void)
{
   UnityBegin("test/test_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);

   return UnityEnd();
}

src/raindrops.h

#ifndef RAINDROPS_H
#define RAINDROPS_H

char *convert(char result[], int drops);

#endif

src/raindrops.c

#include "raindrops.h"
#include <stdio.h>
#include <string.h>

const int factors[] = {3, 5, 7};
const char *raindrops[] = {"Pling", "Plang", "Plong"};

char *convert(char result[], int drops)
{
	for (int i = 0; i < 3; i++)
		if (drops % factors[i] == 0)
			sprintf(result + strlen(result), "%s", raindrops[i]);

	if (strlen(result) == 0)
		sprintf(result, "%d", drops);

	return result;
}

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?