1

I want function to take the last digit of each number in the list and sum them up all together. So forexample, the function below would return "10".

def getSumOfLastDigits(numList): 
    x = sum(int(num[-1:]) for num in numList)
    print x

getSumOfLastDigits([1, 23, 456]) 
>>>10

Here is what i receive instead of the expected out "10"

def getSumOfLastDigits(numList): 
    x = sum(int(num[-1:]) for num in numList)
    print x
    getSumOfLastDigits([1, 23, 456]) 
2
  • 1
    I think you copied and pasted the wrong thing for your 'actual output'. Commented Oct 24, 2011 at 3:40
  • No actually i put 10 as expected output. This wasn't the actual output when i run the above code. Commented Oct 24, 2011 at 4:01

4 Answers 4

3

Too much work.

def getSumOfLastDigits(numList):
  return sum(x % 10 for x in numList)
Sign up to request clarification or add additional context in comments.

4 Comments

And what if the number is negative?
@JBernardo: Dunno. You'll have to ask the asker.
He asked for the last digit. (-1) % 10 is 9 and not 1
So, sum(abs(x) % 10 for x in numList). Done.
2

x = sum(num%10 for num in numList)

Comments

2

You can't index into a number; a number isn't a sequence of digits (internally, it isn't represented in base 10). You can obtain the last digit of a number using mathematical manipulation instead: take the remainder when dividing by 10. We do this with the % operator.

Also:

  • Don't print the value in your function, return it. Let the calling code decide what to do with the value. Calculation and output are separate tasks, and should be kept separate.

  • Avoid indicating data types in variable names - yes, even in Python. It's not a good idea to build in assumptions that aren't actually necessary. You could use any kind of sequence here, for example. The simplest way to indicate that you have more than one number is to use the plural, numbers. That also means you use a full word, and people don't have to think about what 'num' is short for.

  • There is no need to assign the result of an expression to a temporary variable, if you are just going to use it once, and right away. The name x doesn't tell us anything, so cut it out.

  • get is considered an ugly prefix for function names by most Pythonistas. It should already be obvious that the function calculates and returns a value. Use noun-type names for those functions, and verb-type names for functions that are primarily intended to manipulate some existing data.

Thus:

def sum_of_last_digits(numbers):
    return sum(number % 10 for number in numbers)

Comments

0
def getSumOfLastDigits(numList): 
    total=0
    for item in numList:
        newItem=str(item)
        length=newItem[len(newItem)-1]
        total+=int(length)
    return total

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.