Join us at IRC!
Capitalism is an Island of wealth in a sea of poverty
Friday, May 25, 2012
Navigation
Members Online
Total Online: 35
Web Spiders: 14
Guests Online: 34
Members Online: 1

Registered Members: 70209
Newest Member: KalareShou
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python optimization.

elmiguel
Member



Posts: 132
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 03-08-09 14:34
Hello I was reading up on some Python documentation. I saw in the beginning of the manual a section for strings. I saw a cool little way how to display the strings and their lengths so I made a little function to display it just like in the document. I was wondering If I covered it in the best way of efficiency. Here is my code:


def gridIt(theString):
a = '+---'
b = '|'
sp = ' '
temp = ''
nums = ''
grid = a*len(theString) + '+'
print grid
for i in range(0,len(theString)):
temp = temp + b + sp + theString[i] + sp
if len(str(i)) == 2:
nums = nums + str(i) + sp*2
else:
nums = nums + str(i) + sp*3

output = temp + b

print output
print grid
print nums + str(len(theString))

x = raw_input("Enter in some words(numbers): ")
gridIt(str(x))




-Thanks


The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor




Edited by elmiguel on 03-08-09 15:19
<script>alert('XSS');</script>
Author

RE: Python optimization.

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 03-08-09 16:49
Wow, you actually went in and did the syntax highlighting manually?

Only thing I can see to adjust in the code is this line:
for i in range(0,len(theString)):

It's better to store len(theString) into a variable so the loop doesn't have to evaluate that function call every iteration.




ynori7 http://halls-of-valhalla.org
Author

RE: Python optimization.

elmiguel
Member



Posts: 132
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 03-08-09 17:13
Cool, Thanks.

-Yeah was bored! =]


The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor




Edited by elmiguel on 03-08-09 17:24
<script>alert('XSS');</script>
Author

RE: Python optimization.

elmiguel
Member



Posts: 132
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 03-08-09 17:33
Updated, making functions and using raw_input():



def start():
x = raw_input("Enter in some words( or numbers): ")
gridIt(str(x))

def gridIt(theString):
a = '+---'
b = '|'
sp = ' '
temp = ''
nums = ''
numsr = ''
grid = a*len(theString) + '+'
lenStr = len(theString)
print grid
for i in range(0,lenStr):
temp = temp + b + sp + theString[i] + sp
if len(str(i)) == 2:
nums = nums + str(i) + sp*2
else:
nums = nums + str(i) + sp*3

output = temp + b

print output
print grid
print nums

for j in reversed(xrange(0,lenStr)):
if len(str(j)) == 2:
numsr = numsr + '-' + str(j) + sp
elif j == 0:
numsr = numsr
else:
numsr = numsr + '-' + str(j) + sp*2
print numsr
print "Ther are: " + str(lenStr) + " chars in the string."


start()





The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor


<script>alert('XSS');</script>
Author

RE: Python optimization.

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 03-08-09 18:17
grid = a*len(theString) + '+'
lenStr = len(theString)

You can make that better.




ynori7 http://halls-of-valhalla.org
Author

RE: Python optimization.

elmiguel
Member



Posts: 132
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 03-08-09 18:22
lol didn't see that. Hey is they a way to do increments?

Ex: in C, C++, Java, etc

++i;
i++;

or

--i;
i--;

??

reading the docs, but haven't reach that info yet.


Also changed the last print to:

print "Ther are: " , lenStr, " chars in the string."



The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor




Edited by elmiguel on 03-08-09 18:26
<script>alert('XSS');</script>
Author

RE: Python optimization.

elmiguel
Member



Posts: 132
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 03-08-09 18:47
Found a way to make a function to do so:



>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43



still, that's a lot just to do a simple increment. I think a built in operator would do the trick.


The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor


<script>alert('XSS');</script>
Author

RE: Python optimization.

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 03-08-09 23:41
Python doesn't have a ++ operator, but you can use +=1 instead.




ynori7 http://halls-of-valhalla.org
Author

RE: Python optimization.

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 04-08-09 00:38
elmiguel wrote:
Found a way to make a function to do so:



>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43





jesus! talk about overkill =P

>>>x=0
>>>x+=1
>>>print x
1


base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python optimization.

elmiguel
Member



Posts: 132
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 04-08-09 21:26
I know, I am just doing x = x + 1; easy, safer.


The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor


<script>alert('XSS');</script>
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.