DEV Community

dev.to staff
dev.to staff

Posted on

8

Daily Challenge #83 - Deodorant Evaporator

This program tests the life of an evaporator containing a gas.

We know the content of the evaporator (content in ml), the percentage of foam or gas lost every day (evap_per_day) and the threshold (threshold) in percentage beyond which the evaporator is no longer useful. All numbers are strictly positive.

The program reports the nth day (as an integer) on which the evaporator will be out of use.

Note: Content is in fact not necessary in the body of the function "evaporator", you can use it or not use it, as you wish. Some people might prefer to reason with content, some other with percentages only. It's up to you but you must keep it as a parameter because the tests have it as an argument.


This challenge comes from g964 from 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!

Dev Diairies image

User Feedback & The Pivot That Saved The Project ↪️

We’re following the journey of a dev team building on the Stellar Network as they go from hackathon idea to funded startup, testing their product in the real world and adapting as they go.

Watch full video 🎥

Top comments (5)

Collapse
 
pmkroeker profile image
Peter • Edited

In Go! This assumes that the evaporator is always filled up to 100%.

func evaporator(evapPerDay, threshold float64) int {
    days := 0
    fill := 100.0
    for fill >= threshold {
        days += 1
        fill -= (fill * evapPerDay / 100)
    }
    return days
}

Go Playground Example

EDIT:
Forgot to take consider that the evapPerDay is based on the current fill not the starting.

Collapse
 
bhuvan profile image
Bhuvan Ganesan

Javascript version

const evaporator = (content, evapPerDay, threshold) => {
  let  outOfUseDays = 0, percentage = 100;  
  while (percentage > threshold) {
    percentage = percentage - percentage * (evapPerDay / 100);
    outOfUseDays += 1;
  }
  return outOfUseDays;
}
//console.log('evaporator --> ',evaporator('ml',7,2));
Collapse
 
aminnairi profile image
Amin

Elm

getDaysBeforeEvaporation : Float -> Float -> Int
getDaysBeforeEvaporation percentageBeforeUnusable percentageLostPerDay =
    let
        percentageBeforeUnusableClamped : Float
        percentageBeforeUnusableClamped =
            percentageBeforeUnusable
                |> clamp 1 100

        percentageLostPerDayClamped : Float
        percentageLostPerDayClamped =
            percentageLostPerDay
                |> clamp 1 100
    in
    floor <| (100 - percentageBeforeUnusableClamped) / percentageLostPerDayClamped

Playground

Try it online here.

Collapse
 
kvharish profile image
K.V.Harish • Edited

My solution in js


const evaporator = (netContent = 100, evapPerDay, threshold) => {
  let  nthDay = 0;  
  while (netContent >= threshold) {
    netContent -= (netContent * (evapPerDay / 100));
    nthDay++;
  }
  return nthDay;
}

Collapse
 
delixx profile image
DeLiXx
function lifespan(evap_per_day, threshold) {
    let content = 100;
    let day = 0;
    while (fill >= threshold) {
        fill *= 1-(evap_per_day/100);
        day++;
    }
    return day;
}

Runner H image

Automate Your Workflow in Slack, Gmail, Notion & more

Runner H connects to your favorite tools and handles repetitive tasks for you. Save hours daily. Try it free while it’s in beta.

Try for Free

👋 Kindness is contagious

Delve into this thought-provoking piece, celebrated by the DEV Community. Coders from every walk are invited to share their insights and strengthen our collective intelligence.

A heartfelt “thank you” can transform someone’s day—leave yours in the comments!

On DEV, knowledge sharing paves our journey and forges strong connections. Found this helpful? A simple thanks to the author means so much.

Get Started