DEV Community

Discussion on: Daily Challenge #61 - Evolution Rate

Collapse
 
choroba profile image
E. Choroba • Edited

Perl solution. I still don't understand how the values are computed for zeroes, but it passes the tests :-)

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

sub get_evolution_rate_message {
    my ($before, $after) = @_;
    return 'No evolution.' if $before == $after;
    return sprintf 'A %s evolution of %.0f%%.',
        qw(_ negative positive)[ $before <=> $after ],
        abs(($before - ($after || ($before+$before ** 2)))
            * 100 / ($before || 1))
}

use Test::More tests => 5;

is get_evolution_rate_message(11.29, 45.79), 'A positive evolution of 306%.';
is get_evolution_rate_message(95.12, 66.84), 'A negative evolution of 30%.';
is get_evolution_rate_message(0, 27.35),     'A positive evolution of 2735%.';
is get_evolution_rate_message(41.26, 0),     'A negative evolution of 4126%.';
is get_evolution_rate_message(1.26, 1.26),   'No evolution.';
Collapse
 
aminnairi profile image
Amin

Imagine that instead of going from 0 to 27.35, you are going from 0.0000000001 to 27.35. I think you'll have your answer here. 😉