Tuesday, April 23, 2024
HomeSoftware EngineeringThe way to Dry Potatoes in Golang

The way to Dry Potatoes in Golang


The problem

All we eat is water and dry matter.

John purchased potatoes: their weight is 100 kilograms. Potatoes include water and dry matter.

The water content material is 99 p.c of the overall weight. He thinks they’re too moist and places them in an oven – at low temperature – for them to lose some water.

On the output the water content material is barely 98%.

What’s the complete weight in kilograms (water content material plus dry matter) popping out of the oven?

He finds 50 kilograms and he thinks he made a mistake: “A lot weight misplaced for such a small change in water content material!”

Are you able to assist him?

Write operate potatoes with

  • int parameter p0 – preliminary p.c of water-
  • int parameter w0 – preliminary weight –
  • int parameter p1 – closing p.c of water –

potatoesought to return the ultimate weight popping out of the oven w1 truncated as an int.

Instance:

potatoes(99, 100, 98) --> 50

The answer in Golang

Choice 1:

bundle resolution

func Potatoes(p0, w0, p1 int) int {
    return w0 * (100 - p0) / (100 - p1)
}

Choice 2:

bundle resolution

const p.c = 100

func Potatoes(p0, w0, p1 int) int {
  return int(float64(w0) * (float64(percent-p0) / float64(percent-p1)))
}

Choice 3:

bundle resolution

func Potatoes(p0, w0, p1 int) int {
  
  weight := w0 * (100-p0)/(100-p1)

  return int(weight)
}

Check instances to validate our resolution

bundle our_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)

func dotest(p0, w0, p1 int, exp int) {
    var ans = Potatoes(p0, w0, p1)
    Anticipate(ans).To(Equal(exp))
}

var _ = Describe("Check Instance", func() {

    It("ought to deal with primary instances", func() {
        dotest(99, 100, 98, 50)
        dotest(82, 127, 80, 114)
    })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments