# How to Dry Potatoes in Golang

## The challenge

All we eat is water and dry matter.

John bought potatoes: their weight is 100 kilograms. Potatoes contain water and dry matter.

The water content is 99 percent of the total weight. He thinks they are too wet and puts them in an oven – at low temperature – for them to lose some water.

At the output the water content is only 98%.

What is the total weight in kilograms (water content plus dry matter) coming out of the oven?

He finds 50 kilograms and he thinks he made a mistake: “So much weight lost for such a small change in water content!”

Can you help him?

Write function `potatoes` with

• int parameter `p0` – initial percent of water-
• int parameter `w0` – initial weight –
• int parameter `p1` – final percent of water –

`potatoes`should return the final weight coming out of the oven `w1` truncated as an int.

#### Example:

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

## The solution in Golang

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```package solution

func Potatoes(p0, w0, p1 int) int {
return w0 * (100 - p0) / (100 - p1)
}
```Code language: Go (go)```

Option 2:

``````package solution

const percent = 100

func Potatoes(p0, w0, p1 int) int {
return int(float64(w0) * (float64(percent-p0) / float64(percent-p1)))
}
```Code language: Go (go)```

Option 3:

``````package solution

func Potatoes(p0, w0, p1 int) int {

weight := w0 * (100-p0)/(100-p1)

return int(weight)
}
```Code language: Go (go)```

## Test cases to validate our solution

``````package 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)
Expect(ans).To(Equal(exp))
}

var _ = Describe("Test Example", func() {

It("should handle basic cases", func() {
dotest(99, 100, 98, 50)
dotest(82, 127, 80, 114)
})
})
```Code language: Go (go)```
Tags:
Subscribe
Notify of