DEV Community

Discussion on: Daily Challenge #61 - Evolution Rate

Collapse
 
avalander profile image
Avalander • Edited

My knowledge of statistics is a bit rusty (yet I didn't use rust for this challenge), but I think that an increase from 0 to anything is basically an infinite rate and a decrease from anything to 0 is a negative 100% rate, so I disagree with the examples 3 and 4 and have written my solution accordingly.

Haskell, of course

get_rate :: Double -> Double -> String
get_rate before after
  | before == after = "No evolution."
  | before == 0     = "Infinite evolution."
  | evolution < 0   = "A negative evolution of " ++ (show' . abs) evolution ++ "%."
  | otherwise       = "A positive evolution of " ++ show' evolution ++ "%."
  where
    evolution = (after - before) * 100 / before
    show'     = show . round
    -- show_abs = show' . abs -- Hmm, maybe not the best name ever, let's leave it out.

Output

get_rate 11.29 45.79 -- "A positive evolution of 306%."
get_rate 95.12 66.84 -- "A negative evolution of 30%."
get_rate 0 27.35     -- "Infinite evolution."
get_rate 41.26 0     -- "A negative evolution of 100%."
get_rate 1.26 1.26   -- "No evolution."