aboutsummaryrefslogtreecommitdiff
path: root/challenge-084/jo-37
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-10-26 10:30:36 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-10-31 15:40:41 +0100
commit2822ba32a653833a10275929d6c70ffbfee1b29b (patch)
tree711657cc993a5f8db9f56ae170613c930add0a58 /challenge-084/jo-37
parent795ca661fbce66d3f2e571e89da40d58da58900c (diff)
downloadperlweeklychallenge-club-2822ba32a653833a10275929d6c70ffbfee1b29b.tar.gz
perlweeklychallenge-club-2822ba32a653833a10275929d6c70ffbfee1b29b.tar.bz2
perlweeklychallenge-club-2822ba32a653833a10275929d6c70ffbfee1b29b.zip
Solution to task 1
Diffstat (limited to 'challenge-084/jo-37')
-rwxr-xr-xchallenge-084/jo-37/perl/ch-1.pl28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-084/jo-37/perl/ch-1.pl b/challenge-084/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..f6277ee546
--- /dev/null
+++ b/challenge-084/jo-37/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+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.
+sub reverse_l {
+ pop @{[map $_ * (unpack('l', pack 'l', $_) == $_),
+ map copysign(scalar(reverse abs int), $_), shift]};
+}
+
+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';
+
+done_testing;
+
+# vi:ts=4: