aboutsummaryrefslogtreecommitdiff
path: root/challenge-084
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-11-01 01:59:49 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-11-01 02:02:28 +0100
commitf9dc09d7df2e0b97e7129053a11f507c17f7d78f (patch)
treec4d5a9581f7d1ade57c76a75ddb446e1ab9fa923 /challenge-084
parent5d5d424d4b8121547935c503780d9c8f55fbf1de (diff)
downloadperlweeklychallenge-club-f9dc09d7df2e0b97e7129053a11f507c17f7d78f.tar.gz
perlweeklychallenge-club-f9dc09d7df2e0b97e7129053a11f507c17f7d78f.tar.bz2
perlweeklychallenge-club-f9dc09d7df2e0b97e7129053a11f507c17f7d78f.zip
Handle lists with ch-1.pl
Diffstat (limited to 'challenge-084')
-rwxr-xr-xchallenge-084/jo-37/perl/ch-1.pl36
1 files changed, 20 insertions, 16 deletions
diff --git a/challenge-084/jo-37/perl/ch-1.pl b/challenge-084/jo-37/perl/ch-1.pl
index f6277ee546..82e119b55e 100755
--- a/challenge-084/jo-37/perl/ch-1.pl
+++ b/challenge-084/jo-37/perl/ch-1.pl
@@ -1,27 +1,31 @@
#!/usr/bin/perl
+use 5.012;
use Test2::V0;
use Math::Utils 'copysign';
-# Reverse the decimal representation of a given number into a signed
-# long (32-bit) value. Return zero if the result does not fit.
+# Reverse the decimal representation of given numbers into signed
+# long (32-bit) values. Return zero for a result not fitting.
+# This sub handles a list of numbers.
sub reverse_l {
- pop @{[map $_ * (unpack('l', pack 'l', $_) == $_),
- map copysign(scalar(reverse abs int), $_), shift]};
+ map $_ * (unpack('l', pack 'l', $_) == $_),
+ map copysign(scalar(reverse abs int), $_), @_;
}
-is reverse_l( 1234), 4321, 'Example 1';
-is reverse_l(-1234), -4321, 'Example 2';
-is reverse_l( 1231230512), 0, 'Example 3';
-is reverse_l(-8463847412), -2**31, 'negative limit';
-is reverse_l( 7463847412), 2**31-1, 'positive limit';
-is reverse_l(-9463847412), 0, 'below negative limit';
-is reverse_l( 8463847412), 0, 'above positive limit';
-is reverse_l( 074), 6, 'accept octal';
-is reverse_l( 470), 74, 'do not generate octal';
-is reverse_l( 0xf0), 42, 'accept hex';
-is reverse_l( 123.456), 321, 'ignore part after decimal point';
-is reverse_l(-0), 0, 'signless zero';
+is [reverse_l 1234], [4321], 'Example 1';
+is [reverse_l -1234], [-4321], 'Example 2';
+is [reverse_l 1231230512], [0], 'Example 3';
+is [reverse_l -8463847412], [-2**31], 'negative limit';
+is [reverse_l 7463847412], [2**31-1], 'positive limit';
+is [reverse_l -9463847412], [0], 'below negative limit';
+is [reverse_l 8463847412], [0], 'above positive limit';
+is [reverse_l 074], [6], 'accept octal';
+is [reverse_l 470], [74], 'do not generate octal';
+is [reverse_l 0xf0], [42], 'accept hex';
+is [reverse_l 123.456], [321], 'ignore part after decimal point';
+is [reverse_l -0], [0], 'signless zero';
+is [reverse_l 1e9 + 2, -1e9 - 2, 1e9 + 3, -1e9 - 3],
+ [2e9 + 1, -2e9 - 1, 0, 0], 'list input';
done_testing;