# Daily Challenge #64- Drying Potatoes

dev.to staff ・1 min read

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!

Posted on by:

### dev.to staff

The hardworking team behind dev.to ❤️

### Discussion

Oh come on, that's not coding :v

F#, but this will pretty much look the same in every language since it's a simple math problem:

let potatoes p0 w0 p1 = w0 * (100 - p0) / (100 - p1)


Test:

[<Fact>]
let 99% to 98% () = potatoes 99 100 98 |> should equal 50


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:

• No passing in 0 for the mass, since that means you don't have any spuds!
• No passing in a greater water ratio for p1. I don't think you can increase water ratio by drying potatoes.

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:

if p0 < 0 || p1 < 0 || w0 < 0 {
return 0, errors.New("cannot use negative values")
}


Want to see my solutions to the other challenges? Go check them out on my Github! github.com/Dak425/dev-to-challenges

potato.go

package potato

import "errors"

// Dry determines the weight of a mass of potatoes by reducing the current water ratio to the desired ratio
func Dry(p0, w0, p1 uint) (uint, error) {
if p1 > p0 {
return 0, errors.New("p1 cannot be greater than p0")
}
if w0 == 0 {
return 0, errors.New("w0 cannot be zero")
}
if p0 == p1 {
return w0, nil
}
return w0 * (100 - p0) / (100 - p1), nil
}



potato_test.go

package potato

import "testing"

type testCase struct {
description string
input       []uint
expected    uint
expectErr   bool
}

func TestDry(t *testing.T) {
testCases := []testCase{
{
"example test",
[]uint{99, 100, 98},
50,
false,
},
{
"bigger change in water ratio",
[]uint{99, 100, 95},
20,
false,
},
{
"no change in water ratio",
[]uint{87, 120, 87},
120,
false,
},
{
"cannot use a zero mass of potatoes",
[]uint{56, 0, 54},
0,
true,
},
{
"cannot increase the water ratio",
[]uint{42, 50, 50},
0,
true,
},
}
for _, test := range testCases {
result, err := Dry(test.input[0], test.input[1], test.input[2])
if err == nil && test.expectErr {
t.Fatalf("FAIL: %s - Dry(%d, %d, %d): expected error but nil was returned", test.description, test.input[0], test.input[1], test.input[2])
}
if result != test.expected {
t.Fatalf("FAIL: %s - Dry(%d, %d, %d): %d - expected: %d", test.description, test.input[0], test.input[1], test.input[2], result, test.expected)
}
t.Logf("PASS: %s", test.description)
}
}



Perl solution with tests.

#!/usr/bin/perl
use warnings;
use strict;

use Types::Standard qw( Int );
use Function::Parameters;

fun potatoes (Int $p0, Int$w0, Int $p1) { sprintf '%d',$w0 * (100 - $p0) / (100 -$p1)
}

use Test::More tests => 2;
is potatoes(99, 100, 98), 50;
is potatoes(50, 200, 25), 133;


How did I get the formula?
Let's say d is the dry matter weight. We know that

dm = w0 * (1 - p0)
dm = w1 * (1 - p1)


therefore,

w1 = w0 * (1 - p0) / (1 - p1)


potatoes p0 weight p1 = weight * (100 - p0) / (100 - p1)


Good ol' python one-liner* 😊

def potatoes(p0, w0, p1): return w0*(100-p0)/(100-p1)

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


*math sold separately

I didn't know that we can define a function and return it in the same line ...
That's crazy

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#

int potatoes(int p0, int w0, int p1) => w0 * (100 - p0) / (100 - p1);
Console.WriteLine(potatoes(50, 200, 25));


def potatoes (p0 , w0 , p1) :
return (100-p0) * w0 / (100-p1)
print (potatoes(99 , 100 ,98))



mind : blown 🤯