Example #1
0
def bin_coeffs(n, start=None, stop=None, step=None):
    def check_number(n):
        if not isinstance(n, int):
            raise TypeError("The number must be an integer.")
        elif n < 0:
            raise ValueError("The number must be positive.")

    check_number(n)

    if start is None:
        start = 0
    else:
        check_number(start)
        if start > n:
            raise ValueError("'start' must be less than or equal to 'n'.")

    if stop is None:
        stop = n
    else:
        check_number(start)
        if stop > n:
            raise ValueError("'stop' must be less than or equal to 'n'.")

    if start > stop:
        if step is None:
            step = -1
        stop -= 1
        k_range = list(range_(start, stop, -1))
    else:
        if step is None:
            step = 1
        stop += 1
        k_range = list(range_(start, stop, 1))

    coeffs = {0: 1, 1: n, n - 1: n, n: 1}
    while start is not None:
        for k in range_(start, stop, step):
            start = None

            try:
                v = coeffs[k]
            except KeyError:
                try:
                    v = coeffs[k - 1] * (n - k + 1) / k
                except KeyError:
                    try:
                        v = coeffs[k + 1] * (k + 1) / (n - k)
                    except KeyError:
                        v = comb_without_rep(n, k)
                coeffs[k] = v
                coeffs[n - k] = v

            s = (yield v)
            if s is not None:
                check_number(s)
                if s not in k_range:
                    raise ValueError("Sent value must be included in the "
                                     "range (start, stop).")
                start = s
                break
Example #2
0
def seq_without_rep(n, k):
    if not isinstance(n, int) or not isinstance(k, int):
        raise TypeError("Both numbers must be integers.")
    elif n < 0 or k < 0:
        raise ValueError("Both numbers must be non-negative integers.")
    elif n < k:
        raise ValueError("'n' must be bigger than 'k'")

    return functools.reduce(operator.mul, range_(n, n - k, -1), 1)
Example #3
0
    def test_gcd(self):
        self.assertEqual(gcd(0, 0), 1)
        self.assertEqual(gcd(5, 0), 5)
        self.assertEqual(gcd(0, 5), 5)
        self.assertEqual(gcd(5, 5), 5)
        self.assertEqual(gcd(5, 1), 1)
        self.assertEqual(gcd(5, 0), 5)
        self.assertEqual(gcd(33, 121), 11)
        self.assertEqual(gcd(256, 160), 32)
        self.assertEqual(gcd(256, -160), 32)
        self.assertEqual(gcd(-256, 160), 32)
        self.assertEqual(gcd(-256, -160), 32)
        self.assertEqual(gcd(Polynomial("x2 - 2x + 1"),
                             Polynomial("x2 - 3x + 2")), Polynomial("x - 1"))
        self.assertEqual(gcd(Polynomial("120x2y4z7t"), Polynomial("24xy5zhg")),
                         Polynomial("24xy4z"))
        self.assertEqual(gcd(4, Polynomial("2x2 - 2x + 2")), 2)
        self.assertEqual(gcd(Polynomial("4"), Polynomial("2x2 - 2x + 2")), 2)
        self.assertEqual(gcd(fractions.Fraction(3, 2),
                             fractions.Fraction(21, 6)),
                         fractions.Fraction(1, 2))
        self.assertEqual(gcd(1.2, 4.6), fractions.Fraction(1, 5))
        self.assertEqual(gcd(1j, 2), 1)
        self.assertEqual(gcd(fractions.Fraction(4, 2), 6), 2)
        self.assertEqual(gcd(2.0, 6), 2)
        self.assertEqual(gcd(-2.0, 6), 2)
        self.assertEqual(gcd(fractions.Fraction(4),
                             Polynomial("2x2 - 2x + 2")), 2)
        self.assertEqual(gcd(8j, 12j), 4j)
        self.assertEqual(gcd(8j, 0), 8j)
        self.assertEqual(gcd(Polynomial("x - 2"), Polynomial("x")),
                         Polynomial("1"))
        self.assertEqual(gcd(30, 24, 54, 36), 6)
        self.assertEqual(gcd(*(2 * x for x in range_(10))), 2)
        self.assertEqual(gcd(Polynomial("x - 2") * "x - 1",
                             Polynomial("x - 1") ** 2,
                             Polynomial("y") * "x - 1",
                             Polynomial("x - 1")), "x - 1")
        #self.assertEqual(gcd(Polynomial("2x - 4") * "x - 1",
        #                     Polynomial("x - 1") ** 2 * 6,
        #                     Polynomial("4y") * "x",
        #                     Polynomial("2x - 2"),
        #                     -2,
        #                     fractions.Fraction(16, 4),
        #                     8.0,
        #                     16), 2)

        self.assertRaises(TypeError, gcd, "3", 6)
        self.assertRaises(TypeError, gcd, 3, "6")
        self.assertRaises(TypeError, gcd, [3], "6")
        self.assertRaises(TypeError, gcd, 3, 6, [])
        self.assertRaises(TypeError, gcd, 3, 6, 9, 18, "")

        self.assertRaises(TypeError, gcd, 3)
        self.assertRaises(TypeError, gcd)
        self.assertRaises(TypeError, gcd, 3, 6, pippo=3)
Example #4
0
 def is_complete(self, letter=None):
     if letter is None:
         return all(self.is_complete(l) for l in self.letters)
     elif not is_letter_valid(letter):
         raise ValueError("The letter must be a single letter from the "
                          "alphabet.")
     elif letter not in self.letters:
         return True
     return set(self.all_letter_degrees[letter]) ==\
            set(range_(self.max_degree_of_letter(letter) + 1))
Example #5
0
 def complete(self, letter=None):
     if letter is None:
         for l in self.letters:
             self.complete(l)
         return
     elif not is_letter_valid(letter):
         raise ValueError("The letter must be a single letter from the "
                          "alphabet.")
     elif letter not in self.letters:
         return
     degrees = set(self.all_letter_degrees[letter])
     for d in set(range_(self.max_degree_of_letter(letter))) - degrees:
         self._monomials.append([0, {letter: d}])
     self.sort()
Example #6
0
    def test_lcm(self):
        self.assertEqual(lcm(0, 0), 0)
        self.assertEqual(lcm(5, 0), 0)
        self.assertEqual(lcm(0, 5), 0)
        self.assertEqual(lcm(5, 5), 5)
        self.assertEqual(lcm(5, 1), 5)
        self.assertEqual(lcm(5, 0), 0)
        self.assertEqual(lcm(33, 121), 363)
        self.assertEqual(lcm(256, 160), 1280)
        self.assertEqual(lcm(256, -160), 1280)
        self.assertEqual(lcm(-256, 160), 1280)
        self.assertEqual(lcm(-256, -160), 1280)
        self.assertEqual(lcm(Polynomial("x2 - 2x + 1"),
                             Polynomial("x2 - 3x + 2")),
                         Polynomial("x - 1") ** 2 * "x - 2")
        self.assertEqual(lcm(Polynomial("120x2y4z7t"), Polynomial("24xy5zhg")),
                         Polynomial("120x2y5z7hgt"))
        self.assertEqual(lcm(4, Polynomial("2x2 - 2x + 2")),
                         Polynomial("4x2 - 4x + 4"))
        self.assertEqual(lcm(Polynomial("4"), Polynomial("2x2 - 2x + 2")),
                         Polynomial("4x2 - 4x + 4"))
        self.assertEqual(lcm(fractions.Fraction(3, 2),
                             fractions.Fraction(21, 6)),
                         fractions.Fraction(21, 2))
        self.assertEqual(lcm(1.2, 4.6), fractions.Fraction(138, 5))
        self.assertEqual(lcm(1j, 2), 2j)
        self.assertEqual(lcm(fractions.Fraction(4, 2), 6), 6)
        self.assertEqual(lcm(2.0, 6), 6)
        self.assertEqual(lcm(-2.0, 6), 6)
        self.assertEqual(lcm(fractions.Fraction(4),
                             Polynomial("2x2 - 2x + 2")),
                         Polynomial("4x2 - 4x + 4"))
        self.assertEqual(lcm(Polynomial("x - 2"), Polynomial("x")),
                         Polynomial("x2 - 2x"))
        self.assertEqual(lcm(32, 24, 54, 36), 864)
        self.assertEqual(lcm(*(2 * x for x in range_(10))), 0)
        self.assertEqual(lcm(*(2 * x for x in range_(1, 10))), 5040)
        #self.assertEqual(lcm(Polynomial("x - 2") * "x - 1",
        #                     Polynomial("x - 1") ** 2,
        #                     Polynomial("y") * "x - 1",
        #                     Polynomial("x - 1")),
        #                 Polynomial("x - 1") ** 3 * "x - 2" * "y")
        #self.assertEqual(lcm(Polynomial("2x - 4") * "x - 1",
        #                     Polynomial("x - 1") ** 2 * 6,
        #                     Polynomial("4y") * "x",
        #                     Polynomial("2x - 2"),
        #                     -2,
        #                     fractions.Fraction(16, 4),
        #                     8.0,
        #                     16),
        #                 Polynomial("x-1") ** 4 * "x-2" * "xy" * 3 * 2 ** 14)

        self.assertRaises(TypeError, lcm, "3", 6)
        self.assertRaises(TypeError, lcm, 3, "6")
        self.assertRaises(TypeError, lcm, [3], "6")
        self.assertRaises(TypeError, lcm, 3, 6, [])
        self.assertRaises(TypeError, lcm, 3, 6, 9, 18, "")

        self.assertRaises(TypeError, lcm, 3)
        self.assertRaises(TypeError, lcm)
        self.assertRaises(TypeError, lcm, 3, 6, pippo=3)
Example #7
0
 def str_divisors(x, abs_=True):
     seq = range_(1, int(abs(x)) + 1)
     if not abs_:
         seq = itertools.chain(seq, (-x for x in seq))
     return (str(d) for d in seq if not x % d)