Follow us on Twitter!
The important thing is not to stop questioning. - Albert Einstein
Friday, April 25, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 25
Guests Online: 23
Members Online: 2

Registered Members: 82909
Newest Member: awais
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python optimization.

elmiguel
Member



Posts: 161
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:

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

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png


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

RE: Python optimization.

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
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.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Python optimization.

elmiguel
Member



Posts: 161
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

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png


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

RE: Python optimization.

elmiguel
Member



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

Code


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

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png
<script>alert('XSS');</script>
Author

RE: Python optimization.

ynori7
Member



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

You can make that better.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Python optimization.

elmiguel
Member



Posts: 161
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:

Code
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

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png


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

RE: Python optimization.

elmiguel
Member



Posts: 161
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:

Code


>>> 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

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png
<script>alert('XSS');</script>
Author

RE: Python optimization.

ynori7
Member



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


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Python optimization.

Demons Halo
Member



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

Code


>>> 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: 161
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

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png
<script>alert('XSS');</script>