🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of eljueves

eljueves's solution

to Raindrops in the Objective-C Track

Published at Jun 11 2020 · 0 comments
Test suite

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.


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


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/raindrops/Raindrops.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


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.


#import <XCTest/XCTest.h>

#if __has_include("RaindropsExample.h")
# import "RaindropsExample.h"
# else
# import "Raindrops.h"

@interface RaindropsTest : XCTestCase


@implementation RaindropsTest

- (void)test1 {
    XCTAssertEqualObjects(@"1", [[[Raindrops alloc] initWithNumber:1] sounds]);

- (void)test3 {
    XCTAssertEqualObjects(@"Pling", [[[Raindrops alloc] initWithNumber:3] sounds]);

- (void)test5 {
    XCTAssertEqualObjects(@"Plang", [[[Raindrops alloc] initWithNumber:5] sounds]);

- (void)test7 {
    XCTAssertEqualObjects(@"Plong", [[[Raindrops alloc] initWithNumber:7] sounds]);

- (void)test6 {
    XCTAssertEqualObjects(@"Pling", [[[Raindrops alloc] initWithNumber:6] sounds]);

- (void)test9 {
    XCTAssertEqualObjects(@"Pling", [[[Raindrops alloc] initWithNumber:9] sounds]);

- (void)test10 {
    XCTAssertEqualObjects(@"Plang", [[[Raindrops alloc] initWithNumber:10] sounds]);

- (void)test14 {
    XCTAssertEqualObjects(@"Plong", [[[Raindrops alloc] initWithNumber:14] sounds]);

- (void)test15 {
    XCTAssertEqualObjects(@"PlingPlang", [[[Raindrops alloc] initWithNumber:15] sounds]);

- (void)test21 {
    XCTAssertEqualObjects(@"PlingPlong", [[[Raindrops alloc] initWithNumber:21] sounds]);

- (void)test25 {
    XCTAssertEqualObjects(@"Plang", [[[Raindrops alloc] initWithNumber:25] sounds]);

- (void)test35 {
    XCTAssertEqualObjects(@"PlangPlong", [[[Raindrops alloc] initWithNumber:35] sounds]);

- (void)test49 {
    XCTAssertEqualObjects(@"Plong", [[[Raindrops alloc] initWithNumber:49] sounds]);

- (void)test52 {
    XCTAssertEqualObjects(@"52", [[[Raindrops alloc] initWithNumber:52] sounds]);

- (void)test105 {
    XCTAssertEqualObjects(@"PlingPlangPlong", [[[Raindrops alloc] initWithNumber:105] sounds]);

- (void)test12121 {
    XCTAssertEqualObjects(@"12121", [[[Raindrops alloc] initWithNumber:12121] sounds]);



#import <Foundation/Foundation.h>

@interface Raindrops : NSObject

-(instancetype)initWithNumber: (int) drops;
-(NSString *)sounds;



#import <Foundation/Foundation.h>
#import "Raindrops.h"

@implementation Raindrops {
    int drops;

-(instancetype) initWithNumber: (int) drops {
    self = [super init];
    if (self) {
        self->drops = drops;
    return self;

-(NSString *)sounds {
    NSMutableString* result = [[NSMutableString alloc] init];
    if (drops % 3 == 0) [result appendString:@"Pling"];
    if (drops % 5 == 0) [result appendString:@"Plang"];
    if (drops % 7 == 0) [result appendString:@"Plong"];
    if (result.length != 0) return result;
    return [NSString stringWithFormat:@"%d", drops];


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?