nCr in Python.

{}_nC_r = \frac{n!}{(n-r)!r!}
Mit anderen Worten, es ist eine Kombination, die r aus n herausnimmt. Ich möchte dies in Python schreiben. Verwenden Sie in diesem Fall die Funktion "Reduzieren" höherer Ordnung.

import operator as op
def ncr(n,r):
    r = min(n-r,r)
    if r == 0:
        return 1
    num_over = reduce(op.mul, xrange(n, n - r, -1))
    num_under = reduce(op.mul, xrange(1,r + 1))
    return num_over // num_under

Wenn Sie mit einem Lambda-Ausdruck schreiben,

reduce(lambda x, y: x * y, xrange(n, n - r, -1))

Schreiben Sie wie.

Recommended Posts

nCr in Python
nCr in Python.
Quadtree in Python --2
Python in der Optimierung
CURL in Python
Metaprogrammierung mit Python
Python 3.3 mit Anaconda
Geokodierung in Python
SendKeys in Python
Metaanalyse in Python
Unittest in Python
Epoche in Python
Zwietracht in Python
Deutsch in Python
DCI in Python
Quicksort in Python
Plink in Python
Konstante in Python
FizzBuzz in Python
SQLite in Python
Schritt AIC in Python
LINE-Bot [0] in Python
Reverse Assembler mit Python
Reflexion in Python
Konstante in Python
Format in Python
Scons in Python 3
Puyopuyo in Python
Python in Virtualenv
PPAP in Python
Quad-Tree in Python
Reflexion in Python
Chemie mit Python
Hashbar in Python
DirectLiNGAM in Python
LiNGAM in Python
In Python reduzieren
In Python flach drücken
Sortierte Liste in Python
Clustertext in Python
AtCoder # 2 jeden Tag mit Python
Täglicher AtCoder # 6 in Python
Täglicher AtCoder # 18 in Python
Bearbeiten Sie Schriftarten in Python
Singleton-Muster in Python
Dateioperationen in Python
Lesen Sie DXF mit Python
Täglicher AtCoder # 53 in Python
Tastenanschlag in Python
Verwenden Sie config.ini mit Python
Täglicher AtCoder # 33 in Python
Löse ABC168D in Python
Logistische Verteilung in Python
LU-Zerlegung in Python
Ein Liner in Python
Einfacher gRPC in Python
AtCoder # 24 jeden Tag mit Python
Löse ABC167-D mit Python