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;
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Perl solution, tests included: