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
|
//
// 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.
//
//
// Note that the largest *positive* value which can fit in a
// 32 bit integer is 2,147,483,647, but the largest *negative*
// value is -2,147,483,648.
//
//
// Create an interface to read from STDIN
//
const rl = require ('readline') . createInterface ({
input: process . stdin,
});
//
// Read lines of input, calculate the result, and print it.
//
rl . on ('line', (line) => {
console . log (do_reverse (line));
});
function do_reverse (line) {
let sign = "";
//
// Strip off a leading hyphen; remember if we did so.
//
if (line . substring (0, 1) == "-") {
line = line . substring (1);
sign = "-";
}
//
// Reverse the digits.
//
let enil = sign + line . split ("") . reverse () . join ("");
//
// Turn the result into a bigint, so we can safely treat it
// as a number.
//
let result = BigInt (enil);
//
// If the result is too large (or too small), return 0.
// Else, return elin. Note that we should not return result;
// since result is a BigInt, printing it adds a trailing 'n'.
//
return result > 2147483647 || result < -2147483648 ? 0 : enil;
}
|