Question: Python pls 2a. (25 pts) Some programming languages don't draw a distinction between integers (5) and strings that represent integers ('5'). For example, in such

 Python pls 2a. (25 pts) Some programming languages don't draw a

Python pls

2a. (25 pts) Some programming languages don't draw a distinction between integers (5) and strings that represent integers ('5'). For example, in such languages we can add 2+'5' or even '5'+'5'. In this problem you will define a Python class that stores such values and can operate on them in the ways specified below. Note that int (5) and int('5') both return the int 5, but int('abc') raises a ValueError exception (there is no int equivalent to that str). Also note that str(5) and str('5') both return the str '5'. Define the core of a StrInt class by writing the following methods needed to implement its requirements. (1) int_convertable: a static helper method that returns True when the int conversion function can be called on its argument successfully (without raising an exception), otherwise return false. So, int_convertable (5) and int_convertable('5') both return True; int_convertable('abc') returns False. int_convertable itself always returns True or False: it never raises an exception. Hint: call int_convertable in later methods. (2) constructor: its argument must be an int or str that represents an int; otherwise raise an AssertionError. Define an attribute named value: if a str argument, it stores the str argument; if an int argument, it stores the str equivalent of the int. Requirement: Define only this attribute for objects in the class. Construction Result StrInt(5) StrInt: value stores '5' StrInt('5') StrInt: value stores '5' StrInt('abc) raises AssertionError Assume this class also defines the following conversion functions, which you can call in the add operator (3). def_str_(self): return self.value def_int__(self): return int (self.value) (3) add operator: when a StrInt is added to a strInt, int, or a str that represents an int the result a StrInt representing the int sum of the operands. When a StrInt is added to a str that does not represent an int, the result is the concatenation of the operands. For all other types of operands, the add operator is not implemented. Here are examples of each: note StrInt(5) and StrInt('5') each produce the same StrInt object. or or Expression using addition Result: of addition StrInt(5) + StrInt (3) StrInt(8) StrInt(5) + 3 3 + StrInt(5) StrInt(8) StrInt(5) + '3' 13' + StrInt(5) StrInt(8) StrInt(5) + 'abc 5abc' abc' + StrInt(5) abc5 StrInt(5) + [] or [] + StrInt(5) addition not implemented for list or any other type Think of the simplest logic structure that will detect all these cases and compute their correct answers. Hint: addition is not always commutative because concatenation is not commutative: '3'+'a' != 'a'+'3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!