blueloveTH 2 år sedan
förälder
incheckning
c79cd6dca6
2 ändrade filer med 11 tillägg och 1 borttagningar
  1. 6 1
      python/cmath.py
  2. 5 0
      tests/10_cmath.py

+ 6 - 1
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:

+ 5 - 0
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)'