Ad
Fundamentals
Code
Diff
  • class OrdinalNumeral:
        def __init__(self, n: int):
            if n < 0:
                raise ValueError("n must be positive or 0")
            self.n = n
            self.numeral = self._numeral()
        
        def __repr__(self):
            """
            representation that can be used to recreate the object
            """
            return f"OrdinalNumeral({self.n})"
        
        def __str__(self):
            """
            the 'nice' string version of the class
            """
            return self.numeral
    
        def __int__(self):
            return self.n
        
        def __add__(self, other):
            """
            can implement arithmetic operations if desired
            """
            if (isinstance(other, OrdinalNumeral)):
                return OrdinalNumeral(self.n + other.n)
            elif (isinstance(other, int)):
                return OrdinalNumeral(self.n + other)
            else:
                raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")
            
        def _numeral(self):
            """
            define the ordinal numeral string
            """
            if 11 <= (self.n % 100) <= 13:
                return str(self.n)+"th"
            return str(self.n)+("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]
    
    • class OrdinalNumeral:
    • def __init__(self, n: int):
    • if n < 0:
    • raise ValueError("n must be positive or 0")
    • self.n = n
    • self.numeral = self._numeral()
    • def __repr__(self):
    • """
    • representation that can be used to recreate the object
    • """
    • return f"OrdinalNumeral({self.n})"
    • def __str__(self):
    • """
    • the 'nice' string version of the class
    • """
    • return self.numeral
    • def __int__(self):
    • return self.n
    • def __add__(self, other):
    • """
    • can implement arithmetic operations if desired
    • """
    • if (isinstance(other, OrdinalNumeral)):
    • return OrdinalNumeral(self.n + other.n)
    • elif (isinstance(other, int)):
    • return OrdinalNumeral(self.n + other)
    • else:
    • raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")
    • def _numeral(self):
    • """
    • define the ordinal numeral string
    • """
    • if 11 <= (self.n % 100) <= 13:
    • return str(self.n)+"th"
    • return str(self.n)+("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]
Fundamentals
Code
Diff
  • class OrdinalNumeral:
        def __init__(self, n: int):
            if n < 0:
                raise ValueError("n must be positive or 0")
            self.n = n
            self.numeral = self._numeral()
        
        def __repr__(self):
            return self.numeral
        
        def __add__(self, other):
            """
            can implement arithmetic operations if desired
            """
            if (isinstance(other, OrdinalNumeral)):
                return OrdinalNumeral(self.n + other.n)
            elif (isinstance(other, int)):
                return OrdinalNumeral(self.n + other)
            else:
                raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")
            
        def _numeral(self):
            """
            define the ordinal numeral string
            """
            if 11 <= (self.n % 100) <= 13:
                return str(self.n)+"th"
            return str(self.n)+("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]
    
    • class OrdinalNumbers:
    • def __init__(self, n):
    • class OrdinalNumeral:
    • def __init__(self, n: int):
    • if n < 0:
    • raise ValueError("n must be positive or 0")
    • self.n = n
    • def solution(self):
    • ht = {1: 'st', 2: 'nd', 3: 'rd'}
    • last_digit = self.n % 10
    • if self.n % 100 in [11, 12, 13] or last_digit not in [1,2,3]:
    • return str(self.n) + 'th'
    • elif last_digit in [1,2,3]:
    • return str(self.n) + ht[last_digit]
    • self.numeral = self._numeral()
    • def __repr__(self):
    • return self.numeral
    • def __add__(self, other):
    • """
    • can implement arithmetic operations if desired
    • """
    • if (isinstance(other, OrdinalNumeral)):
    • return OrdinalNumeral(self.n + other.n)
    • elif (isinstance(other, int)):
    • return OrdinalNumeral(self.n + other)
    • else:
    • raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")
    • def _numeral(self):
    • """
    • define the ordinal numeral string
    • """
    • if 11 <= (self.n % 100) <= 13:
    • return str(self.n)+"th"
    • return str(self.n)+("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]