Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-37128: Add math.perm(). #13731

Merged
merged 6 commits into from
Jun 2, 2019
Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jun 1, 2019

@serhiy-storchaka serhiy-storchaka changed the title Add math.perm(). bpo-35431: Add math.perm(). Jun 1, 2019
@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Jun 1, 2019
@mdickinson
Copy link
Member

@serhiy-storchaka Please could you open a new b.p.o. issue for math.perm? I think we do need to give people an opportunity to discuss before it goes in, and since issue 35431 ended up focusing on math.comb, people interested in math.perm might not be following it.

@serhiy-storchaka serhiy-storchaka changed the title bpo-35431: Add math.perm(). bpo-37128: Add math.perm(). Jun 1, 2019
@@ -192,6 +207,18 @@ Number-theoretic and representation functions
of *x* and are floats.


.. function:: perm(n, k)

Return the number of ways to choose *k* items from *n* items without repetition.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "with order" part should be mentioned. Perhaps s/the number/the distinct number/.

FWIW, here is the wording from the itertools docs:

permutations() | p[, r] | r-length tuples, all possible orderings, no repeated elements

combinations() | p, r | r-length tuples, in sorted order, no repeated elements

static PyObject *
math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
/*[clinic end generated code: output=bd2cec8d854f3493 input=2f336ac9ec8242f9]*/
perm_comb(PyObject *n, PyObject *k, int comb)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ISTM that combining the code for comb() and perm() doesn't save us much. I would rather they be decoupled at outset so as to not preclude possible optimizations (there are a number of ways to compute combinations that do early cancellation to keep the intermediate products from exploding in size).

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; one request for a docstring tweak.

@@ -207,6 +207,19 @@ Number-theoretic and representation functions
of *x* and are floats.


.. function:: perm(n, k)

Return the distinct number of ways to choose *k* items from *n* items
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"distinct number of ways" doesn't quite make sense. "number of distinct ways"? Or just lose the "distinct" word altogether.

@@ -3028,11 +3142,24 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
if (n == NULL) {
return NULL;
}
if (!PyLong_CheckExact(n)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking forward to the day when we can remove this kind of block as unnecessary (perhaps in 3.9). I thought I'd persuaded myself that everything does eventually get converted to plain ints, but now that I look again it's not clear, and I agree that making this check up front is a good idea.

@serhiy-storchaka serhiy-storchaka merged commit 5ae299a into python:master Jun 2, 2019
@serhiy-storchaka serhiy-storchaka deleted the math-perm branch June 2, 2019 08:16
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants