[Tutor] For Loop
Hi, I was wondering if someone can complete a for loop in an array for me. Thank you ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] For Loop
On 30/10/15 01:27, Shelby Neely wrote: Hi, I was wondering if someone can complete a for loop in an array for me. for item in anArray: print(item) If that doesn't help then you are going to have to explain more about what exactly you want to know. Show us your code, explain what you expected to happen and what isn't working. Show us any error messages. Tell us your Python version and OS too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counter not working in Quick Sort script
That make sense, thank you. I changed the quick_sort() to def quick_sort(A, start, stop, count): if start < stop: # increment count by length of partition less the pivot count += (stop - start - 1) print(count) split = partition(A, start, stop) # recursively sort lower partition left = quick_sort(A, start, split-1, count) if left: count = left # recursively count upper partition right = quick_sort(A, split, stop, count) if right: count = right return count Yes, the sorting worked on all the *lists I tested.* For counting comparisons, I used a sorted list with a known number of comparisons to be able check the accumulator variable. -Patricia On Thu, 10/29/15, Alan Gauld wrote: Subject: Re: [Tutor] counter not working in Quick Sort script To: tutor@python.org Date: Thursday, October 29, 2015, 9:12 PM On 29/10/15 19:11, Patti Scott via Tutor wrote: Caveat: I didn't check the algorithms for correctness, I'll just take your word for that. > My accumulator variable to count the number of comparisons returns nonsense. > def quick_sort(A, start, stop, count): > if start < stop: > # increment count by length of partition less the pivot > count += (stop - start - 1) > print(count) > split = partition(A, start, stop) > > # recursively sort lower partition > quick_sort(A, start, split-1, count) > # recursively count upper partition > quick_sort(A, split, stop, count) > > return count Notice that you only set count once at the top of the function. What the recursive instances of the function do is irrelevant because you don't use their return values. So the return value of this function is always (count + stop - start -1) for the initial invocation value of count. I suspect you really want to do something to count based on the returns from the recursive calls too. > def main(): > unsorted = [ 1, 2, 3, 4, 5, 6, 7, 8 ] This looks very sorted to me? Is that correct? > count = quick_sort(unsorted, 0, len(unsorted), 0) count should return 0+len()-0-1 -> len-1 = 7 > print(unsorted) > print("There are {} comparisons.".format(count)) > > main() > > > This code is giving me this output: > > ➜ algorithms python3 quick_sort_first.py > 7 This is the outer functions count > 13 > 18 > 22 > 25 > 27 > 28 > 28 These are the recursive values of count which are invisible to the outer invocation > [1, 2, 3, 4, 5, 6, 7, 8] This is the sorted result > There are 7 comparisons. And this reflects the outer value of count again. Your code does exactly what you told it to do. Your problem is that you are not using the returned counts from the recursive calls. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] For Loop
Hello Shelby, > I was wondering if someone can complete a for loop in an array for > me. Your question is a bit too terse. You don't give us too much detail in understanding what you want to do. But, we are the Tutor list and we are here to help! Here are a few things which may help you get started * A few other programming languages refer to an array as a basic data type. In Python, it is the 'list', which is the analogous basic data type (although there is a an 'array' data type). You should start by learning how to use a Python 'list', though. * A for loop on a list is very easy. Here's a Python3 code snippet: data = list(range(10)) # -- put 10 elements in a list for item in data: print(item) * You may find it helpful to work through some tutorial material. There are many. We can help you choose one if you tell us what you are trying to do. I will suggest the main Python tutorial at the language documentation site as a starting place. Since you asked about for loops, you probably want to read about control flow. https://docs.python.org/3/tutorial/ https://docs.python.org/3/tutorial/controlflow.html If you have more specific details on what you are trying to accomplish and/or learn, then send along those questions! Good luck as you get started, -Martin -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] ncurses question
Hi. Looking over various sites on ncurses. Curious. I see various chunks of code for creating multiple windows.. But I haven't seen any kind of example that shows how to 'move' or switch between multiple windows. Anyone have any code sample, or any tutorial/site that you could point me to! I'm thinking of putting together a simple test to be able to select between a couple of test windows, select the given field in the window, and then generate the results in a lower window based on what's selected.. Just curious. Any pointers, greatly appreciated. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ncurses question
In a message of Fri, 30 Oct 2015 16:06:44 -0400, bruce writes: >Hi. > >Looking over various sites on ncurses. Curious. I see various chunks >of code for creating multiple windows.. But I haven't seen any kind of >example that shows how to 'move' or switch between multiple windows. > >Anyone have any code sample, or any tutorial/site that you could point me to! > >I'm thinking of putting together a simple test to be able to select >between a couple of test windows, select the given field in the >window, and then generate the results in a lower window based on >what's selected.. > >Just curious. Any pointers, greatly appreciated. > >Thanks I went looking and found this: http://www.tuxradar.com/content/code-project-build-ncurses-ui-python#null but I think you are going to have to believe Eric Raymond when he mentions here: http://invisible-island.net/ncurses/ncurses-intro.html newterm(type, ofp, ifp) A program which outputs to more than one terminal should use newterm() instead of initscr(). newterm() should be called once for each terminal. It returns a variable of type SCREEN * which should be saved as a reference to that terminal. (NOTE: a SCREEN variable is not a screen in the sense we are describing in this introduction, but a collection of parameters used to assist in optimizing the display.) The arguments are the type of the terminal (a string) and FILE pointers for the output and input of the terminal. If type is NULL then the environment variable $TERM is used. endwin() should called once at wrapup time for each terminal opened using this function. set_term(new) This function is used to switch to a different terminal previously opened by newterm(). The screen reference for the new terminal is passed as the parameter. The previous terminal is returned by the function. All other calls affect only the current terminal. That paper is talking about ncurses and C, but since as far as I know all that the python bindings do is wrap the same things, it should be true there, as well. But I have no working code, so take this with the appropriate amount of salt Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] For Loop
On Fri, Oct 30, 2015 at 10:48 AM, Martin A. Brown wrote: > > Hello Shelby, > >> I was wondering if someone can complete a for loop in an array for >> me. > > Your question is a bit too terse. You don't give us too much detail > in understanding what you want to do. Also, we have no idea *why* you want to do what you're asking. "Why" is important. The reason is because, since you're presumably a beginner, you might not realize that you're asking is something possibly nonsensical. I'm being serious. As a programmer, it's all too easy for us to answer the wrong question because we haven't double-checked the "why" to make sure it makes internal sense. It's especially problematic for programmers because there's a temptation to rush toward a problem's solution before asking oneself: "Is this even a good idea?" [For rest of the Tutor group: an interesting example of this situation is in the "Cracking the Oyster" chapter of Programming Pearls (http://www.amazon.com/Programming-Pearls-2nd-Edition-Bentley/dp/0201657880). It describes a scenario where the solution hinged, not directly on the question being asked, but on why the question was asked.] Anyway, any details about what you already know, and what you think you might need help with, these are details that will make it easier for us to help. Let us know. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Plotting with python
If you were going to get started doing some simple plotting with Python 2.7 (in my case, I'm simply plotting temperature against time-of-day) what would you use? - matplotlib [1] - gnuplot [2] - something else entirely? Assume no substantial familiarity with the underlying plotting software, let alone the Python bindings. The only thing I can think of that might be special is to specify the upper/lower bounds of the plot; for example, in my case, I know the temperatures vary between somewhere around 70-78 degrees F., so I'd want the Y-axis to go, say 60-90, not arbitrarily start at zero; but I suspect this is a pretty standard thing in almost any plotting package. [1] http://matplotlib.org/api/pyplot_api.html [2] http://gnuplot-py.sourceforge.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Plotting with python
On 31/10/2015 00:00, Terry Carroll wrote: If you were going to get started doing some simple plotting with Python 2.7 (in my case, I'm simply plotting temperature against time-of-day) what would you use? - matplotlib [1] - gnuplot [2] - something else entirely? Assume no substantial familiarity with the underlying plotting software, let alone the Python bindings. The only thing I can think of that might be special is to specify the upper/lower bounds of the plot; for example, in my case, I know the temperatures vary between somewhere around 70-78 degrees F., so I'd want the Y-axis to go, say 60-90, not arbitrarily start at zero; but I suspect this is a pretty standard thing in almost any plotting package. [1] http://matplotlib.org/api/pyplot_api.html [2] http://gnuplot-py.sourceforge.net/ matplotlib, I gave up gnuplot in favour of it maybe 15 years ago and have never looked back. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Plotting with python
>> If you were going to get started doing some simple plotting with Python >> 2.7 (in my case, I'm simply plotting temperature against time-of-day) >> what would you use? >> >> - matplotlib [1] >> - gnuplot [2] >> - something else entirely? >> >> Assume no substantial familiarity with the underlying plotting software, >> let alone the Python bindings. >> >> The only thing I can think of that might be special is to specify the >> upper/lower bounds of the plot; for example, in my case, I know the >> temperatures vary between somewhere around 70-78 degrees F., so I'd want >> the Y-axis to go, say 60-90, not arbitrarily start at zero; but I >> suspect this is a pretty standard thing in almost any plotting package. >> >> [1] http://matplotlib.org/api/pyplot_api.html >> [2] http://gnuplot-py.sourceforge.net/ > > matplotlib, I gave up gnuplot in favour of it maybe 15 years ago and have > never > looked back. I think my transition was later and I'm modestly bilingual with these tools. However, in principle, I agree with Mark--IF you are primarily using Python as your tool for massaging and exploring data. If you are, then I might add one more suggestion. There's a project called 'IPython' [0] which has built a very nicely extended and richer interactive interface to the Python interpreter. You can use IPython as a replacement for the Python interactive shell. I have for years, and it's wonderful (even though, I also use the interactive shell that ships with the system supplied Python I use). Why am I talking about IPython? Aside from other benefits, the IPython Notebook [1] is directly useful to those who are also matplotlib users, because it allows you to record an entire analysis session, display graphics inline (see macro "%matplotlib inline") and then later, share the data explorations in a web browser. N.B. I have not found any running, public IPython Notebooks. This doesn't surprise me, because of the security risks of allowing just anybody access to a Python instance is like letting strangers into your kitchen. They might eat all of your food, or try to crack that safe behind the portrait in the dining room. http://calebmadrigal.com/graph-ipython-notebook/ So, if I were in your shoes, starting today, I'd install IPython and matplotlib and then fire up the IPython Notebook on my local machine, type '%matplotlib inline' and start trying to display my data. One nice feature of matplotlib is that it autoscales by default. So, if all of your values (temperature) are within the range you want to display, you don't need to mess with the axes. See their tutorial: http://matplotlib.org/users/pyplot_tutorial.html Good luck and enjoy! -Martin [0] http://ipython.org/ [1] http://ipython.org/notebook.html -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Plotting with python
I'd use matplotlib, unless the ultimate goal is to render onto a webpage. Then I would use bokeh. http://bokeh.pydata.org/en/latest/ Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor