Calculate the Third Angle of a Triangle in Go

[ad_1]

The problem

You might be given two inside angles (in levels) of a triangle.

Write a operate to return the third.

Be aware: solely optimistic integers will likely be examined.

The answer in Golang

Possibility 1:

package deal answer

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

Possibility 2:

package deal answer

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

Possibility 3:

package deal answer

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
}

Take a look at circumstances to validate our answer

package deal check

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

var _ = Describe("Fundamental Exams", func() {
    It("OtherAngle(30, 60)", func() { Count on(OtherAngle(30, 60)).To(Equal(90)) })
    It("OtherAngle(60, 60)", func() { Count on(OtherAngle(60, 60)).To(Equal(60)) })
    It("OtherAngle(43, 78)", func() { Count on(OtherAngle(43, 78)).To(Equal(59)) })
    It("OtherAngle(10, 20)", func() { Count on(OtherAngle(10, 20)).To(Equal(150)) })
})

[ad_2]

Leave a Reply