Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python3
# coding:utf-8
import sys
def combi(n, r):
assert r >= 0, 'r must be larger than zero'
if n < r:
return 0
if r == 0:
return 1
if r == 1:
return n
return combi(n-1, r) + combi(n-1, r-1)
if __name__ == '__main__':
if len(sys.argv) != 3:
print('Usage: {} n r'.format(sys.argv[0]))
n = int(sys.argv[1])
r = int(sys.argv[2])
except ValueError:
print('n or r must be a integer')
assert n >= r, 'n must be larger or equals to r'
print(combi(n, r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment