From c79cd6dca64785a79754ac9336fe3ee4779de2ce Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 2 Jan 2024 00:36:23 +0800 Subject: [PATCH] ... --- python/cmath.py | 7 ++++++- tests/10_cmath.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/python/cmath.py b/python/cmath.py index 81029bbc..6845d94a 100644 --- a/python/cmath.py +++ b/python/cmath.py @@ -17,7 +17,12 @@ class complex: return complex(self.real, -self.imag) def __repr__(self): - return f"({self.real}+{self.imag}j)" + s = ['(', str(self.real)] + if self.imag >= 0: + s.append('+') + s.append(str(self.imag)) + s.append('j)') + return ''.join(s) def __eq__(self, other): if type(other) is complex: diff --git a/tests/10_cmath.py b/tests/10_cmath.py index e4cd077c..f7ba9af8 100644 --- a/tests/10_cmath.py +++ b/tests/10_cmath.py @@ -20,3 +20,8 @@ res = sqrt(1+2j) assert isclose(res, 1.272019649514069+0.7861513777574233j) assert {1+2j: 1}[1+2j] == 1 + +assert repr(1+2j) == '(1.0+2.0j)' +assert repr(1+0j) == '(1.0+0.0j)' +assert repr(-1-3j) == '(-1.0-3.0j)' +assert repr(1-3j) == '(1.0-3.0j)'