ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# KiligWY's solution

## to Triangle in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

## 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/triangle/Triangle.m
``````

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

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### TriangleTest.m

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

#if __has_include("TriangleExample.h")
# import "TriangleExample.h"
# else
# import "Triangle.h"
#endif

@interface TriangleTest : XCTestCase

@end

@implementation TriangleTest

- (void)testEquilateralTrianglesHaveEqualSides {
XCTAssertEqual(TriangleKindEquilateral, [Triangle kindForSides:2 :2 :2]);
}

- (void)testLargerEquilateralTrianglesAlsoHaveEqualSides {
XCTAssertEqual(TriangleKindEquilateral, [Triangle kindForSides:10 :10 :10]);
}

- (void)testIsoscelesTrianglesHaveLastTwoSidesEqual {
XCTAssertEqual(TriangleKindIsosceles, [Triangle kindForSides:3 :4 :4]);
}

- (void)testIsoscelesTrianglesHaveFirstAndLastSidesEqual {
XCTAssertEqual(TriangleKindIsosceles, [Triangle kindForSides:4 :3 :4]);
}

- (void)testIsoscelesTrianglesHaveTwoFirstSidesEqual {
XCTAssertEqual(TriangleKindIsosceles, [Triangle kindForSides:4 :4 :3]);
}

- (void)testIsoscelesTrianglesHaveInFactExactlyTwoSidesEqual {
XCTAssertEqual(TriangleKindIsosceles, [Triangle kindForSides:10 :10 :2]);
}

- (void)testScaleneTrianglesHaveNoEqualSides {
XCTAssertEqual(TriangleKindScalene, [Triangle kindForSides:3 :4 :5]);
}

- (void)testScaleneTrianglesHaveNoEqualSidesAtALargerScaleToo {
XCTAssertEqual(TriangleKindScalene, [Triangle kindForSides:10 :11 :12]);
}

- (void)testScaleneTrianglesHaveNoEqualSidesInDescendingOrderEither {
XCTAssertEqual(TriangleKindScalene, [Triangle kindForSides:5 :4 :2]);
}

- (void)testVerySmallTrianglesAreLegal {
XCTAssertEqual(TriangleKindScalene, [Triangle kindForSides:0.4 :0.6 :0.3]);
}

- (void)testTrianglesWithNoSizeAreIllegal {
XCTAssertThrows([Triangle kindForSides:0 :0 :0]);
}

- (void)testTrianglesWithNegativeSidesAreIllegal {
XCTAssertThrows([Triangle kindForSides:3 :4 :-5]);
}

- (void)testTrianglesViolatingTriangleInequalityAreIllegal {
XCTAssertThrows([Triangle kindForSides:1 :1 :3]);
}

- (void)testTrianglesViolatingTriangleInequalityAreIllegal3 {
XCTAssertThrows([Triangle kindForSides:7 :3 :2]);
}

@end``````

### Triangle.h

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

typedef NS_ENUM(NSUInteger, TriangleKind) {
TriangleKindEquilateral,
TriangleKindIsosceles,
TriangleKindScalene,
};

@interface Triangle : NSObject

+ (TriangleKind)kindForSides:(float)side1 :(float)side2 :(float)side3;

@end``````

### Triangle.m

``````#import "Triangle.h"

@implementation Triangle

+ (TriangleKind)kindForSides:(float)side1 :(float)side2 :(float)side3 {
NSAssert(side1 > 0, @"sides have to be of length > 0");
NSAssert(side2 > 0, @"sides have to be of length > 0");
NSAssert(side3 > 0, @"sides have to be of length > 0");
NSAssert(side1 + side2 > side3, @"not a triangle");
NSAssert(side1 + side3 > side2, @"not a triangle");
NSAssert(side2 + side3 > side1, @"not a triangle");
if (side1 == side2 && side2 == side3) {
return TriangleKindEquilateral;
} else if (side1 == side2 || side2 == side3 || side1 == side3) {
return TriangleKindIsosceles;
} else {
return TriangleKindScalene;
}
}

@end``````