Calculate the Third Angle of a Triangle in Go

0 min read 155 words

The challenge

You are given two interior angles (in degrees) of a triangle.

Write a function to return the 3rd.

Note: only positive integers will be tested.

The solution in Golang

Option 1:

package solution

func OtherAngle(a int, b int) int {
    return 180-a-b
}

Option 2:

package solution

func OtherAngle(a int, b int) int {
  if a + b > 180{
    return -1
  }
  return 180 - a - b
}

Option 3:

package solution

import "math"

func OtherAngle(a int, b int) int {
  const fullNumber = 180
  var thirdNumber int = int(float64(fullNumber) - (math.Abs(float64(a)) + math.Abs(float64(b))))
  return thirdNumber
}

Test cases to validate our solution

package test

import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
  . "my/<meta charset="utf-8">solution"
)

var _ = Describe("Basic Tests", func() {
    It("OtherAngle(30, 60)", func() { Expect(OtherAngle(30, 60)).To(Equal(90)) })
    It("OtherAngle(60, 60)", func() { Expect(OtherAngle(60, 60)).To(Equal(60)) })
    It("OtherAngle(43, 78)", func() { Expect(OtherAngle(43, 78)).To(Equal(59)) })
    It("OtherAngle(10, 20)", func() { Expect(OtherAngle(10, 20)).To(Equal(150)) })
})
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts