Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will implement a class ModInt that implements integers with a specified modulus. We can then create numbers modulo 7 using: x = ModInt(7, modulus=10)

image text in transcribed

We will implement a class ModInt that implements integers with a specified modulus. We can then create numbers modulo 7 using: x = ModInt(7, modulus=10) y = ModInt(5, modulus-10) and if we do x + y, we should obtain a number that is equal to ModInt(2, modulus=10), because: (5+7) mod 10 = 12 mod 10 = 2. In other words, to computery for x,y that are Modint, you do like this: . First, you check that both x and y share the same modulus (10 in the example above); if they do not, you raise a TypeError exception. Second, you compute ry as if x and y were integers, and then you compute the result mod n, where n is the common modulus of x and y Some implementation notes: To computer mod n, you write in Python x % n. To raise a TypeError you can simply do: raise TypeError("Operation between numbers with different modulus") We will have you implement only the +, -, * operators, as well as the integer division //, which is implemented via the _floordiv_ operator You might want to refer to the implementation of complex in the class book chapter for an example. 1 class Mod Int(object): 2 def __init_(self, x, modulus-10): 3 ***"Creates an integer with a specified modulus." 4 assert modulus > 0 5 self.x = x % modulus 6 self.modulus = modulus 7 8 def eq_(self, other): 9 *****We define equality, so that we can easily write tests." 10 return self.x == other.x and self.modulus == other.modulus 11 12 def __repr_(self): 13 "To print them in a meaningful way" 14 return "(c) mod ().format(self.x, self.modulus) 15 16 # Here you have to add the class methods to make +, -, // work, 17 ### YOUR CODE HERE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions