Fundamentals
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")- 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})"- """representation that can be used to recreate the object"""
- return f"OrdinalNumeral(self.n)"
- def __str__(self):
"""the 'nice' string version of the class"""- """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")- """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"- """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)]
import codewars_test as test from solution import OrdinalNumeral @test.describe("Example") def test_group(): @test.it("test case 1: (0-10)") def test_case(): samples = [n for n in range(0, 11)] expected = ['0th', '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th'] for s, e in zip(samples, expected): test.assert_equals(OrdinalNumeral(s).numeral, e) @test.it("test case 2: (11-30)") def test_case(): samples = [n for n in range(11, 31)] expected = ['11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th'] for s, e in zip(samples, expected): test.assert_equals(OrdinalNumeral(s).numeral, e) @test.it("test case 3: Random") def test_case(): samples = [776, 411, 1001100, 99999999, 567090803542432, 33197390315682527] expected = ['776th', '411th', '1001100th', '99999999th', '567090803542432nd', '33197390315682527th'] for s, e in zip(samples, expected): test.assert_equals(OrdinalNumeral(s).numeral, e) @test.it("test case 4: Addition with ints") def test_case(): samples = [(0,1), (1,1), (1,2), (100,1), (200,2), (300,3), (100,11)] expected = ['1st', '2nd', '3rd', '101st', '202nd', '303rd', '111th'] for s,e in zip(samples, expected): test.assert_equals((OrdinalNumeral(s[0]) + s[1]).numeral, e) @test.it("test case 5: error handling") def test_case(): def negative_numeral(): return OrdinalNumeral(-1) def numeral_plus_float(): return OrdinalNumeral(1) + 1.5 test.expect_error("n must be positive or 0", negative_numeral, exception=ValueError) test.expect_error("Unable to add float with OrdinalNumeral", numeral_plus_float, exception=TypeError)
- import codewars_test as test
- from solution import OrdinalNumeral
- @test.describe("Example")
- def test_group():
- @test.it("test case 1: (0-10)")
- def test_case():
- samples = [n for n in range(0, 11)]
- expected = ['0th', '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th']
for s, e in zip(samples, expected):test.assert_equals(OrdinalNumeral(s).numeral, e)- for s, e in zip(samples, expected): test.assert_equals(OrdinalNumeral(s).numeral, e)
- @test.it("test case 2: (11-30)")
- def test_case():
- samples = [n for n in range(11, 31)]
- expected = ['11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th']
for s, e in zip(samples, expected):test.assert_equals(OrdinalNumeral(s).numeral, e)- for s, e in zip(samples, expected): test.assert_equals(OrdinalNumeral(s).numeral, e)
- @test.it("test case 3: Random")
- def test_case():
- samples = [776, 411, 1001100, 99999999, 567090803542432, 33197390315682527]
- expected = ['776th', '411th', '1001100th', '99999999th', '567090803542432nd', '33197390315682527th']
for s, e in zip(samples, expected):test.assert_equals(OrdinalNumeral(s).numeral, e)- for s, e in zip(samples, expected): test.assert_equals(OrdinalNumeral(s).numeral, e)
- @test.it("test case 4: Addition with ints")
- def test_case():
- samples = [(0,1), (1,1), (1,2), (100,1), (200,2), (300,3), (100,11)]
- expected = ['1st', '2nd', '3rd', '101st', '202nd', '303rd', '111th']
for s,e in zip(samples, expected):s1,s2 = stest.assert_equals((OrdinalNumeral(s1) + s2).numeral, e)- for s,e in zip(samples, expected): test.assert_equals((OrdinalNumeral(s[0]) + s[1]).numeral, e)
- @test.it("test case 5: error handling")
- def test_case():
def negative_numeral():return OrdinalNumeral(-1)def numeral_plus_float():return OrdinalNumeral(1) + 1.5- def negative_numeral(): return OrdinalNumeral(-1)
- def numeral_plus_float(): return OrdinalNumeral(1) + 1.5
- test.expect_error("n must be positive or 0", negative_numeral, exception=ValueError)
- test.expect_error("Unable to add float with OrdinalNumeral", numeral_plus_float, exception=TypeError)