Project 2 · Practice
Quiz & exercises
Answer each question to see the explanation. Your best score is saved in this browser.
Quiz
Coding exercises
Write your code, then press Check (or Shift+Enter) to run the tests. Drafts are saved as you type.
Exercise 1: Goal 1: construction and representation ✓ Passed
Write the class Mod(value, modulus):
- Both arguments must be integers, otherwise raise TypeError. The modulus must be positive, otherwise raise ValueError. - Store the residue value % modulus, so Mod(8, 3).value is 2 and Mod(-1, 5).value is 4. - value and modulus are read-only properties. - repr(Mod(8, 3)) is 'Mod(2, 3)' and int(Mod(8, 3)) is 2.
class Mod:
def __init__(self, value, modulus):
# validate, then store the residue
self.value = value
self.modulus = modulus
def test_residue():
"""stores the residue as value"""
assert Mod(8, 3).value == 2, f"Mod(8, 3).value is {Mod(8, 3).value!r}"
assert Mod(-1, 5).value == 4, "negative values should wrap around: Mod(-1, 5).value == 4"
assert Mod(3, 3).value == 0
assert Mod(8, 3).modulus == 3
def test_validation_types():
"""non-int value or modulus raises TypeError"""
for args in [(1.5, 3), ('1', 3), (1, 3.0), (1, None)]:
try:
Mod(*args)
except TypeError:
pass
else:
raise AssertionError(f"Mod{args} should raise TypeError")
def test_validation_modulus():
"""a modulus <= 0 raises ValueError"""
for m in (0, -3):
try:
Mod(1, m)
except ValueError:
pass
else:
raise AssertionError(f"Mod(1, {m}) should raise ValueError")
def test_read_only():
"""value and modulus are read-only properties"""
m = Mod(1, 3)
for name in ('value', 'modulus'):
try:
setattr(m, name, 2)
except AttributeError:
pass
else:
raise AssertionError(f"assigning to .{name} should raise AttributeError")
def test_repr_int():
"""repr and int()"""
assert repr(Mod(8, 3)) == 'Mod(2, 3)', f"repr is {repr(Mod(8, 3))!r}"
assert int(Mod(14, 12)) == 2
Hints (3)
- Validate first, then store
value % modulus. Python's%already returns a non-negative result for a positive modulus, even for negative values. - Store the data in
_valueand_modulusand expose them with@propertymethods that have no setter: assigning to a property without a setter raisesAttributeError. int(m)callsm.__int__(), which must return anint.
Show a solution
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.valueExercise 2: Goal 2: equality, hashing and ordering ✓ Passed
The class from goal 1 is given. Add:
- Equality by congruence: a Mod equals a Mod with the same modulus when the residues match, and equals an int congruent to it (Mod(8, 3) == 5, and also 5 == Mod(8, 3)). For a different modulus or any other type, __eq__ must return NotImplemented, so == gives False. - Hashing: congruent Mod objects with the same modulus have the same hash. - Ordering <, <=, >, >= by residue, against a Mod with the same modulus or an int (use its residue). Use functools.total_ordering; incompatible operands must lead to a TypeError.
from functools import total_ordering
@total_ordering
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.value
# your code here: equality, hashing and __lt__
def test_congruence():
"""Mods with the same modulus are equal when congruent"""
assert Mod(8, 3) == Mod(2, 3)
assert Mod(8, 3) != Mod(1, 3)
def test_equal_to_int():
"""a Mod equals a congruent int, on either side"""
assert Mod(8, 3) == 5, "Mod(8, 3) == 5 should be True"
assert 5 == Mod(8, 3), "5 == Mod(8, 3) should be True"
assert Mod(8, 3) != 4
def test_incompatible_eq():
"""different modulus or other types: NotImplemented, so == is False"""
assert Mod(2, 3).__eq__(Mod(2, 7)) is NotImplemented, "__eq__ should return NotImplemented for a different modulus"
assert Mod(2, 3).__eq__('2') is NotImplemented, "__eq__ should return NotImplemented for a str"
assert (Mod(2, 3) == Mod(2, 7)) is False
assert (Mod(2, 3) == 'two') is False
def test_hash():
"""hashable, and congruent Mods have equal hashes"""
assert hash(Mod(2, 3)) == hash(Mod(5, 3))
assert len({Mod(2, 3), Mod(5, 3), Mod(2, 7)}) == 2
def test_ordering():
"""<, <=, >, >= compare residues"""
assert Mod(1, 5) < Mod(3, 5)
assert Mod(4, 5) >= 9, "9 has residue 4, so Mod(4, 5) >= 9"
assert Mod(4, 5) > 3 and Mod(3, 5) <= 3 and not (Mod(4, 5) < 4)
assert 3 > Mod(1, 5)
assert sorted([Mod(4, 5), Mod(0, 5), Mod(7, 5)]) == [Mod(0, 5), Mod(2, 5), Mod(4, 5)]
def test_ordering_incompatible():
"""ordering with a different modulus raises TypeError"""
try:
Mod(1, 5) < Mod(1, 7)
except TypeError:
pass
else:
raise AssertionError("Mod(1, 5) < Mod(1, 7) should raise TypeError")
Hints (3)
- Write a helper
_residue(other)that returns the residue of anint, thevalueof aModwith the same modulus, orNonewhenotheris incompatible. Every comparison method then starts the same way. - Return
NotImplemented(don't raise) for incompatible operands: then5 == Mod(8, 3)is reflected to your__eq__, andMod(2, 3) == 'two'quietly givesFalse. - Defining
__eq__removes the inherited hash. Hash the same data that__eq__uses:hash((self.value, self.modulus)). Then decorate the class with@total_orderingand write only__lt__.
Show a solution
from functools import total_ordering
@total_ordering
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.value
def _residue(self, other):
if isinstance(other, Mod):
return other.value if other.modulus == self.modulus else None
if isinstance(other, int):
return other % self.modulus
return None
def __eq__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value == r
def __hash__(self):
return hash((self.value, self.modulus))
def __lt__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value < rExercise 3: Goal 3: arithmetic and reflected operators ✓ Passed
Goals 1 and 2 are given. Add arithmetic that returns new Mod objects:
- +, - and * with a Mod of the same modulus or with an int, with the int on either side: Mod(2, 5) + 4, 3 + Mod(2, 5) and 3 - Mod(1, 5) (which is Mod(2, 5)) all work. - ** with an int exponent, including negative ones: Mod(3, 7) ** -1 is Mod(5, 7) (the modular inverse). A non-int exponent is unsupported. - unary -: -Mod(2, 5) is Mod(3, 5). - A different modulus or an unsupported type must raise TypeError, so return NotImplemented from your methods in those cases.
import operator
from functools import total_ordering
@total_ordering
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.value
def _residue(self, other):
if isinstance(other, Mod):
return other.value if other.modulus == self.modulus else None
if isinstance(other, int):
return other % self.modulus
return None
def __eq__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value == r
def __hash__(self):
return hash((self.value, self.modulus))
def __lt__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value < r
# your code here: +, -, *, ** (and the reflected versions), unary -
def expect_type_error(fn, label):
try:
fn()
except TypeError:
return
raise AssertionError(f"{label} should raise TypeError")
def test_with_mods():
"""+, -, * with a Mod of the same modulus"""
a, b = Mod(2, 5), Mod(4, 5)
assert a + b == Mod(1, 5), f"got {a + b!r}"
assert a - b == Mod(3, 5), f"got {a - b!r}"
assert a * b == Mod(3, 5), f"got {a * b!r}"
assert isinstance(a + b, Mod) and (a + b).modulus == 5
def test_with_ints_on_the_right():
"""+, -, * with an int on the right"""
assert Mod(2, 5) + 4 == Mod(1, 5)
assert Mod(2, 5) - 3 == Mod(4, 5)
assert Mod(3, 5) * 4 == Mod(2, 5)
def test_reflected():
"""ints on the left work too, and 3 - Mod(1, 5) is 3 - 1"""
assert 3 + Mod(2, 5) == Mod(0, 5), "3 + Mod(2, 5) failed: implement __radd__"
assert 2 * Mod(3, 5) == Mod(1, 5), "2 * Mod(3, 5) failed: implement __rmul__"
r = 3 - Mod(1, 5)
assert isinstance(r, Mod) and r == Mod(2, 5), f"3 - Mod(1, 5) gave {r!r}, expected Mod(2, 5)"
def test_power():
"""** with an int exponent, including negative (modular inverse)"""
assert Mod(2, 5) ** 5 == Mod(2, 5), "2**5 = 32, which is 2 mod 5 (don't reduce the exponent!)"
assert Mod(3, 7) ** 0 == Mod(1, 7)
assert Mod(3, 7) ** -1 == Mod(5, 7), "use pow(base, exp, mod): pow(3, -1, 7) is 5"
def test_negation():
"""unary minus"""
assert -Mod(2, 5) == Mod(3, 5)
def test_incompatible():
"""different modulus or unsupported types raise TypeError"""
expect_type_error(lambda: Mod(1, 5) + Mod(1, 7), "Mod(1, 5) + Mod(1, 7)")
expect_type_error(lambda: Mod(1, 5) * 1.5, "Mod(1, 5) * 1.5")
expect_type_error(lambda: 'a' - Mod(1, 5), "'a' - Mod(1, 5)")
expect_type_error(lambda: Mod(2, 5) ** Mod(1, 5), "Mod(2, 5) ** Mod(1, 5)")
assert Mod(1, 5).__add__(1.5) is NotImplemented, "__add__ should return NotImplemented for a float"
def test_new_objects():
"""operators return new objects and leave the operands unchanged"""
a = Mod(2, 5)
b = a + 1
assert b is not a and a == Mod(2, 5)
Hints (3)
- A generic
_op(self, other, op, *, reflected=False)usingoperator.add,operator.subandoperator.mulavoids writing the same checks six times. ReturnMod(op(a, b), self.modulus)and let the constructor reduce the result. - For the reflected methods the
intis the left operand:3 - Mod(1, 5)callsMod(1, 5).__rsub__(3)and must compute3 - 1, not1 - 3. - For powers use the built-in
pow(self.value, exp, self.modulus): it's fast, and since Python 3.8 a negative exponent gives the modular inverse. Never reduce the exponent modulo the modulus.
Show a solution
import operator
from functools import total_ordering
@total_ordering
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.value
def _residue(self, other):
if isinstance(other, Mod):
return other.value if other.modulus == self.modulus else None
if isinstance(other, int):
return other % self.modulus
return None
def __eq__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value == r
def __hash__(self):
return hash((self.value, self.modulus))
def __lt__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value < r
def _op(self, other, op, *, reflected=False):
r = self._residue(other)
if r is None:
return NotImplemented
a, b = (r, self.value) if reflected else (self.value, r)
return Mod(op(a, b), self.modulus)
def __add__(self, other):
return self._op(other, operator.add)
def __radd__(self, other):
return self._op(other, operator.add, reflected=True)
def __sub__(self, other):
return self._op(other, operator.sub)
def __rsub__(self, other):
return self._op(other, operator.sub, reflected=True)
def __mul__(self, other):
return self._op(other, operator.mul)
def __rmul__(self, other):
return self._op(other, operator.mul, reflected=True)
def __pow__(self, exp):
if not isinstance(exp, int):
return NotImplemented
return Mod(pow(self.value, exp, self.modulus), self.modulus)
def __neg__(self):
return Mod(-self.value, self.modulus)Exercise 4: Goal 4: in-place operators ✓ Passed
Goals 1–3 are given. Add +=, -=, *= and **= so that they modify the object in place (its identity stays the same, and other names referring to it see the change). The operands allowed are the same as for the regular operators, the stored value must remain the residue, and incompatible operands must raise TypeError without changing the object.
import operator
from functools import total_ordering
@total_ordering
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.value
def _residue(self, other):
if isinstance(other, Mod):
return other.value if other.modulus == self.modulus else None
if isinstance(other, int):
return other % self.modulus
return None
def __eq__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value == r
def __hash__(self):
return hash((self.value, self.modulus))
def __lt__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value < r
def _op(self, other, op, *, reflected=False):
r = self._residue(other)
if r is None:
return NotImplemented
a, b = (r, self.value) if reflected else (self.value, r)
return Mod(op(a, b), self.modulus)
def __add__(self, other):
return self._op(other, operator.add)
def __radd__(self, other):
return self._op(other, operator.add, reflected=True)
def __sub__(self, other):
return self._op(other, operator.sub)
def __rsub__(self, other):
return self._op(other, operator.sub, reflected=True)
def __mul__(self, other):
return self._op(other, operator.mul)
def __rmul__(self, other):
return self._op(other, operator.mul, reflected=True)
def __pow__(self, exp):
if not isinstance(exp, int):
return NotImplemented
return Mod(pow(self.value, exp, self.modulus), self.modulus)
def __neg__(self):
return Mod(-self.value, self.modulus)
# your code here: +=, -=, *=, **=
def test_iadd_mutates():
"""+= changes the object itself"""
m = Mod(1, 5)
alias = m
m += 7
assert m is alias, "+= should modify the object in place, not create a new one"
assert m == Mod(3, 5), f"got {m!r}"
def test_all_inplace_ops():
"""-=, *=, **= work in place with Mods and ints"""
m = Mod(4, 7)
before = id(m)
m -= Mod(1, 7)
assert m == 3
m *= 3
assert m == 2
m **= 3
assert m == 1
m += Mod(2, 7)
assert m == 3
assert id(m) == before, "one of the in-place operators created a new object"
def test_value_stays_residue():
"""the stored value stays in range(modulus)"""
m = Mod(4, 5)
m += 100
assert m.value == 4, f"value is {m.value}"
m -= 10
assert m.value == 4, f"value is {m.value}"
def test_inplace_incompatible():
"""incompatible operands raise TypeError and leave the object unchanged"""
m = Mod(2, 5)
for op in ('m += 1.5', 'm *= Mod(1, 7)', 'm **= Mod(1, 5)'):
try:
exec(op, {'m': m, 'Mod': Mod})
except TypeError:
pass
else:
raise AssertionError(f"{op} should raise TypeError")
assert m == Mod(2, 5), f"m changed to {m!r}"
def test_inplace_returns_self():
"""the in-place methods return the object itself"""
m = Mod(1, 3)
assert m.__iadd__(1) is m
assert m.__iadd__('x') is NotImplemented, "__iadd__ should return NotImplemented for a str"
Hints (3)
__iadd__must changeselfand thenreturn self: Python rebinds the name on the left of+=to whatever the method returns.- Reuse your regular operators:
result = self.__add__(other)is either a newMod(copy itsvalueintoself._value) orNotImplemented(return it unchanged, and Python raises theTypeErrorfor you). - Call the method (
self.__add__(other)) rather than writingself + other: the expression would raise inside__iadd__instead of returningNotImplemented.
Show a solution
import operator
from functools import total_ordering
@total_ordering
class Mod:
def __init__(self, value, modulus):
if not isinstance(modulus, int):
raise TypeError('Unsupported type for modulus')
if not isinstance(value, int):
raise TypeError('Unsupported type for value')
if modulus <= 0:
raise ValueError('Modulus must be positive')
self._modulus = modulus
self._value = value % modulus
@property
def modulus(self):
return self._modulus
@property
def value(self):
return self._value
def __repr__(self):
return f'Mod({self.value}, {self.modulus})'
def __int__(self):
return self.value
def _residue(self, other):
if isinstance(other, Mod):
return other.value if other.modulus == self.modulus else None
if isinstance(other, int):
return other % self.modulus
return None
def __eq__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value == r
def __hash__(self):
return hash((self.value, self.modulus))
def __lt__(self, other):
r = self._residue(other)
return NotImplemented if r is None else self.value < r
def _op(self, other, op, *, reflected=False):
r = self._residue(other)
if r is None:
return NotImplemented
a, b = (r, self.value) if reflected else (self.value, r)
return Mod(op(a, b), self.modulus)
def __add__(self, other):
return self._op(other, operator.add)
def __radd__(self, other):
return self._op(other, operator.add, reflected=True)
def __sub__(self, other):
return self._op(other, operator.sub)
def __rsub__(self, other):
return self._op(other, operator.sub, reflected=True)
def __mul__(self, other):
return self._op(other, operator.mul)
def __rmul__(self, other):
return self._op(other, operator.mul, reflected=True)
def __pow__(self, exp):
if not isinstance(exp, int):
return NotImplemented
return Mod(pow(self.value, exp, self.modulus), self.modulus)
def __neg__(self):
return Mod(-self.value, self.modulus)
def _iop(self, result):
if result is NotImplemented:
return NotImplemented
self._value = result.value
return self
def __iadd__(self, other):
return self._iop(self.__add__(other))
def __isub__(self, other):
return self._iop(self.__sub__(other))
def __imul__(self, other):
return self._iop(self.__mul__(other))
def __ipow__(self, exp):
return self._iop(self.__pow__(exp))