Wednesday, February 21, 2024
HomeSoftware EngineeringCalculate the Third Angle of a Triangle in Go

Calculate the Third Angle of a Triangle in Go


The problem

You’re given two inside angles (in levels) of a triangle.

Write a perform to return the third.

Word: solely optimistic integers will probably be examined.

The answer in Golang

Choice 1:

bundle resolution

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

Choice 2:

bundle resolution

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

Choice 3:

bundle resolution

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 instances to validate our resolution

bundle check

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

var _ = Describe("Fundamental Checks", 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)) })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments