Bronze/Silver Challenge


#1

Enums seem pretty straightforward, interesting how the recursive typing is implemented. I’m just putting the entire ShapeDimensions enum because it covers both challenges:

Bronze & Silver Challenge:

enum ShapeDimensions {
    case point
    case square(side: Double)
    case rectangle(width: Double, height: Double)
    case rightTriangle(legA: Double, legB: Double)

    func area() -> Double {
        switch self {
            case .point:
                return 0
            case let .square(side:side):
                return side * side
            case let .rectangle(width:w, height:h):
                return w * h
            case let .rightTriangle(legA:a, legB:b):
                return (a * b) / 2
        }
    }

    func perimeter() -> Double {
        switch self {
            case .point:
                return 0
            case let .square(side:side):
                return 4 * side
            case let .rectangle(width:w, height:h):
                return 2 * w + 2 * h
            case let .rightTriangle(legA:a, legB:b): 
                return a + b + sqrt(a * a + b * b) // computing the hypotenuse automatically, no need to define in case
        }
    }
}

var rightTriableShape = ShapeDimenstions.rightTriangle(legA: 1, legB: 1)

I decided to compute the hypotenuse automatically in the perimeter function and not have it specified in the enum type simply because it’s easier and less error-prone. legA and legB are as follows:


#2

I used a slightly different method for the square root:

>     >      case let .rightTriangle(width: w, height: h):
>     >                 return (w * w + h * h).squareRoot() + w + h

#3

Bronze Challenge:

enum ShapeDimensions {
    // square's associated value is the length of one side
    case square(side: Double)
    
    //rectangle's associated value defines its width and height
    case rectangle(width: Double, height: Double)
    
    //point.  A point has no dimensions
    case point
    
    func area() -> Double {
        switch self {
        case .point:
            return 0
        case let .square(side: side):
            return side * side
            
        case let .rectangle(width: w, height: h):
            return w * h
        }
    }
    func perimeter() -> Double {
        switch self {
        case .point:
            return 0
        case let .square(side: side):
            return side * 4
        case let .rectangle(width: w, height: h):
            return (h * 2) + (w * 2)
        }
    }
}

var aRect = ShapeDimensions.rectangle(width: 50, height: 40)
print ("My rectangle has a perimeter of \(aRect.perimeter()).")

var aSquare = ShapeDimensions.square(side: 101)
print ("My rectangle has a perimeter of \(aSquare.perimeter()).")

#4

Silver challenge code:

enum ShapeDimensions {
    // square's associated value is the length of one side
    case square(side: Double)
    
    //rectangle's associated value defines its width and height
    case rectangle(width: Double, height: Double)
    
    //point.  A point has no dimensions
    case point
    
    //right triangle
    case rightTriangle(base: Double, height: Double)
    
    func area() -> Double {
        switch self {
        case .point:
            return 0
        case let .square(side: side):
            return side * side
        case let .rectangle(width: w, height: h):
            return w * h
        case let .rightTriangle(base: b, height: h):
            return (b*h)/2
        }
    }
    func perimeter() -> Double {
        switch self {
        case .point:
            return 0
        case let .square(side: side):
            return side * 4
        case let .rectangle(width: w, height: h):
            return (h * 2) + (w * 2)
        case let .rightTriangle(base: b, height: h):
            return ((b * b) + (h * h)).squareRoot()
        }
    }
}

var rTriangle = ShapeDimensions.rightTriangle(base: 20, height: 10)
print("My right triangle has a perimeter of \(rTriangle.perimeter()).")
print("My right triangle has an area of \(rTriangle.area()).")

#5

this was my solution to the silver and bronze challenge for any given triangle (not just the right angled)

enum ShapeDimentions {
    //point has no associated values, it is dimentionless
    case point
    //square's associated value is the length of one side
    case square(side: Double)
    //rectangle's associated value defines its width and the height
    case rectangle(width: Double, height: Double)
    //triangle
    case triangle(side1: Double, side2: Double, side3: Double)
    
    func area()->Double {
        switch self {
        case .point:
            return 0
        case let .square(side: side):
            return side * side
        case let .rectangle(width: w, height: h):
            return w * h
        case let .triangle(side1: a, side2: b, side3: c):
            let p = self.perimeter() / 2
            return sqrt(p * (p - a) * (p - b) * (p - c))
        }
    }
    
    func perimeter()-> Double {
        switch self {
        case .point:
            return 0
        case let .square(side: side):
            return 4 * side
        case let .rectangle(width: w, height: h):
            return 2 * (w + h)
        case let .triangle(side1: s1, side2: s2, side3: s3):
            return s1 + s2 + s3
        }
    }
}

var squareShape = ShapeDimentions.square(side: 10.0)
var rectShape = ShapeDimentions.rectangle(width: 5.0, height: 10.0)
var pointShape = ShapeDimentions.point
var triangleShape = ShapeDimentions.triangle(side1: 3, side2: 5, side3: 4)

#6