Problem overview
The method lastTwoDigitSum receives a non-negative integer n with at least two digits. It returns the sum of the last two digits of n.
For n = 2379 the last two digits are 7 and 9, so the answer is 7 + 9 = 16. The same idea answers the common question of how to get the last digit or the last two digits of a number in Python, and the lesson does it with two operators and no strings.
Approach: modulo takes digits from the right
The remainder operator % returns what is left after a division. Dividing by 10 leaves the last digit, and dividing by 100 leaves the last two:
n = 2379
print(n % 10) # 9
print(n % 100) # 79
That gives the number 79 in one operation. To split 79 into its two digits, use the same tool again for the ones digit (79 % 10 is 9) and floor division // for the tens digit (79 // 10 is 7). Floor division divides and throws away the remainder.
The sum of the two is the answer. Every step is a small calculation on integers, so no conversion to text is needed.
Step by step
- Keep the last two digits together in
lastTwoDigits, usingn % 100. Everything above the tens place is gone: 2379 becomes 79. - Take the last digit of that number into
lastDigit, withlastTwoDigits % 10. This gives 9. - Take the other digit into
secondLastDigit, withlastTwoDigits // 10. It must be//, because/always returns a float in Python and79 / 10is7.9. - Return the sum of the two variables. A plain
+is enough, since each is already a single digit.
The solution in Python
class Solution:
def lastTwoDigitSum(self, n: int) -> int:
lastTwoDigits = n % 100
lastDigit = lastTwoDigits % 10
secondLastDigit = lastTwoDigits // 10
return lastDigit + secondLastDigit
Some Python details here:
%and//go together. For non-negative numbers,a // bis the quotient anda % bis the remainder, anddivmod(79, 10)returns both at once as(7, 9).- The result of
//on integers is an integer. The method is annotated to return anint, and floor division keeps it that way. - The same code works on huge numbers. Python integers have no fixed size, so a number with 30 digits still works.
The string alternative
You can also read the last two characters of the number as text. That is a valid way to see the digits, but it creates strings and needs conversions back to integers:
n = 2379
print(str(n)[-2:]) # 79
print(sum(int(c) for c in str(n)[-2:])) # 16
The arithmetic version is the habit to build, because the same % 10 and // 10 moves are used in many digit problems.
Complexity
Time: O(1). The method does four arithmetic operations. Python's integers can be arbitrarily large, so the first modulo technically scales with the number's size, but for numbers of normal size it is constant.
Space: O(1). Three integer variables are used.
Edge cases and common mistakes
- Exactly two digits. For
n = 99,99 % 100is 99, and the answer is 9 + 9 = 18. The modulo does nothing harmful when the number is already short. - Zeros in the tens place. For
n = 105, the last two digits are 0 and 5, and105 % 100is 5, sosecondLastDigitis 0 and the answer is 5. The string approach would give"05", which is also fine when each character is converted separately. - Trailing zeros. For
n = 100, the result is 0. - Using
/instead of//.79 / 10is7.9, so the sum becomes16.9instead of 16. - Taking
% 10twice on the same number.n % 10is the last digit, so doing it again gives the last digit again. The second-to-last digit needs a shift first:n // 10 % 10, orlastTwoDigits // 10as in the lesson. - Negative inputs. The lesson only gives non-negative numbers. In Python,
-2379 % 100is21, because the remainder takes the sign of the divisor, so negative inputs needabs(n)first.
Testing
lastTwoDigitSum(2379)returns16.lastTwoDigitSum(99)returns18.lastTwoDigitSum(105)returns5.lastTwoDigitSum(100)returns0.lastTwoDigitSum(1000000007)returns7.
