How to Find the Sum of Angles in Golang

0 min read 108 words

The challenge

Find the total sum of internal angles (in degrees) in an n-sided simple polygon.

N will be greater than 2.

The solution in Golang

Option 1:

package solution
func Angle(n int) int {
    return (n - 2) * 180
}

Option 2:

package solution
func Angle(n int) (r int) {
    r = (n-2)*180
    return
}

Option 3:

package solution
func Angle(n int) int {
    return (n/2-1) * 360 + n%2 * 180
}

Test cases to validate our solution

package solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("Basic tests", func() {
    It("Angle(3)", func() { Expect(Angle(3)).To(Equal(180)) })
    It("Angle(4)", func() { Expect(Angle(4)).To(Equal(360)) })
})
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