Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this question, we will implement the class BinaryPositiveInteger, that will be used to represent positive integer numbers, and preform some operations on them. The

In this question, we will implement the class BinaryPositiveInteger, that will be used to represent positive integer numbers, and preform some operations on them. The BinaryPositiveInteger objects will represent positive integers by their binary (base 2) representation. That is, each object will have a data-member of type string, containing the binary representation of the number. For example, the object representing the number 13, will have '1101', as its string datamember.

Complete the definition of the following BinaryPositiveInteger class: class BinaryPositiveInteger:

def __init__(self, num): ''' Initializes a BinaryPositiveInteger object representing the value given in the integer num'''

def __add__(self, other): ''' Creates and returns a BinaryPositiveInteger object that represent the sum of self and other (also of type BinaryPositiveInteger)'''

def __lt__(self, other): ''' returns True if self is less than other, or False otherwise'''

def is_power_of_2(self): ''' returns True if self is a power of 2, or False otherwise'''

def largest_power_of_2(self): ''' returns the largest power of 2 that is less than or equal to self'''

def __repr__(self): ''' Creates and returns the string representation of self. The string representation starts with 0b, followed by a sequence of 0s and 1s'''

For example, after implementing the BinaryPositiveInteger class, you should expect the following behavior:

>>> n1 = BinaryPositiveInteger(13)

>>> n1 0b1101

>>> n2 = BinaryPositiveInteger(25)

>>> n2 0b11001

>>> n1.is_power_of_2() False

>>> n1.largest_power_of_2() 8

>>> n1 < n2 True

>>> n1 + n2 0b100110

Implementation Requirements: 1. Each BinaryPositiveInteger object should have only one data-member (which is the string with the binary representation of the number).

2. You are not allowed to use the int() and bin() functions, as well as the format method of the str class.

3. In the __init__ method you should convert the given int to its binary representation. From that point, all the other methods (__add__, __lt__, is_power_of_2, largest_power_of_2 and __repr__) should make their computations in the binary representation, and should not convert the numbers back to ints. Specifically, when adding two BinaryPositiveInteger objects, you should implement the Elementary School technique.

Extra Credit: Support also the multiplication of two BinaryPositiveInteger objects (by implementing the Elementary School multiplication technique):

def __mul__(self, other): ''' Creates and returns a BinaryPositiveInteger object that represent the multiplication of self and other (also of type BinaryPositiveInteger )'''

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

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

Students also viewed these Databases questions

Question

Describe two types of mediation.

Answered: 1 week ago