Open Side Menu Go to the Top

12-15-2011 , 10:06 PM
If re are really your bottleneck, I would make your re simpler and/or solve it with something else. What are you trying to do?
** Python Support Thread ** Quote
** Python Support Thread **
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** Python Support Thread **
12-15-2011 , 10:30 PM
Quote:
Originally Posted by tyler_cracker
If re are really your bottleneck, I would make your re simpler and/or solve it with something else. What are you trying to do?
free poker database (fpdb) so parsing of hand histories. Regex is one of the bottlenecks - probably 20% if the db backend is mysql. With Full Tilt files it can run up to 40% since it's all in utf-16 and for the most part, twice the bytes per char. My repo is here, other 2p2ers such as sorrow have their's here.
** Python Support Thread ** Quote
12-26-2011 , 03:50 PM
how could i switch the values of two variables without using a dummy variable?
** Python Support Thread ** Quote
12-26-2011 , 03:51 PM
a, b = b, a
** Python Support Thread ** Quote
12-26-2011 , 04:01 PM
So pythons using one? That doesn't work out to
a=b
b=a
With a's all over the place?
** Python Support Thread ** Quote
12-26-2011 , 04:05 PM
Yes, it seriously will just automatically figure that out and do it for you.

Code:
>>> a = [1,2]
>>> b = [3,4]
>>> a,b = b,a
>>> a
[3, 4]
>>> b
[1, 2]
** Python Support Thread ** Quote
12-26-2011 , 04:10 PM
that's a neat trick but fwiw as a mostly non-pythonist i find that way less readable than using a temp variable or wrapping the trick in a function called swapValues() or something.
** Python Support Thread ** Quote
12-26-2011 , 04:29 PM
on another forum it got brought up that someone asked prospective employees this in his interviews. some other guy then said it was a stupid question. now they're raging over pointer related stuff. lol.
** Python Support Thread ** Quote
12-26-2011 , 04:33 PM
this seems like a pretty bad interview question to me, but i guess it depends on what the interviewer is hoping to learn by asking it. if the answer is, "does the applicant know this trick involving the comma operator", then it's really bad.
** Python Support Thread ** Quote
12-26-2011 , 04:52 PM
I agree that it is a bad question. If you know python, you would probably know it exists, but might not. But I don't think not knowing it is an indication of your abilities as a programmer. If the goal is to see how the person works through a problem that they don't immediately know the answer to, then there are much better problems that could be used instead.
** Python Support Thread ** Quote
12-26-2011 , 05:38 PM
I consider "a, b = b, a" to be pretty basic and idiomatic Python. I would rather see it in Python code

The reason it works is because of Python's tuple unpacking. You can do

Code:
>>> a, b = (10,12)
>>> a
10
>>> b
12
a situation where this can be quite useful is multiple return values from functions

Code:
def return_mult():
	return 1,2,3

>>> a,b,c = return_mult()
>>> a
1
>>> b
2
>>> c
3
writing a,b=b,a is equivalent to writing a,b = (b,a). The parentheses are implied in the former.
** Python Support Thread ** Quote
12-27-2011 , 03:42 AM
ha the only reason I answered that question so quickly was because I thought you were honestly curious. I'm not a "real" programmer but if I applied for a job for some reason and was asked that question I'd honestly wonder if it was a trick question and if I'm supposed to know if the tuple (b,a) counts as a temp variable. I wouldn't be surprised at all if expert programmers who learned other languages first routinely miss it as it's the kind of thing I doubt anyone knows until someone else tells them.

For math stuff I like the multiple assignment thing for recursively-defined sequences where I need the old value just long enough to calculate the new one, which is probably why like every tutorial uses the fibonacci sequence as an example
** Python Support Thread ** Quote
12-27-2011 , 04:23 PM
What is a good GUI to use for python?

Also, I used to mess around with visual basic like 10 years ago and remember that it had a very noob friendly tool to make good looking apps. Is there anything like that for python? Should I learn the tedious way anyway?
** Python Support Thread ** Quote
12-27-2011 , 04:30 PM
tkinter is the one that comes included. I'm trying to learn it myself right now, now that I found a Python-3 tk tutorial that's better than all the others because it has the key feature of actually existing: http://www.tkdocs.com/tutorial/index.html

If you're on Python 2 you might find one of these more helpful instead. The first link looked good to me except it's in Python 2 and too much of the syntax has changed for too much of it to work for me: http://wiki.python.org/moin/TkInter

btw if you try tkinter do not use IDLE, it runs in tkinter and you'll end up with some weird bugs.

EDIT: You may also want to check out this thread: http://stackoverflow.com/questions/1...ui-development
** Python Support Thread ** Quote
12-27-2011 , 05:01 PM
I was using a book for python 2 but am on python 3. it took me a while to figure out that the module's name was 'tkinter' not 'Tkinter' which is annoying. yet the methods used in the module still require capitalisation, so its tkinter.Tk()

that doc looks great, thanks
** Python Support Thread ** Quote
12-27-2011 , 07:43 PM
So I'm not sure if this is best posted in NC or here but whatever. Last night I got bored and decided to try starting Project Euler while drinking and got as far as problem 11. Looking back at my problem 10 I think I see why loop-else annoys some people (other than not being used to it):

Spoiler:
Code:
N = 1000

for i in range(1, N):
    for j in range(i,N-i):
        if i**2 + j**2 == (N-i-j)**2:
            break
    else:
        continue
    break

print(i*j*(N-i-j))

Last edited by Xhad; 12-27-2011 at 07:43 PM. Reason: indenting was wrong, lol
** Python Support Thread ** Quote
12-28-2011 , 12:50 AM
lol fail, I just realized that's problem 9 not 11. And also that the obvious answer is to put it in a function and use a return statement instead of that monstrosity of break/continues at the end
** Python Support Thread ** Quote
12-28-2011 , 01:56 AM
Quote:
Originally Posted by e i pi
What is a good GUI to use for python?

Also, I used to mess around with visual basic like 10 years ago and remember that it had a very noob friendly tool to make good looking apps. Is there anything like that for python? Should I learn the tedious way anyway?
wxpython is by far the best. It's frames skin to match those of the native platform. Easily portable between Mac, Windows, Linux. Custom frames and elements aren't that difficult to apply. And there's a vast array of tutorials and example code on the net.
** Python Support Thread ** Quote
12-30-2011 , 01:17 AM
Quote:
Originally Posted by Xhad
So I'm not sure if this is best posted in NC or here but whatever. Last night I got bored and decided to try starting Project Euler while drinking and got as far as problem 11. Looking back at my problem 10 I think I see why loop-else annoys some people (other than not being used to it):

Spoiler:
Code:
N = 1000

for i in range(1, N):
    for j in range(i,N-i):
        if i**2 + j**2 == (N-i-j)**2:
            break
    else:
        continue
    break

print(i*j*(N-i-j))
My only concern with this one is that sometimes i = j, and the problem calls for i < j < (N-i-j)**2, which depending on what number they tossed at you, could have giving you multiple and incorrect answers.

Okay, now you can lol at my solution:

Spoiler:
Code:
def fun9():
    for a in range(100, 500):
        for b in range(a+1, 500):
            if (int(math.sqrt(a**2 + b**2))**2) == (a**2 + b**2):
                c = int(math.sqrt(a**2 + b**2))
                if a+b+c==1000:
                    return a*b*c

ans = fun9()
print(ans)


regardless, the above only shows my mediocre math skills. After reading the accompanying PDFs, I began taking some math classes. Still too new at it, but to really get all of it, but it is helping to think differently, which is all I can really ask for at this moment. I wasn't sure where the top limit should be or how to compute it, or even intuit it properly. I think this sort of stuff will become very important moving forward.

I was attempting to create a semi-efficient brute force algorithm that isn't completely O(n^n), which I guess worked out a little bit in this case, but clearly, there would be many better solutions.
** Python Support Thread ** Quote
12-30-2011 , 02:07 AM
Oh yeah, you're right, the inner loop should be range(i+1, N-i) and not (i, N-i) given the problem constraints. It only ever returns one answer because that goofy series of break/continues means the algorithm quits the instant it finds a satisfactory answer...it breaks the inner loop, skips the else: continue and then hits the other break statement for the outer loop (this also means that if multiple answers exist it will only ever find one of them)
** Python Support Thread ** Quote
12-30-2011 , 08:04 PM
a simple way to speed it up, though not reduce its complexity, is to calculate c first (inside the inner loop) then check if a^2 + b^2 = c^2. Square roots are slower than integer multiplication.

