Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Python optimization.


elmiguel's Avatar
Member
2,795 1

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


ynori7's Avatar
Future Emperor of Earth
0 0

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.


elmiguel's Avatar
Member
2,795 1

Cool, Thanks.

-Yeah was bored! =]


elmiguel's Avatar
Member
2,795 1

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



ynori7's Avatar
Future Emperor of Earth
0 0

grid = a*len(theString) + '+' lenStr = len(theString) You can make that better.


elmiguel's Avatar
Member
2,795 1

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:

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


elmiguel's Avatar
Member
2,795 1

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.


ynori7's Avatar
Future Emperor of Earth
0 0

Python doesn't have a ++ operator, but you can use +=1 instead.


Demons Halo's Avatar
Member
0 0

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


elmiguel's Avatar
Member
2,795 1

I know, I am just doing x = x + 1; easy, safer.