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
|
#!/opt/perl/bin/perl
use 5.032;
use strict;
use warnings;
no warnings 'syntax';
use experimental 'signatures';
use experimental 'lexical_subs';
#
# 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.
#
#
# This is two challenges in one. First, we have to reverse a given
# integer -- which is trivial using Perl's "reverse" buildin.
#
# For the second part, we have to compare it against a given
# maximum value. We *don't* have to write code to handle huge
# numbers -- if the number is too big to handle, Perl will treat
# it as "Inf", which is larger than any regular integers.
#
# There's a tiny thing to consider. 2,147,483,647 is the largest
# positive value which fits in a 32-bit signed integer, but the
# smallest integer which fits is -2,147,483,648 [1]. So, if the input
# is 8463847412, the output should 0, but if the input is -8463847412,
# the output should be -2147483648.
#
# [1] We're assuming 2s-complement integers, which seems to have
# been the norm for the past 50 years.
#
my $MAX = 2_147_483_647; # Maximum value which fits in a 32-bit signed integer.
my $MIN = - $MAX - 1; # Minimum value which fits in a 32-bit signed integer.
while (<>) {
chomp;
#
# Reverse the numeric part; this keeps the sign as is.
#
s/[0-9]+/reverse $&/e;
#
# Either print 0, or the result.
#
say $_ > $MAX || $_ < $MIN ? 0 : $_;
}
__END__
|