473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

type conversion problem

50
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:
Expand|Select|Wrap|Line Numbers
  1. sum(int(i)**2 for i in str(x))
  2.  
I think I read somewhere that type conversion in python was costly? Is there a better way to do this?

Thanks
Jul 20 '08 #1
5 1124
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. sum(int(i)**2 for i in str(x))
  2.  
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'?
Expand|Select|Wrap|Line Numbers
  1. >>> def sum_squares(x,y):
  2. ...     return sum([num**2 for num in [x,y]])
  3. ... 
  4. >>> sum_squares(3,4)
  5. 25
Why go to all the trouble of a list comprehension or generator?
Expand|Select|Wrap|Line Numbers
  1. >>> def sum_squares(x,y):
  2. ...     return x**2+y**2
  3. ... 
  4. >>> sum_squares(5,6)
  5. 61
  6. >>> 
Jul 20 '08 #2
kdt
50
thanks for the reply

I'm summing the squares of elements in a list

Expand|Select|Wrap|Line Numbers
  1. x=[1, 2, 3]
  2.  
  3. # I would expect 14 as a result
  4.  
cheers
Jul 21 '08 #3
jlm699
314 100+
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1,2,3]
  2. >>> sum( [ i**2 for i in x ] )
  3. 14
  4. >>> 
  5.  
Why the type conversion?
Jul 21 '08 #4
kdt
50
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1,2,3]
  2. >>> sum( [ i**2 for i in x ] )
  3. 14
  4. >>> 
  5.  
Why the type conversion?

sorry I gave the wrong info

x=1234
Jul 21 '08 #5
bvdet
2,851 Expert Mod 2GB
This is kind of screwy. Here's a couple of ways. One uses no conversion and the other converts once.
Expand|Select|Wrap|Line Numbers
  1. def sum_squares_int(num):
  2.     num_list = []
  3.     while num > 0:
  4.         num_list.append(num%10)
  5.         num /= 10
  6.     return sum([i**2 for i in num_list])
  7.  
  8. def sum_squares_int1(num):
  9.     return sum([(num/10**i%10)**2 for i in range(len(str(num)))])
Jul 21 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Aaron Watters | last post by:
I'm doing a heart/lung bypass procedure on a largish Python program at the moment and it prompted the thought that the methodology I'm using would be absolutely impossible with a more "type safe"...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
1
by: Jimmy Seow | last post by:
Dear All, Hope somebody can help me with this. I have created a class and have got it to serialize and deserialize using XmlSerialization without any problems. I've also read the documentation...
11
by: RWC | last post by:
Hello, I'm having trouble converting code in Access XP / 2002. I have some code that declares an variable "as database" in Access 97, which is not recognized in Access XP. I've tried to find a...
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
6
by: Dhirendra Singh | last post by:
Hi, The following C++ program is not compiling on my system. #include <iostream> using namespace std; class complex { double re, im; public: complex( ) :re(0), im(0) {}
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.