type conversion problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    type conversion problem

    hi all,

    I've just started back up with python after a looong break - it's great to be back :D

    Anyway, I'm going through "how to think like a computer scientist" as a refresher and trying to come up with a function to return the sum of squares for a given number.

    I decided to write it as a list comprehension:
    Code:
    sum(int(i)**2 for i in str(x))
    I think I read somewhere that type conversion in python was costly? Is there a better way to do this?

    Thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by kdt
    hi all,

    I've just started back up with python after a looong break - it's great to be back :D

    Anyway, I'm going through "how to think like a computer scientist" as a refresher and trying to come up with a function to return the sum of squares for a given number.

    I decided to write it as a list comprehension:
    Code:
    sum(int(i)**2 for i in str(x))
    I think I read somewhere that type conversion in python was costly? Is there a better way to do this?

    Thanks
    I don't understand how you could sum the squares of a number. You can sum the squares of 2 numbers though. Why do you need to convert anything? What is 'x'?
    [code=Python]>>> def sum_squares(x,y ):
    ... return sum([num**2 for num in [x,y]])
    ...
    >>> sum_squares(3,4 )
    25[/code]Why go to all the trouble of a list comprehension or generator?[code=Python]>>> def sum_squares(x,y ):
    ... return x**2+y**2
    ...
    >>> sum_squares(5,6 )
    61
    >>> [/code]

    Comment

    • kdt
      New Member
      • Mar 2007
      • 50

      #3
      thanks for the reply

      I'm summing the squares of elements in a list

      Code:
      x=[1, 2, 3]
      
      # I would expect 14 as a result
      cheers

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        [code=python]
        >>> x = [1,2,3]
        >>> sum( [ i**2 for i in x ] )
        14
        >>>
        [/code]
        Why the type conversion?

        Comment

        • kdt
          New Member
          • Mar 2007
          • 50

          #5
          Originally posted by jlm699
          [code=python]
          >>> x = [1,2,3]
          >>> sum( [ i**2 for i in x ] )
          14
          >>>
          [/code]
          Why the type conversion?

          sorry I gave the wrong info

          x=1234

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            This is kind of screwy. Here's a couple of ways. One uses no conversion and the other converts once.[code=Python]def sum_squares_int (num):
            num_list = []
            while num > 0:
            num_list.append (num%10)
            num /= 10
            return sum([i**2 for i in num_list])

            def sum_squares_int 1(num):
            return sum([(num/10**i%10)**2 for i in range(len(str(n um)))])[/code]

            Comment

            Working...