Start learning free

LESSON 244 OF 673 · HELPER FUNCTIONS

Biggest Digit Sum in Python: Digits with % and //

Practice Read the full explanation

You get a list of integers nums. Return the number whose digits add up to the most.

The digit sum of 472 is 4 + 7 + 2 = 13. If two numbers have the same digit sum, return the one that comes first.

The work is split between two functions. The first one is a helper. It gets one number and returns the sum of its digits. The second one is the function that is graded. It uses the helper to compare the numbers.

 

Example 1:

Input: nums = [472, 95, 1000]
Output: 95
Explanation: The digit sums are 13, 14 and 1.
95 has the biggest one: 9 + 5 = 14.

Example 2:

Input: nums = [19, 55, 28]
Output: 19
Explanation: All three digit sums are 10.
19 comes first, so it wins the tie.

Example 3:

Input: nums = [0, 7]
Output: 7
Explanation: The digit sum of 0 is 0.
The digit sum of 7 is 7.

 

Constraints:

  • nums has at least one number.
  • Every number is between 0 and 109.
  • Return the number itself, not its digit sum.
STEP 1 OF 9
    Ctrl + Enter to check

    PYTHON · HELPER FUNCTIONS

    Biggest Digit Sum in Python, explained

    Problem overview

    The method biggestDigitSum receives a list of integers nums, each between 0 and 1,000,000,000, and returns the number whose digits add up to the most. The digit sum of 472 is 4 + 7 + 2 = 13. If two numbers have the same digit sum, the one that comes first wins. The method returns the number itself, not its digit sum.

    For [472, 95, 1000] the digit sums are 13, 14 and 1, so the answer is 95. For [19, 55, 28] all three sums are 10, so the first, 19, wins. For [0, 7] the sums are 0 and 7, so the answer is 7.

    Approach: a helper that sums digits

    The work is split in two. The helper digitSum turns one number into its digit sum. biggestDigitSum then runs a "best so far" search over the list, comparing numbers by their digit sums instead of their values.

    To read the digits of a number you do not need to turn it into text. The remainder of dividing by 10 is the last digit, and dividing by 10 and dropping the fraction removes it:

    n at the start of the turnn % 10 (added)n // 10 (next n)
    472247
    4774
    440

    Adding up 2, 7 and 4 gives 13. Once the last digit is removed, n is 0 and the loop stops. The same loop gives 0 for 0 and 1 for 1000000000, because zeros add nothing.

    Step by step

    1. In digitSum, start total = 0.
    2. Loop with while n > 0.
    3. Add n % 10 to total.
    4. Set n = n // 10 to drop the last digit. Without this line n never changes and the loop never ends.
    5. After the loop, return total.
    6. In biggestDigitSum, set best = nums[0].
    7. Loop with for num in nums, and if self.digitSum(num) > self.digitSum(best), set best = num.
    8. After the loop, return best.

    The solution in Python

    class Solution:
        def digitSum(self, n: int) -> int:
            total = 0
            while n > 0:
                total += n % 10
                n = n // 10
            return total
    
        def biggestDigitSum(self, nums: List[int]) -> int:
            best = nums[0]
            for num in nums:
                if self.digitSum(num) > self.digitSum(best):
                    best = num
            return best

    Details:

    • Use //, not /. In Python 472 / 10 is 47.2, a float, while 472 // 10 is 47.
    • For n = 0 the loop never runs and the helper returns 0, which is the right digit sum.
    • The loop starts with the first number, which is also best. Comparing a number with itself changes nothing.
    • The comparison uses >, not >=. On a tie, best keeps the number that came first, as the task asks.
    • The parameter n is rebound inside the helper. The caller's number is not changed, because integers are immutable.

    Why compare digit sums with a helper

    The search is the familiar "best so far" pattern: remember a winner, let every number challenge it, and replace the winner when a challenger is better. The only new part is what "better" means, and that is hidden inside the helper. The loop body stays one line long, and the digit logic lives in one place. The helper is called twice per number, once for the challenger and once for the current best. That repeats some work, but it keeps the code short, and the numbers are at most ten digits long.

    Testing

    numsdigit sumsresult
    [472, 95, 1000]13, 14, 195
    [19, 55, 28]10, 10, 1019
    [0, 7]0, 77

    Common mistakes

    • Returning the digit sum. The task wants the number: 95, not 14.
    • Using >=. Ties would go to the last number, not the first.
    • Forgetting n = n // 10. The loop repeats forever.
    • Comparing the numbers themselves. The biggest number is not the one with the biggest digit sum: 1000 is the biggest value in the first example but has the smallest sum.
    • Calling the helper without self.. Inside a method of the class, digitSum(num) alone is a NameError.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free