Skip to content

bpo-37178: Allow a one argument form of math.perm() #13905

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

Merged
merged 2 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,17 @@ Number-theoretic and representation functions
of *x* and are floats.


.. function:: perm(n, k)
.. function:: perm(n, k=None)

Return the number of ways to choose *k* items from *n* items
without repetition and with order.

Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
to zero when ``k > n``.

If *k* is not specified or is None, then *k* defaults to *n*
and the function returns ``n!``.

Raises :exc:`TypeError` if either of the arguments are not integers.
Raises :exc:`ValueError` if either of the arguments are negative.

Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1885,16 +1885,21 @@ def testPerm(self):
self.assertEqual(perm(n, 1), n)
self.assertEqual(perm(n, n), factorial(n))

# Test one argument form
for n in range(20):
self.assertEqual(perm(n), factorial(n))
self.assertEqual(perm(n, None), factorial(n))

# Raises TypeError if any argument is non-integer or argument count is
# not 2
# not 1 or 2
self.assertRaises(TypeError, perm, 10, 1.0)
self.assertRaises(TypeError, perm, 10, decimal.Decimal(1.0))
self.assertRaises(TypeError, perm, 10, "1")
self.assertRaises(TypeError, perm, 10.0, 1)
self.assertRaises(TypeError, perm, decimal.Decimal(10.0), 1)
self.assertRaises(TypeError, perm, "10", 1)

self.assertRaises(TypeError, perm, 10)
self.assertRaises(TypeError, perm)
self.assertRaises(TypeError, perm, 10, 1, 3)
self.assertRaises(TypeError, perm)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For math.perm(n, k), let k default to n, giving the same result as
factorial.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Give math.perm() a one argument form that means the same as
math.factorial().
15 changes: 11 additions & 4 deletions Modules/clinic/mathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3002,26 +3002,32 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
math.perm

n: object
k: object
k: object = None
/

Number of ways to choose k items from n items without repetition and with order.

Evaluates to n! / (n - k)! when k <= n and evaluates
to zero when k > n.

If k is not specified or is None, then k defaults to n
and the function returns n!.

Raises TypeError if either of the arguments are not integers.
Raises ValueError if either of the arguments are negative.
[clinic start generated code]*/

static PyObject *
math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
/*[clinic end generated code: output=e021a25469653e23 input=b2e7729d9a1949cf]*/
/*[clinic end generated code: output=e021a25469653e23 input=5311c5a00f359b53]*/
{
PyObject *result = NULL, *factor = NULL;
int overflow, cmp;
long long i, factors;

if (k == Py_None) {
return math_factorial(module, n);
}
n = PyNumber_Index(n);
if (n == NULL) {
return NULL;
Expand Down