Members Online
Total Online: 35 Web Spiders: 14
Guests Online: 34
Members Online: 1
Registered Members: 70209 Newest Member: KalareShou
|
View Thread
| Author |
Python optimization. |
elmiguel
Member

Posts: 132
Location: Your Computer
Joined: 12.12.07 Rank: God |
|
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 |
|
| Author |
RE: Python optimization. |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
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.
|
|
| Author |
RE: Python optimization. |
elmiguel
Member

Posts: 132
Location: Your Computer
Joined: 12.12.07 Rank: God |
|
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 |
|
| Author |
RE: Python optimization. |
elmiguel
Member

Posts: 132
Location: Your Computer
Joined: 12.12.07 Rank: God |
|
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
|
|
| Author |
RE: Python optimization. |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
|
grid = a*len(theString) + '+'
lenStr = len(theString)
You can make that better.
|
|
| Author |
RE: Python optimization. |
elmiguel
Member

Posts: 132
Location: Your Computer
Joined: 12.12.07 Rank: God |
|
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 |
|
| Author |
RE: Python optimization. |
elmiguel
Member

Posts: 132
Location: Your Computer
Joined: 12.12.07 Rank: God |
|
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
|
|
| Author |
RE: Python optimization. |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
Python doesn't have a ++ operator, but you can use +=1 instead.
|
|
| Author |
RE: Python optimization. |
Demons Halo
Member

Posts: 261
Location: Sweden
Joined: 26.03.09 Rank: Hacker Level 1 |
|
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
|
|
| Author |
RE: Python optimization. |
elmiguel
Member

Posts: 132
Location: Your Computer
Joined: 12.12.07 Rank: God |
|
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
|
|
|
|
|