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

stevenhuey'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.m
//  Triangle
//
//  Created by Steve Huey on 6/12/17.
//  Copyright © 2017 Steve Huey. All rights reserved.
//

#import "Triangle.h"

@interface Triangle ()
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat z;
@property (nonatomic, readonly) BOOL isValid;

- (instancetype)initWithX:(CGFloat)x y:(CGFloat)y z:(CGFloat)z;
- (BOOL)isEquilateral;
- (BOOL)isIsosceles;
@end

@implementation Triangle
+ (TriangleKind)kindForSides:(CGFloat)x :(CGFloat)y :(CGFloat)z
{
   TriangleKind kind = TriangleKindInvalid;
   
   Triangle* t = [[Triangle alloc] initWithX:x y:y z:z];
   
   if (t.isValid)
   {
      if ([t isEquilateral])
      {
         kind = TriangleKindEquilateral;
      }
      else if ([t isIsosceles])
      {
         kind = TriangleKindIsosceles;
      }
      else
      {
         kind = TriangleKindScalene;
      }
   }
   else
   {
      @throw [NSException exceptionWithName:@"InvalidTriangleException"
                                     reason:nil
                                   userInfo:nil];
   }
   
   return kind;
}

- (instancetype)initWithX:(CGFloat)x y:(CGFloat)y z:(CGFloat)z
{
   self = [super init];
   
   if (self)
   {
      _x = x;
      _y = y;
      _z = z;
   }
   
   return self;
}

- (BOOL)isValid
{
   return ((self.x > 0.0 && self.y > 0.0 && self.z > 0.0) &&
           (self.x <= self.y + self.z && self.z <= self.x + self.y));
}

- (BOOL)isEquilateral
{
   return (self.x == self.y) && (self.y == self.z);
}

- (BOOL)isIsosceles
{
   return (self.x == self.y) || (self.x == self.z) || (self.y == self.z);
}


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