aboutsummaryrefslogtreecommitdiff
path: root/challenge-066/javier-luque/perl/ch-1.pl
blob: a0f83fa93f213ed365a66f206794f19d56e865e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/perl
# Test: ./ch-1.pl
use Modern::Perl;
use List::Util qw/sum/;
use Test::More;

is (divide(5,2),   2,  'Divide 5 , 2');
is (divide(-5,2), -3,  'Divide -5 , 2');
is (divide(-5,-2), 2,  'Divide -5 , -2');
done_testing();

sub divide {
    my ($M, $N) = @_;
    my $quotient = 0;
    my $negative = 0;

    if ($M < 0 && $N > 0) {
    	$negative = 1;
    	$M = - $M;
    } elsif ($M > 0 && $N < 0) {
    	$negative = 1;
    	$N = - $N;
    } elsif ($M < 0 && $N < 0) {
    	$M = - $M;
    	$N = - $N;
    }

    while ($M > $N) {
    	$M = $M - $N;
    	$quotient++;
    }

    return ($negative) ?
    	- $quotient - 1 : $quotient;
}