Thursday, February 29, 2024
HomeSoftware EngineeringThe best way to Sum the Common for NBA Gamers in Golang

The best way to Sum the Common for NBA Gamers in Golang


The problem

Write a perform, known as sumPPG, that takes two NBA participant objects/struct/Hash/Dict/Class and sums their PPG

Examples:

iverson := NBAPlayer{ Staff: "76ers", Ppg: 11.2 }
jordan  := NBAPlayer{ Staff: "bulls", Ppg: 20.2 }
SumPpg(iverson, jordan) // => 31.4

The answer in Golang

Possibility 1:

bundle answer
sort NBAPlayer struct {
  Staff string
  Ppg  float64
}
func SumPpg(playerOne, playerTwo NBAPlayer) float64 {
  return playerOne.Ppg+playerTwo.Ppg  
}

Possibility 2:

bundle answer
sort NBAPlayer struct {
  Staff string
  Ppg  float64
}
func SumPpg(a, b NBAPlayer) float64 {
  return float64(a.Ppg + b.Ppg)
}

Check circumstances to validate our answer

bundle solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
const tol=1e-6
var _ = Describe("Fastened checks", func() {
   It("Iverson and jordan", func() {
     iverson := NBAPlayer{ Staff: "76ers", Ppg: 11.2 }
     jordan  := NBAPlayer{ Staff: "bulls", Ppg: 20.2 }
     Anticipate(SumPpg(iverson,jordan)).To(BeNumerically("~",31.4,tol))
   })
  It("Normal checks", func(){
    player1 := NBAPlayer{ Staff: "p1_team", Ppg: 20.2   }
    player2 := NBAPlayer{ Staff: "p2_team", Ppg: 2.6    }
    player3 := NBAPlayer{ Staff: "p3_team", Ppg: 2023.2 }
    player4 := NBAPlayer{ Staff: "p4_team", Ppg: 0      }
    player5 := NBAPlayer{ Staff: "p5_team", Ppg: -5.8   }
    Anticipate(SumPpg(player1, player2)).To(BeNumerically("~",22.8      ,tol))
    Anticipate(SumPpg(player3, player1)).To(BeNumerically("~",2043.4    ,tol))
    Anticipate(SumPpg(player3, player4)).To(BeNumerically("~",2023.2    ,tol))
    Anticipate(SumPpg(player4, player5)).To(BeNumerically("~",-5.8      ,tol))
    Anticipate(SumPpg(player5, player2)).To(BeNumerically("~",2.6 - 5.8 ,tol))
  })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments