Software Engineering

Learn how to Sum the Common for NBA Gamers in Golang

Learn how to Sum the Common for NBA Gamers in Golang
Written by admin


The problem

Write a operate, referred to asĀ sumPPG, that takes two NBA participant objects/struct/Hash/Dict/Class and sums their PPG

Examples:

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

The answer in Golang

Choice 1:

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

Choice 2:

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

Take a look at 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{ Crew: "76ers", Ppg: 11.2 }
     jordan  := NBAPlayer{ Crew: "bulls", Ppg: 20.2 }
     Anticipate(SumPpg(iverson,jordan)).To(BeNumerically("~",31.4,tol))
   })
  It("Customary checks", func(){
    player1 := NBAPlayer{ Crew: "p1_team", Ppg: 20.2   }
    player2 := NBAPlayer{ Crew: "p2_team", Ppg: 2.6    }
    player3 := NBAPlayer{ Crew: "p3_team", Ppg: 2023.2 }
    player4 := NBAPlayer{ Crew: "p4_team", Ppg: 0      }
    player5 := NBAPlayer{ Crew: "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))
  })
})

About the author

admin

Leave a Comment