Question
PYTHON: In this question, we will implement the class BinaryPositiveInteger, that will be used to represent positive integer numbers, and preform some operations on them.
PYTHON:
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 )''' Appendix A. Player #1, enter your name: >? Arya Player #2, enter your name: >? Sansa Arya's turn: You rolled 3. Your score for this turn is: 3 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 2. Your score for this turn is: 5 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 3. Your score for this turn is: 8 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 6. Your score for this turn is: 14 Roll again? (type 'r' for roll, or 'h' for hold): >? h You scored 14 points this turn. Your total score is 14 Sansa's turn: You rolled 6. Your score for this turn is: 6 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 2. Your score for this turn is: 8 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 3. Your score for this turn is: 11 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 4. Your score for this turn is: 15 Roll again? (type 'r' for roll, or 'h' for hold): >? h You scored 15 points this turn. Your total score is 15 Arya's turn: You rolled 6. Your score for this turn is: 6 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 1. You scored 0 points this turn. Your total score is 14 Sansa's turn: You rolled 6. Your score for this turn is: 6 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 5. Your score for this turn is: 11 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 6. Your score for this turn is: 17 Roll again? (type 'r' for roll, or 'h' for hold): >? h You scored 17 points this turn. Your total score is 32 Arya's turn: You rolled 1. You scored 0 points this turn. Your total score is 14 Sansa's turn: You rolled 2. Your score for this turn is: 2 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 3. Your score for this turn is: 5 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 4. Your score for this turn is: 9 Roll again? (type 'r' for roll, or 'h' for hold): >? h You scored 9 points this turn. Your total score is 98 Arya's turn: You rolled 4. Your score for this turn is: 4 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 3. Your score for this turn is: 7 Roll again? (type 'r' for roll, or 'h' for hold): >? r You rolled 2. Your score for this turn is: 9 Roll again? (type 'r' for roll, or 'h' for hold): >? h You scored 9 points this turn. Your total score is 87 Sansa's turn: You rolled 4. Your score for this turn is: 4 Roll again? (type 'r' for roll, or 'h' for hold): >? h You scored 4 points this turn. Your total score is 102 Sansa won!
Answer Both
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started