Avatar of jhlink

jhlink's solution

to Raindrops in the C Track

Published at Aug 15 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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 "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("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.c

#include "raindrops.h"

char *convert(char result[], int drops) {
  int char_buff_pos = 0;

  if ( drops % 3 == 0 ) {
    char_buff_pos += sprintf(result, "%s", THREE_PLING);
  } 
  
  if ( drops % 5 == 0 ) {
    char_buff_pos += sprintf(result + char_buff_pos, "%s", FIVE_PLANG);
  } 

  if ( drops % 7 == 0 ) {
    char_buff_pos += sprintf(result + char_buff_pos, "%s", SEVEN_PLONG);
  } 
  
  if ( drops % 3 != 0 && drops % 5 != 0 && drops % 7 != 0 ) {
    sprintf(result, "%d", drops);
  }

  return result;
}

src/raindrops.h

#ifndef RAINDROPS_H
#define RAINDROPS_H

#include <stdio.h>

static const char THREE_PLING[] = "Pling";
static const char FIVE_PLANG[] = "Plang";
static const char SEVEN_PLONG[] = "Plong";

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

#endif

Community comments

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

jhlink's Reflection

I've opted to manually track the buffer position because of the gross inefficiencies of strcat. Beyond that, everything is pretty simple!