diff options
| author | Abigail <abigail@abigail.be> | 2020-10-30 16:15:00 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2020-10-30 16:15:00 +0100 |
| commit | 4b95ed4b127ca266a79eff0d550eb1df8efafb1f (patch) | |
| tree | 717e5f7e9a03f20fd13d53bc4ba58babfce3ee61 | |
| parent | c3be7c56493e4193863155d53bb410c7af21cb28 (diff) | |
| download | perlweeklychallenge-club-4b95ed4b127ca266a79eff0d550eb1df8efafb1f.tar.gz perlweeklychallenge-club-4b95ed4b127ca266a79eff0d550eb1df8efafb1f.tar.bz2 perlweeklychallenge-club-4b95ed4b127ca266a79eff0d550eb1df8efafb1f.zip | |
AWK solution for week 84, part 1.
| -rw-r--r-- | challenge-084/abigail/awk/ch-1.awk | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-084/abigail/awk/ch-1.awk b/challenge-084/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..f595d15676 --- /dev/null +++ b/challenge-084/abigail/awk/ch-1.awk @@ -0,0 +1,62 @@ +# +# 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 in AWK we do character by character. +# +# For the second part, we have to compare it against a given +# maximum value. Awk, or at least my version, handles big integers +# just fine. +# +# 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 or so. +# + +{ + low = 0; # Indicates lower bound for reversing + reverse = ""; # This will contain the reversed number. + + # + # Check whether the number starts with a hyphen; if so, + # keep the sign, and don't include it in the part which + # needs reversing. + # + if (substr ($0, 1, 1) == "-") { + reverse = "-"; + low = 1; + } + + # + # Do the actual reversal. Note that in AWK strings are 1-based, + # and, hence, length() gives the highest index. + # + for (i = length ($0); i > low; i --) { + reverse = reverse substr ($0, i, 1); + } + # + # Turn reverse into a number + # + reverse = reverse + 0; + + # + # Check wether it fits in a 32-bit integer (we assume 2's complement) + # + if (reverse < -2147483648 || reverse > 2147483647) { + reverse = 0; + } + print reverse; +} |
