multinomial combinations

2011-09-24 Thread Dr. Phillip M. Feldman
I wrote a small generator function that produces multinomial combinations. (Python's itertools module does ordinary combinations, but not multinomial combinations). The code essentially works, except that the the last combination in each tuple is not enclosed in a nested tuple: In [2]: x=

strange behavior from recursive generator

2011-09-23 Thread Dr. Phillip M. Feldman
A few weeks ago, I wrote a class that creates an iterator for solving the general unlabeled-balls-in-labeled boxes occupancy problem. Chris Rebert converted my code to a generator, which made the code cleaner, and I subsequently simplified it somewhat further. My problem is the following: All of

Re: recursive algorithm for balls in numbered boxes

2011-09-12 Thread Dr. Phillip M. Feldman
Mark Dickinson-2 wrote: This is a well-known trick: to divide 5 (unlabeled) balls amongst 3 (labeled) boxes, you write down sequences of 5 o's and 2 x's, where the o's represent the 5 balls and the 'x's represent dividers: ooxooxo - [2, 2, 1] xoooxoo - [0, 3, 2] And

Re: recursive algorithm for balls in numbered boxes

2011-09-11 Thread Dr. Phillip M. Feldman
Hello Peter, When I run my code, I get the same 14 configurations that your code produces; the only different that I can see in the output is that the configurations are produced in a different order. Note that your code is not creating an iterator, so thus doesn't do what I want. Also,

Re: recursive algorithm for balls in numbered boxes

2011-09-11 Thread Dr. Phillip M. Feldman
Chris, Your code is much cleaner than mine. I will have to figure out exactly how it is working. Thanks! Phillip -- View this message in context: http://old.nabble.com/recursive-algorithm-for-balls-in-numbered-boxes-tp32440187p32443579.html Sent from the Python - python-list mailing list

Re: can't generate iterator from list

2011-09-10 Thread Dr. Phillip M. Feldman
Very nice explanation! I've circumvented the problem by returning a `deepcopy` of the list. I've also added an acknowledgment. The revised code is attached. I'd like to see both balls in numbered boxes (this code) and balls in unnumbered (indistinguishable) boxes in Python's `itertools`

Re: can't generate iterator from list

2011-09-10 Thread Dr. Phillip M. Feldman
I just realized that there is a defect in my algorithm, so I will try to code this using a recursive algorithm instead. -- View this message in context: http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32439439.html Sent from the Python - python-list mailing list archive at

recursive algorithm for balls in numbered boxes

2011-09-10 Thread Dr. Phillip M. Feldman
I've written a recursive class that creates an iterator to solve a general formulation of the combinatorics problem known as balls in numbered boxes (also known as indistinguishable balls in distinguishable boxes). The code has been extensively tested and appears to work, but isn't terribly

Re: can't generate list from iterator

2011-09-09 Thread Dr. Phillip M. Feldman
The title should have been can't generate list from iterator. -- View this message in context: http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32435569.html Sent from the Python - python-list mailing list archive at Nabble.com. --

can't generate iterator from list

2011-09-09 Thread Dr. Phillip M. Feldman
It is supposed to be possible to generate a list representation of any iterator that produces a sequence of finite length, but this doesn't always work. Here's a case where it does work: Input: from itertools import combinations list(combinations(range(4),2)) Output: [(0, 1), (0, 2), (0, 3),

Re: Turtle graphics speed(). Seems broken

2010-02-27 Thread Dr. Phillip M. Feldman
Stefan Behnel-3 wrote: alexander@gmail.com wrote: I think the speed function may be broken from the turtle graphics package from turtle import * speed('fastest') forward(50) I have tried all of the different speed settings, but I get no change in the turtle's speed

Re: how to convert string function to string method?

2009-12-07 Thread Dr. Phillip M. Feldman
Bruno- You've made some excellent suggestions, and I'm always grateful for the opportunity to learn. My revised code appears below. Philllip def strip_pairs(s, open='([{\'', close=')]}\''): OVERVIEW This function strips matching pairs of characters from the beginning and end of

Re: How to create a docstring for a module?

2009-12-06 Thread Dr. Phillip M. Feldman
Steven D'Aprano-7 wrote: On Sun, 06 Dec 2009 10:55:50 +0100, Andreas Waldenburger wrote: On Sat, 5 Dec 2009 23:04:42 -0800 (PST) Dr. Phillip M. Feldman pfeld...@verizon.net wrote: If I create a module xyz.py with a docstring xyz does everything you could possibly want. at the top

Re: How to create a docstring for a module?

2009-12-06 Thread Dr. Phillip M. Feldman
OK. I was able to reproduce the problem. My difficulty was that the command that I issued initially was from xyz import * rather than just import xyz. If I say import xyz, then the docstring is defined; if I say from xyz import *, it isn't. I'm not sure whether this is a bug or expected

how to convert string function to string method?

2009-12-06 Thread Dr. Phillip M. Feldman
I wrote a handy-dandy function (see below) called strip_pairs for stripping matching pairs of characters from the beginning and end of a string. This function works, but I would like to be able to invoke it as a string method rather than as a function. Is this possible? def strip_pairs(s=None,

How to create a docstring for a module?

2009-12-05 Thread Dr. Phillip M. Feldman
If I create a module xyz.py with a docstring xyz does everything you could possibly want. at the top, the command ?xyz issued at the IPython prompt does not display this docstring. What am I doing wrong? -- View this message in context:

How to find number of line that is currently executing?

2009-10-09 Thread Dr. Phillip M. Feldman
I would like to put a statement on line N of my program that prints the line number that is currently executing. This may sound fairly trivial, but I don't want to hard code the line number because N will change if lines are inserted or deleted above that point. Any advice will be appreciated. --

Re: Is there a better way to code variable number of return arguments?

2009-10-08 Thread Dr. Phillip M. Feldman
I'm amazed that this works. I had not realized that x,y= [3,4] is equivalent to x= 3; y= 4 Python is rather clever. Thanks! snip To elaborate on Paul's answer, returning the list will also unpack it if you have it set up that way. E.g. def func(alist): return alist some_list = [1,

comparison on list yields surprising result

2009-08-28 Thread Dr. Phillip M. Feldman
In [21]: x Out[21]: [1, 2, 3, 5] In [22]: x6 Out[22]: True Is this a bug? -- View this message in context: http://www.nabble.com/comparison-on-list-yields-surprising-result-tp25195170p25195170.html Sent from the Python - python-list mailing list archive at Nabble.com. --

Re: comparison on list yields surprising result

2009-08-28 Thread Dr. Phillip M. Feldman
It looks as though what I should have done is the following: In [23]: array(x) 6 Out[23]: array([False, False, False, False], dtype=bool) -- View this message in context: http://www.nabble.com/comparison-on-list-yields-surprising-result-tp25195170p25195893.html Sent from the Python -

Python 'for' loop is memory inefficient

2009-08-14 Thread Dr. Phillip M. Feldman
I wrote the following correct but inefficient test of primality for purposes of demonstrating that the simplest algorithm is often not the most efficient. But, when I try to run the following code with a value of n that is large enough to produce a significant amount of running time, I get an

trouble with reload

2009-08-13 Thread Dr. Phillip M. Feldman
According to the Python documentation, 'reload' reloads a previously imported module (so that changes made via an external editor will be effective). But, when I try to use this command, I get the following error message: TypeError: reload() argument must be module Any suggestions will be

Re: trouble with reload

2009-08-13 Thread Dr. Phillip M. Feldman
how you're calling it? E.g. reload('foo') or reload(foo) ? On Thu, 13 Aug 2009 12:05:26 -0700, Dr. Phillip M. Feldman pfeld...@verizon.net wrote: According to the Python documentation, 'reload' reloads a previously imported module (so that changes made via an external

How to comment on a Python PEP?

2009-08-06 Thread Dr. Phillip M. Feldman
Is there a mechanism for submitting comments on a Python PEP? -- View this message in context: http://www.nabble.com/How-to-comment-on-a-Python-PEP--tp24840417p24840417.html Sent from the Python - python-list mailing list archive at Nabble.com. --

trouble with complex numbers

2009-08-05 Thread Dr. Phillip M. Feldman
When I try to compute the phase of a complex number, I get an error message: In [3]: from cmath import * In [4]: x=1+1J In [5]: phase(x) snip NameError: name 'phase' is not defined snip AttributeError: 'complex' object has no attribute 'phase' Any advice will be appreciated. -- View this

Re: trouble with complex numbers

2009-08-05 Thread Dr. Phillip M. Feldman
I am using Python 2.5, and most of the cmath functions are not yet available in this version. Thanks! Phillip P.S. In your code, that should be x+= 0J P.P.S. I wish that the documentation indicated anything that is new. Christian Heimes-2 wrote: snip phase() has been added to Python

possible to round number and convert to string?

2009-07-31 Thread Dr. Phillip M. Feldman
I'd like to be able to convert a float to a string representation in which the number is rounded to a specified number of digits. If num2str is a hypothetical function that does this, then num2str(pi,3) would be '3.142' (not '3.141'). I've been told that there is no such function in Python. I

Re: possible to round number and convert to string?

2009-07-31 Thread Dr. Phillip M. Feldman
This was very close to what I wanted. Thanks! My final code looks like this: def num2str(x,f=4): Convert x (int or float) to a string with f digits to right of the decimal point. f may be zero or negative, in which case the decimal point is suppressed. s= str(round(x,f)) if

delayed sys.exit?

2009-07-29 Thread Dr. Phillip M. Feldman
In the attached http://www.nabble.com/file/p24726902/test.py test.py code, it appears that additional statements execute after the call to sys.exit(0). I'll be grateful if anyone can shed light on why this is happening. Below is a copy of some sample I/O. Note that in the last case I get

Re: list vs. tuple [Re: len() should always return something]

2009-07-24 Thread Dr. Phillip M. Feldman
isinstance(x, (int, float, complex)) is certainly very compact, and does what I want. Thanks! -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654347.html Sent from the Python - python-list mailing list archive at Nabble.com. --

Re: len() should always return something

2009-07-24 Thread Dr. Phillip M. Feldman
As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1 Actually, Matlab does: length(5) ans = 1 -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654358.html Sent

Re: len() should always return something

2009-07-24 Thread Dr. Phillip M. Feldman
: print x Having to put such extra logic into practically every function is one of the annoying things about Python. Phillip Diez B. Roggisch-2 wrote: Dr. Phillip M. Feldman schrieb: Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example

Re: missing 'xor' Boolean operator

2009-07-17 Thread Dr. Phillip M. Feldman
Suppose that 'xor' returns the value that is true when only one value is true, and False otherwise. This definition of xor doesn't have the standard associative property, that is, (a xor b) xor c will not necessarily equal a xor (b xor c) To see that this is the case, let a= 1, b= 2, and c=

PDF version of Python Tutorial?

2009-07-17 Thread Dr. Phillip M. Feldman
Does anyone know if there is a PDF version of the Python Tutorial (URL= http://www.python.org/doc/current/tutorial/)? -- View this message in context: http://www.nabble.com/PDF-version-of-Python-Tutorial--tp24543817p24543817.html Sent from the Python - python-list mailing list archive at

Re: missing 'xor' Boolean operator

2009-07-15 Thread Dr. Phillip M. Feldman
I did initially ask for an infix xor operator, but eventually gave up on this. I like the first of your two one-line solutions below; this is clean and easy to understand. Thanks! I'd still like to be able to write an expression like '(a and b) xor (c and d) xor (e and f)', but it looks as

Re: missing 'xor' Boolean operator

2009-07-14 Thread Dr. Phillip M. Feldman
= not result return result MRAB-2 wrote: Ethan Furman wrote: Robert Kern wrote: On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: != does do what I want, except that it doesn't indicate to someone reading the code that the operands are being treated as logicals. (Readability is supposed