the number theory of Pythagorean triples in general is interesting and I think there might be an almost-closed form solution to this problem. There are some more questions later where that might be relevant.
** Python Support Thread ** Quote
12-31-2011 , 04:19 AM
Quote:
Originally Posted by e i pi
how could i switch the values of two variables without using a dummy variable?
This works for numeric values.

assume two vars, a and b

b = a + b
a = b - a
b = b - a
** Python Support Thread ** Quote
12-31-2011 , 01:02 PM
Quote:
Originally Posted by DavidFongs
This works for numeric values.

assume two vars, a and b

b = a + b
a = b - a
b = b - a
lol
** Python Support Thread ** Quote
12-31-2011 , 08:14 PM
Quote:
Originally Posted by RoundTower
a simple way to speed it up, though not reduce its complexity, is to calculate c first (inside the inner loop) then check if a^2 + b^2 = c^2. Square roots are slower than integer multiplication.

the number theory of Pythagorean triples in general is interesting and I think there might be an almost-closed form solution to this problem. There are some more questions later where that might be relevant.
Yeah, I was attempting to skip the inner loop as much as possible by tossing out as many c computations as possible. To test this theory a little further:

Spoiler:
Code:
def fun9b(x):
    counter=0
    for a in range(1, x//2):
        for b in range(a+1, x//2):
            for c in range(b+1, x//2):
                if a**2 + b**2 == c**2:
                    counter+=1
                    if a+b+c == 1000:
                        print(counter)
                        return a*b*c
ans9b=fun9b(1000)
print(ans9b)

--vs--

def fun9(x):
    counter=0
    for a in range(1, x//2):
        for b in range(a+1, x//2):
            if math.sqrt(a**2 + b**2)%1== 0:
                c = int(math.sqrt(a**2 + b**2))
                counter+=1
                if a+b+c==x:
                    print(counter)
                    return a*b*c

ans = fun9(1000)
print(ans)


What's fascinating is that the counter showed up being 313 in fun9b(), while fun9() counts out to 320. I guess my intuition about computing c was incorrect on this one. One version passes the if more often but returns an answer faster while the other version passes if less yet returns an answer slower. Regardless, neither of these answers scale well, so if there was a complete brute-force style answer, these most certainly aren't it.

I did problem 39 last night. I have no idea what the heck the other people wrote or why they wrote it. My solution still leans on that horrid sqrt() function, but at least I figured out how to find out if a float is equal to an integer (%1).

I just don't know how to find the keys relative to a value in a dictionary, so I ended up with a very ugly solution:

Code:
Spoiler:
def fun39(): d = {} for a in range(1, 500): for b in range(a+1, 500): if math.sqrt(a**2 + b**2)%1 == 0: c = math.sqrt(a**2 + b**2) p = a+b+c if p <= 1000: if p in d: d[p]+=1 else: d[p]=1 a = {value:key for key, value in d.items()} print(a[max(a.keys())])
I only know that the more I do these, the more incentive I have to go back, read the PDFs, see other answers, and try out different and better solutions, and ffs, become a little better at a different programming language already.

In one of the math classes, they explore the Well-Ordering Principle. This fascinates me. I think once I get a decent grip on this concept, ideas will really start to meld together. Exposure to this at least helps me to get the idea of where some of the gcd concepts they discuss in the PDF on problem set 9 are coming from (or elude to, since the Ancient Greeks probably didn't have a formal definition of WOP).
** Python Support Thread ** Quote
12-31-2011 , 09:39 PM
I meant you should simply do c = 1000 - a - b. Once you know a and b, you don't need to test every possible value of c to see if a + b + c = 1000. And also skip the square roots so you do it more like the first function you posted.

As well as being slower, I think your methods of using sqrt() and checking to see if you get an integer value are going to fail for big numbers. I forget exactly what Python promises you here but floating point arithmetic is hard and almost all of the Project Euler questions are designed to deal with integers.
** Python Support Thread ** Quote
** Python Support Thread **
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** Python Support Thread **

      
m