DEV Community

Discussion on: Daily Coding Puzzles - Nov 11th - Nov 16th

Collapse
 
choroba profile image
E. Choroba

Perl solution, tests included:

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

my @discounts = ([7, 50], [3, 20], [0, 0]);
sub total {
    my ($days) = @_;
    my $total = $days * 40;
    for (@discounts) {
        my ($at_least, $discount) = @$_;
        return $total - $discount if $days >= $at_least;
    }
}

use Test::More tests => 8;
is total(1), 40;
is total(2), 80;
is total(3), 3 * 40 - 20;
is total(4), 4 * 40 - 20;
is total(5), 5 * 40 - 20;
is total(6), 6 * 40 - 20;
is total(7), 7 * 40 - 50;
is total(8), 8 * 40 - 50;