|
Just spent enough time fighting with matplotlib in a Python 2 virtualenv on Windows that I gave up and rebooted into an Ubuntu livestick. Then ran into problems there cause I forgot how much PyCharm was doing behind the scenes to make it easy to just run a script from wherever with relative imports and not worry about the path and such. So it sounds like time to try Anaconda in Windows. Does it have like virtualenvs of its own or do I put it inside a virtualenv and how does it integrate with PyCharm?
|
# ? Jun 8, 2015 02:29 |
|
|
# ? Jun 18, 2024 05:37 |
|
KICK BAMA KICK posted:Just spent enough time fighting with matplotlib in a Python 2 virtualenv on Windows that I gave up and rebooted into an Ubuntu livestick. Then ran into problems there cause I forgot how much PyCharm was doing behind the scenes to make it easy to just run a script from wherever with relative imports and not worry about the path and such. It replaces the need for virtualenv. You just point your project interpreter in PyCharm to your conda environment.
|
# ? Jun 8, 2015 02:40 |
|
Thermopyle posted:It replaces the need for virtualenv. It replaces virtualenv with conda.
|
# ? Jun 8, 2015 09:53 |
|
pmchem posted:Anaconda is also great for enterprise environments where getting Anaconda approved is one piece of paperwork, and you can't run "pip install" etc., because instead you're supposed to do more paperwork for each module. This is the very reason that I use Anaconda at work.
|
# ? Jun 8, 2015 11:47 |
|
KICK BAMA KICK posted:Just spent enough time fighting with matplotlib in a Python 2 virtualenv on Windows... Couple of things not really Anaconda-related: (1) PyCharm also runs in Ubuntu; it sounds like you were relying on PyCharm in Windows but not in Ubuntu? (2) You shouldn't be worrying about the path after activation if you have your virtualenvs set up correctly; you just (in bash) do the '$ source ~/path/to/virtualenv/bin/activate' then launch python / ipython / whatever. I believe the relative imports will also allow you to call python binaries inside the virtualenv if you don't want to run the activation script for some reason (3) A possibly even better solution for you if you're doing data analysis type stuff would be to run ipython notebook in your Ubuntu virtual machine. Then you can just (in windows) tunnel to it on the web port it uses (I think 8888), and you can do all the data analysis or prototyping you want there. Or still use the browser in Ubuntu, whatever you want
|
# ? Jun 8, 2015 15:41 |
|
Context is this is just for a Coursera algorithms class. Right, I wasn't using PyCharm in Ubuntu; I was trying to just boot it up, sudo apt-get install python-matplotlib and run what I thought was a simple program to generate the plots I needed. Error I got in Ubuntu was about "attempting a relative import not inside a package", which I'm not sure the cause of. __init__.py was in all the right places; the import was a simple from .sibling_module_in_the_same_directory import name, and it works fine in PyCharm so I assumed PyCharm was setting up something in the PYTHONPATH that made this OK. Might be a 2 vs. 3 thing cause I'm used to 3 but using 2 for the class, and I vaguely remember there being a difference between the two with regard to imports. The issues I had getting matplotlib going in Windows were some of the well-known vcvarsall.bat things, which you can sidestep by using the Windows binary packages from the one web site everyone knows; after that when calling pyplot.whatever() it throws some errors about not finding some graphics library it needs, which a search indicates is a known issue with the paths it searches when run inside a virtualenv that can be fixed but gently caress it. I ended up just c/ping all the (short) code into the same file to make it work under Ubuntu (just a one-off and the code isn't what you're turning in),. Then back in Windows I tried Anaconda and everything's perfect. (Actually Miniconda and then conda install matplotlib, not seeing any poo poo about vcvarsall.bat is like magic).
|
# ? Jun 8, 2015 16:24 |
|
Project Euler - Problem 10 SPOILERS AHEAD. I got the right answer, but it took several hours to run the program. Most people in the answer thread on Project Euler measured run time in milliseconds. Why does my program suck? code:
|
# ? Jun 9, 2015 01:33 |
|
You are iterating over every single number which isn't the answer. I strugle with project euler because I struggle with math. I need to get back into it. Here's the simplest plain english answer I've found for you. http://code.jasonbhill.com/c/project-euler-problem-3/ Basically there are a couple of sieves you can use for prime numbers that drop numbers off of lists so that you aren't even preforming math on them. I think Sieve of Eratosthenes is the one I always landed on when doing the research. Gothmog1065 fucked around with this message at 02:49 on Jun 9, 2015 |
# ? Jun 9, 2015 02:01 |
|
PongAtari posted:Project Euler - Problem 10 SPOILERS AHEAD. To start, you can simply throw out all even numbers because you already know that all of them (except for 2) are not prime. Your algorithm is O(n^2), modifying your loops to use steps of 2 instead of steps of 1 is at least a factor of 4 improvement. You also don't need to worry about numbers between (num/2) and (num): that's another factor of 2 improvement. Actually, since you're already ignoring even numbers, technically you don't need to worry about numbers between (num/3) and num, which is even better. From there it's just a matter of performing clever math tricks to further optimize the solution. If you're doing this in Python2.X, then every time that you call your function you're creating a huge list; using xrange would speed that up. If you're using Python3, then that comment doesn't apply. Other solutions might be using Numpy array or Numba compilation in order to speed up the math. But even if you don't go down that route, there are lots of things that you can do to optimize this. And therein lies the fun
|
# ? Jun 9, 2015 03:29 |
|
QuarkJets posted:To start, you can simply throw out all even numbers because you already know that all of them (except for 2) are not prime. Your algorithm is O(n^2), modifying your loops to use steps of 2 instead of steps of 1 is at least a factor of 4 improvement. You also don't need to worry about numbers between (num/2) and (num): that's another factor of 2 improvement. Actually, since you're already ignoring even numbers, technically you don't need to worry about numbers between (num/3) and num, which is even better.
|
# ? Jun 9, 2015 06:18 |
|
ShadowHawk posted:To determine if a number is prime, you only need to test division with other known primes up to the square root of that number. Nice. See? That's a rad math fact that should make the problem run much faster
|
# ? Jun 9, 2015 06:20 |
|
ShadowHawk posted:To determine if a number is prime, you only need to test division with other known primes up to the square root of that number. Python code:
Python code:
|
# ? Jun 9, 2015 06:29 |
|
PongAtari posted:Project Euler - Problem 10 SPOILERS AHEAD. It's not really a rad math fact and it won't make as big a difference as some of these other suggesions... but to get you in the habit of thinking about the minimal necessary work needed to solve the problem: To get the sum of all those numbers, you don't actually need to save them all in a big list. Instead, keep a running total, and increment it every time you find a new prime.
|
# ? Jun 9, 2015 06:42 |
|
SurgicalOntologist posted:It's not really a rad math fact and it won't make as big a difference as some of these other suggesions... but to get you in the habit of thinking about the minimal necessary work needed to solve the problem:
|
# ? Jun 9, 2015 06:45 |
|
You're completely right of course. Saving the primes is necessary to efficiently test new primes. You (PongAtari) should keep doing it, just not for the reason you did it to begin with
|
# ? Jun 9, 2015 06:49 |
|
And by the way; Project Euler is fun but don't worry if you can't get a question! Annoyingly they're almost all math questions with some programming rather than the other way around. The best 100% "Coding" tests I've actually found were surprisingly https://www.codingame.com which actually owns and is a lot more complex than you'd realize at first.
|
# ? Jun 9, 2015 12:01 |
|
Jewel posted:And by the way; Project Euler is fun but don't worry if you can't get a question! Annoyingly they're almost all math questions with some programming rather than the other way around. The best 100% "Coding" tests I've actually found were surprisingly https://www.codingame.com which actually owns and is a lot more complex than you'd realize at first. Not math? Count me in!
|
# ? Jun 9, 2015 13:12 |
|
ShadowHawk posted:Including the square root optimization: FYI ^ is bitwise XOR, not exponentiation.
|
# ? Jun 9, 2015 14:17 |
|
QuarkJets posted:If you're doing this in Python2.X, then every time that you call your function you're creating a huge list; using xrange would speed that up. If you're using Python3, then that comment doesn't apply. What's 3 doing differently?
|
# ? Jun 9, 2015 16:18 |
|
Opulent Ceremony posted:What's 3 doing differently? Treating range() like xrange() from python 2, I think.
|
# ? Jun 9, 2015 16:24 |
|
vikingstrike posted:Treating range() like xrange() from python 2, I think. Yes quote:ActivePython 2.7.0.2 (ActiveState Software Inc.) based on quote:Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In
|
# ? Jun 9, 2015 16:51 |
|
PyQt question: How do I find out which row a user has selected in a QTable? edit: n/m I see the dumb thing I did - QModelIndex is the only member of a list that is being returned. FoiledAgain fucked around with this message at 21:09 on Jun 9, 2015 |
# ? Jun 9, 2015 20:15 |
|
Gothmog1065 posted:Not math? Count me in! Also look at Reddit's Daily Programming Challenges. The range from Easy, Intermediate and Hard on a variety of topics.
|
# ? Jun 10, 2015 03:26 |
|
Lysidas posted:FYI ^ is bitwise XOR, not exponentiation.
|
# ? Jun 10, 2015 04:20 |
|
Hughmoris posted:Also look at Reddit's Daily Programming Challenges. The range from Easy, Intermediate and Hard on a variety of topics. I might look at that in concurrence with the codingame, but I"m loving codingame as I don't have to math all that much. However, I think I've written myself into a corner. I might be misunderstanding while. Basically this is the "Move Thor to the right spots" game. It gives you Thor's starting locations and the locations of the lightning thing. However, I'm having some difficulty with Thor's "current position. It never updates past his initial position. Here's the code: Python code:
|
# ? Jun 10, 2015 19:38 |
|
Well, you sure made that a lot more complicated than I at least did (I've been enjoying CodinGame as well)! I'm having some trouble understanding your functions and whatnot since you've decided to pack everything up so much, but the problem I had with the Thor one was that I wasn't updating my program's knowledge of Thor's position when he moves. Are you doing this?
|
# ? Jun 10, 2015 20:11 |
|
hooah posted:Well, you sure made that a lot more complicated than I at least did (I've been enjoying CodinGame as well)! I thought I was doing that here (the return adds the "move" to the coordinates supposedly), but it's not updating. Probably because I'm doing something wrong. I'm also thinking of putting the coordinates in as a simple list (x,y). Python code:
|
# ? Jun 10, 2015 20:32 |
|
I dunno, I just did a bunch of if/elifs based on the difference between Thor's location and the light thing's location. What I ended up doing for a couple other easy-level ones was firing up PyCharm so I could do some proper debugging. That might help a lot.
|
# ? Jun 10, 2015 20:41 |
|
Once you get it working, here's my Thor program's equivalent in python, for reference:Python code:
Python code:
|
# ? Jun 10, 2015 20:47 |
|
The while loop gets executed after every move you output and it only passes in the "energy" level. So you need to store your own internal version of where you think Thor is. This also means you can move your function defs outside of the while loop. In general, as far as I can tell, all you're missing is keeping track of where do YOU think Thor is after printing out the command direction, since the game doesn't give you that information except for the starting position. The structure of the program is basically: code:
edit: I see you are doing that with currentX and currentY, but you are overriding it because start gets reset to 0 at the top of your loop. Move "start = 0" from inside to outside of the while. Helado fucked around with this message at 23:42 on Jun 10, 2015 |
# ? Jun 10, 2015 23:34 |
|
Helado posted:The while loop gets executed after every move you output and it only passes in the "energy" level. So you need to store your own internal version of where you think Thor is. This also means you can move your function defs outside of the while loop. In general, as far as I can tell, all you're missing is keeping track of where do YOU think Thor is after printing out the command direction, since the game doesn't give you that information except for the starting position. Aha! I was thinking of doing that last night, and completely missed the overriding start part. I knew it was something dumb. Is it better to move all my functions outside of the loop in general? Once the program is working, I'm going to work on trimming it down to a smaller and less overworked size.
|
# ? Jun 11, 2015 13:02 |
|
Gothmog1065 posted:Is it better to move all my functions outside of the loop in general? As far as I'm aware, yes, it's better to separate your functions from your main program/function in all programming languages (although I only have experience with C/C++ and Python).
|
# ? Jun 11, 2015 14:27 |
|
hooah posted:As far as I'm aware, yes, it's better to separate your functions from your main program/function in all programming languages (although I only have experience with C/C++ and Python). For example, one thing I sometimes do is code:
|
# ? Jun 11, 2015 19:44 |
|
Jewel posted:Once you get it working, here's my Thor program's equivalent in python, for reference: I finally ended up with: Python code:
|
# ? Jun 11, 2015 19:51 |
|
Cingulate posted:Are there any exceptions to this rule? I see this a lot and either it's dependent on nested namespaces, which gets hairy, or it's just sloppy. For example here you could just do: Python code:
|
# ? Jun 11, 2015 19:56 |
|
Cingulate posted:Are there any exceptions to this rule? I'm not an expert by any means, but why define functions within functions, rather than defining them elsewhere and calling them as needed? I guess I could see an argument if you're tight on memory, but you'd probably be more likely to be using C anyway in that case.
|
# ? Jun 11, 2015 20:01 |
|
Closures are super powerful and you should use them. They let you do stuff like this:code:
|
# ? Jun 11, 2015 20:12 |
|
Ah, yes, I would make an exception for functions that return a function.
|
# ? Jun 11, 2015 20:21 |
|
Gothmog1065 posted:Aha! I was thinking of doing that last night, and completely missed the overriding start part. I knew it was something dumb. Is it better to move all my functions outside of the loop in general? Once the program is working, I'm going to work on trimming it down to a smaller and less overworked size. Others have already responded, but I'll add, that based on where you're probably at on learning, you will almost always want to keep your function definitions outside of other things (other functions, loops, etc). There are some neat things and reasons for defining functions inside other scopes, but until you really need to, you're better off keeping functions 'global'. For example, it would have probably been easier to see where your problem was with that start variable if you didn't have all that extra code somewhere it doesn't have to be. It also would be easier for you to test the function independently of the surrounding code.
|
# ? Jun 11, 2015 20:50 |
|
|
# ? Jun 18, 2024 05:37 |
|
It's also going to slow down your program a lot if you define functions inside loops or other things that get evaluated repeatedly (if you need to care about speed at all).
|
# ? Jun 12, 2015 06:06 |