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

6401's solution

to Raindrops in the Objective-C Track

Published at Feb 15 2020 · 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/Raindrops_Tests/Raindrops.m

//
//  Raindrops.m
//  Raindrops_Tests
//
//  Created by Сергей Пупчик on 15/02/2020.
//  Copyright © 2020 Сергей Пупчик. All rights reserved.
//

#import "Raindrops.h"

@implementation Raindrops
-(id)initWithNumber:(int)number{
    self = [super init];
    _sound = [[NSMutableString alloc]init];
    _drops = number;
    return self;
}
-(NSString*)sounds{
    if(_drops % 3 == 0){[_sound appendString:@"Pling"];}
    if(_drops % 5 == 0){[_sound appendString:@"Plang"];}
    if(_drops % 7 == 0){[_sound appendString:@"Plong"];}
    if([_sound isEqualToString:@""]) {[_sound appendFormat:@"%i",_drops];}
    return _sound;
}
@end

Raindrops/Raindrops_Tests/Raindrops.h

//
//  Raindrops.h
//  Raindrops_Tests
//
//  Created by Сергей Пупчик on 15/02/2020.
//  Copyright © 2020 Сергей Пупчик. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Raindrops : NSObject
@property NSMutableString* sound;
@property int drops;
-(id)initWithNumber:(int)number;
-(NSString*)sounds;
@end

NS_ASSUME_NONNULL_END

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?