diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-06-22 11:36:55 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-06-25 22:45:59 +0200 |
| commit | 5e697646644c9fcaa5fb49384f27787156ce3476 (patch) | |
| tree | fb964a267d9e61ecf489dc69136bafbb4ae01b5b | |
| parent | 2a8a0f879f94caecf73922866c26dcfe133d96c0 (diff) | |
| download | perlweeklychallenge-club-5e697646644c9fcaa5fb49384f27787156ce3476.tar.gz perlweeklychallenge-club-5e697646644c9fcaa5fb49384f27787156ce3476.tar.bz2 perlweeklychallenge-club-5e697646644c9fcaa5fb49384f27787156ce3476.zip | |
solution for task 1
| -rw-r--r-- | challenge-066/jo-37/perl/ch-1.pl | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-066/jo-37/perl/ch-1.pl b/challenge-066/jo-37/perl/ch-1.pl new file mode 100644 index 0000000000..9a34b6f1ad --- /dev/null +++ b/challenge-066/jo-37/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +use Test2::V0; + +# Apply the sign of the second argument to the first. +sub apply { + my ($value, $sign) = @_; + + $sign >= 0 ? $value : -$value; +} + +# Return floor of dividend divided by divisor. +# Performs basically a long division. +sub divide { + my ($dividend, $divisor) = @_; + die 'divisor is zero' unless $divisor; + + # Save sign. + my $sign = apply $dividend, $divisor; + + # Take absolute values of parameters. + $dividend = apply $dividend, $dividend; + $divisor = apply $divisor, $divisor; + + # long division + my ($quotient, $remainder); + while (length $dividend > 0) { + $remainder .= substr $dividend, 0, 1, ''; + my $digit = 0; + for (; $remainder >= $divisor; $remainder -= $divisor) { + $digit++; + } + $quotient .= $digit; + } + + # Restore sign and adjust result. + apply($quotient, $sign) - ($sign < 0 && $remainder > 0 ? 1 : 0); +} + +is divide(0, 2), 0, 'zero numerator'; + +eval {divide(1, 0)}; +is $@, D(), 'division by zero'; + +is divide(5, 2), 2, 'first example'; +is divide(-5, 2), -3, 'second example'; +is divide(-5, -2), 2, 'third example'; +is divide(5, -2), -3, 'like second example'; +is divide(6, 2), 3, 'first divisible example'; +is divide(-6, 2), -3, 'second divisible example'; +is divide(-6, -2), 3, 'third divisible example'; +is divide(6, -2), -3, 'like second divisible example'; +is divide(2, 3), 0, 'zero'; +is divide(-2 , 3), -1, 'negative'; +is divide(12345, 23), 536, 'larger positive non divisable'; +is divide(-12345, 23), -537, 'larger negative non divisable'; +is divide(12328, 23), 536, 'larger positive divisable'; +is divide(-12328, 23), -536, 'larger negative divisable'; +is divide(101101, 101), 1001, 'many zeros, positive'; +is divide(-101100, 101), -1001, 'many zeros, negative'; +is divide(9223372036854775808, 16), 576460752303423488, 'very large dividend'; + +done_testing; |
