blob: 561e95cb82beae6ba3c7baad75c107a65bdc9005 (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/usr/bin/env perl
use v5.20;
use strict;
use warnings;
use feature qw(signatures);
no warnings qw(experimental::signatures);
=pod
TASK #1 › Reverse Integer
Submitted by: Mohammad S Anwar
You are given an integer $N.
Write a script to reverse the given integer and print the result. Print 0 if the result doesn’t fit in 32-bit signed integer.
The number 2,147,483,647 is the maximum positive value for a 32-bit signed binary integer in computing.
Example 1:
Input: 1234
Output: 4321
Example 2:
Input: -1234
Output: -4321
Example 3:
Input: 1231230512
Output: 0
=cut
#<<<
sub reverse_integer($N) {
my $M = reverse abs $N, '-'x($N<0);
$M*(-2**31<=$M<2**31);
}
#>>>
my $int = qr/-?\d+/;
if ( @ARGV && $ARGV[0] =~ /^$int$/ ) {
say reverse_integer( $ARGV[0] );
}
else {
say "Usage: $0 <Integer>";
}
# Tests.
while (<DATA>) {
if (/^($int)\s+($int)$/) {
my $actual = reverse_integer($1);
$2 eq $actual || die "Error for $1. Expected $2, got $actual.";
}
}
__DATA__
# Test data of the form <Input> <Expected Output>
1234 4321
-1234 -4321
1231230512 0
|