🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # tokisakikurumi's solution

## to Raindrops in the Objective-C Track

Published at Sep 08 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".

## Setup

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.

## 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.

### RaindropsTest.m

``````#import <XCTest/XCTest.h>

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

@interface RaindropsTest : XCTestCase

@end

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

@end``````

### Raindrops-ObjC/Raindrops-ObjC/Raindrops.h

``````//
//  Raindrop.h
//  Raindrop-ObjC
//
//  Created by tokisakikurumi on 9/6/19.
//  Copyright © 2019 tokisakikurumi. All rights reserved.
//

@interface Raindrops : NSObject
{
int number;
}
-(id)initWithNumber:(int)num;
-(NSString *)sounds;
@end``````

### Raindrops-ObjC/Raindrops-ObjC/Raindrops.m

``````//
//  Raindrops.m
//  Raindrop-ObjC
//
//  Created by tokisakikurumi on 9/6/19.
//  Copyright © 2019 tokisakikurumi. All rights reserved.
//

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

@implementation Raindrops
-(id)initWithNumber:(int)num;
{
number = num;
return self;
}

-(NSString *)sounds;
{
NSString * str = @"";
NSDictionary * dict = @{@"3": @"Pling", @"5": @"Plang", @"7": @"Plong"};

int real_key;
for(NSString * key in dict)
{
id value = dict[key];
real_key = [key intValue];
if(number % real_key == 0) {
str = [NSString stringWithFormat:@"%@%@",str,value];
}
}

if([str length] == 0) {
str = [NSString stringWithFormat:@"%d",number];
}

return str;
}
@end

int main(int argc, const char * argv[]) {
@autoreleasepool {
}
return 0;
}``````

## 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?