def combinations_with_replacement(self, r: int): """See itertools.combinations_with_replacement_ .. _itertools.combinations_with_replacement: https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement """ # noqa this_len = len_or_none(self) if this_len is None: length = None else: length = nCr(this_len, r, True) return FIt(combinations_with_replacement(self, r), length)
def test_nCr_edges(): with pytest.raises(ValueError): nCr(-1, 1) with pytest.raises(ValueError): nCr(1, -1) assert nCr(0, 0) == 0 assert nCr(2, 3) == 0
def combinations(self, r: int, replace=False): """See itertools.combinations_ Difference from stdlib: ``replace`` argument to use itertools.combinations_with_replacement_ .. _itertools.combinations: https://docs.python.org/3/library/itertools.html#itertools.combinations .. _itertools.combinations_with_replacement: https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement """ # noqa if replace: return FIt.combinations_with_replacement(self, r) this_len = len_or_none(self) if this_len is None: length = None else: length = nCr(this_len, r) return FIt(combinations(self, r), length)
def test_nCr_replace(): assert nCr(3, 2, True) == 6
def test_nCr(): assert nCr(3, 2) == 3