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
This challenge comes from g964 on CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (10)
Oh come on, that's not coding :v
Perl solution with tests.
How did I get the formula?
Let's say
d
is the dry matter weight. We know thattherefore,
All this talk of potatoes makes me want to Go get some french fries.
I added some error checking for a few cases that made sense to me, such as:
I also changed the inputs and outputs to specifically be unsigned integers, since it seems like negative numbers were not meant to be used here and this avoids having to do error checking for things like:
Want to see my solutions to the other challenges? Go check them out on my Github! github.com/Dak425/dev-to-challenges
potato.go
potato_test.go
Good ol' python one-liner* 😊
def potatoes(p0, w0, p1): return w0*(100-p0)/(100-p1)
*math sold separately
I didn't know that we can define a function and return it in the same line ...
That's crazy
Haskell:
let potatoes = (p0, w0, p1) => parseInt(w0 * (100 - p0) / (100 - p1))
console.log(potatoes(99, 100, 98));
console.log(potatoes(50, 200, 25));
in C#
mind : blown 🤯