FAQ
Hey everyone,
I'm working on one of the puzzles on pyschools.com, and am trying to figure
out if I can make my solution a bit more elegant.

def getSumOfLastDigit(numList):
sumOfDigits=0
for i in range(0, len(numList)):
num=str(numList.pop())
sumOfDigits+=int(num[-1:])
return sumOfDigits

Below is the problem. Basically you take the last digit of each number in
the list, and add them together.

Write a function: getSumOfLastDigit(numList) that takes in a list of
positive numbers and returns the sum of all the last digit in the list.

*Examples*
getSumOfLastDigit([12, 23, 34])
9
getSumOfLastDigit([2, 3, 4])
9
getSumOfLastDigit([1, 23, 456])
10
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101109/d1084ae3/attachment-0001.html>

Search Discussions

  • Chris Rebert at Nov 9, 2010 at 7:09 pm

    On Tue, Nov 9, 2010 at 11:00 AM, Matty Sarro wrote:
    Hey everyone,
    I'm working on one of the puzzles on pyschools.com, and am trying to figure
    out if I can make my solution a bit more elegant.

    def getSumOfLastDigit(numList):
    ??? sumOfDigits=0
    ??? for i in range(0, len(numList)):
    ??? ??? num=str(numList.pop())
    Just loop over the list directly:
    for num in numList:
    num = str(num)
    ??? ??? sumOfDigits+=int(num[-1:])
    No need for the colon:
    sumOfDigits+= int(num[-1])
    ??? return sumOfDigits
    And now for the much simpler math-based solution:

    def getSumOfLastDigit(numList):
    return sum(num%10 for num in numList)

    Cheers,
    Chris
  • Peter Otten at Nov 9, 2010 at 7:13 pm

    Matty Sarro wrote:

    Hey everyone,
    I'm working on one of the puzzles on pyschools.com, and am trying to
    figure out if I can make my solution a bit more elegant.

    def getSumOfLastDigit(numList):
    sumOfDigits=0
    for i in range(0, len(numList)):
    num=str(numList.pop())
    sumOfDigits+=int(num[-1:])
    return sumOfDigits

    Below is the problem. Basically you take the last digit of each number in
    the list, and add them together.

    Write a function: getSumOfLastDigit(numList) that takes in a list of
    positive numbers and returns the sum of all the last digit in the list.

    *Examples*
    getSumOfLastDigit([12, 23, 34])
    9
    getSumOfLastDigit([2, 3, 4])
    9
    getSumOfLastDigit([1, 23, 456])
    10
    Loop over a list directly:

    for n in numlist:
    # ...

    Use the modulo (%) operator to find the last digit:

    last_digit = n % 10

    There is a built-in sum() function:

    sum([1,2,3]) # 6

    There is an inlined form of the for-loop called "generator expression"

    sum(i*i for i in [1,2,3]) # 14

    Putting it all together:
    def gsold(numbers):
    ... return sum(i%10 for i in numbers)
    ...
    gsold([12,23,34])
    9
    gsold([2,3,4])
    9
    gsold([1,23,456])
    10

    Peter
  • Terry Reedy at Nov 9, 2010 at 7:18 pm

    On 11/9/2010 2:00 PM, Matty Sarro wrote:

    I'm working on one of the puzzles on pyschools.com
    <http://pyschools.com>, and am trying to figure out if I can make my
    solution a bit more elegant.
    Definitely
    def getSumOfLastDigit(numList):
    sumOfDigits=0
    for i in range(0, len(numList)):
    num=str(numList.pop())
    This is an awkward way to iterate through a list ;-)
    sumOfDigits+=int(num[-1:])
    return sumOfDigits
    Write a function: getSumOfLastDigit(numList) that takes in a list of
    positive numbers and returns the sum of all the last digit in the list.

    *Examples*
    getSumOfLastDigit([12, 23, 34])
    9
    getSumOfLastDigit([2, 3, 4])
    9
    getSumOfLastDigit([1, 23, 456])
    10
    # Straightforward version of what you did

    def getSumOfLastDigit(numList):
    sumOfDigits=0
    for i in numList:
    sumOfDigits+=int(str(i)[-1:])
    return sumOfDigits

    print(getSumOfLastDigit([12, 23, 34]),
    getSumOfLastDigit([2, 3, 4]),
    getSumOfLastDigit([1, 23, 456]) )
    # 9 9 10

    # Use generator expression with built-in sum function

    def getSumOfLastDigit(numList):
    return sum(int(str(i)[-1:]) for i in numList)

    print(getSumOfLastDigit([12, 23, 34]),
    getSumOfLastDigit([2, 3, 4]),
    getSumOfLastDigit([1, 23, 456]) )
    # 9 9 10

    --
    Terry Jan Reedy
  • Matteo Landi at Nov 10, 2010 at 8:06 am
    I agree with Peter:
    * iterate over the list directly
    * use %10 instead of string conversion + slice
    (*) use genexps

    Good luck,
    Matteo
    On Tue, Nov 9, 2010 at 8:18 PM, Terry Reedy wrote:
    On 11/9/2010 2:00 PM, Matty Sarro wrote:

    I'm working on one of the puzzles on pyschools.com
    <http://pyschools.com>, and am trying to figure out if I can make my
    solution a bit more elegant.
    Definitely
    def getSumOfLastDigit(numList):
    ? ? sumOfDigits=0
    ? ? for i in range(0, len(numList)):
    ? ? ? ? num=str(numList.pop())
    This is an awkward way to iterate through a list ;-)
    ? ? ? ? sumOfDigits+=int(num[-1:])
    ? ? return sumOfDigits
    Write a function: getSumOfLastDigit(numList) that takes in a list of
    positive numbers and returns the sum of all the last digit in the list.

    *Examples*
    ? ?>>> ?getSumOfLastDigit([12, ?23, ?34])
    ? ?9
    ? ?>>> ?getSumOfLastDigit([2, ?3, ?4])
    ? ?9
    ? ?>>> ?getSumOfLastDigit([1, ?23, ?456])
    ? ?10
    # Straightforward version of what you did

    def getSumOfLastDigit(numList):
    ? ?sumOfDigits=0
    ? ?for i in numList:
    ? ? ? ?sumOfDigits+=int(str(i)[-1:])
    ? ?return sumOfDigits

    print(getSumOfLastDigit([12, 23, 34]),
    ? ? ?getSumOfLastDigit([2, 3, 4]),
    ? ? ?getSumOfLastDigit([1, 23, 456]) )
    # 9 9 10

    # Use generator expression with built-in sum function

    def getSumOfLastDigit(numList):
    ? ?return sum(int(str(i)[-1:]) for i in numList)

    print(getSumOfLastDigit([12, 23, 34]),
    ? ? ?getSumOfLastDigit([2, 3, 4]),
    ? ? ?getSumOfLastDigit([1, 23, 456]) )
    # 9 9 10

    --
    Terry Jan Reedy

    --
    http://mail.python.org/mailman/listinfo/python-list


    --
    Matteo Landi
    http://www.matteolandi.net/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 9, '10 at 7:00p
activeNov 10, '10 at 8:06a
posts5
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase