Skip to content

Commit

Permalink
fixed oop tests, some relsym attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Nov 23, 2018
1 parent 73cf33f commit 3c67de4
Show file tree
Hide file tree
Showing 9 changed files with 905 additions and 38 deletions.
52 changes: 27 additions & 25 deletions exercises/oop/ComplexNumber_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def phase(self):
https://en.wikipedia.org/wiki/Complex_number#Absolute_value_and_argument
"""
return math.atan2(self.imaginary, self.real)

def magnitude(self):
""" Returns a float which is the magnitude (that is, the absolute value) of the complex number
This method is something we introduce by ourselves, according to the definition:
https://en.wikipedia.org/wiki/Complex_number#Absolute_value_and_argument
"""
#jupman-raise
return math.sqrt(self.real**2 + self.imaginary**2)
#/jupman-raise


def log(self, base):
""" Returns another ComplexNumber which is the logarithm of this complex number
Expand All @@ -28,34 +39,23 @@ def log(self, base):
return ComplexNumber(math.log(self.real) / math.log(base), self.phase() / math.log(base))


def magnitude(self):
""" Returns a float which is the magnitude (that is, the absolute value) of the complex number
This method is something we introduce by ourselves, according to the definition:
https://en.wikipedia.org/wiki/Complex_number#Absolute_value_and_argument
def isclose(self, c, delta):
""" Returns True if the complex number is within a delta distance from complex number c.
"""
#jupman-raise
return math.sqrt(self.real**2 + self.imaginary**2)
return math.sqrt((self.real-c.real)**2 + (self.imaginary-c.imaginary)**2) < delta
#/jupman-raise


def __eq__(self, other):
# subtitute this with more precise code using the properties of the object
return self is other
def __eq__(self, other):
#jupman-strip
return self.real == other.real and self.imaginary == other.imaginary
#/jupman-strip
# subtitute this with more precise code using the properties of the object
return self is other

def isclose(self, c, delta):
""" Returns True if the complex number is within a delta distance from complex number c.
"""
#jupman-raise
return math.sqrt((self.real-c.real)**2 + (self.imaginary-c.imaginary)**2) < delta
#/jupman-raise

def __add__(self, other):
# subtitute this with more precise code using the properties of the object
return NotImplemented
def __add__(self, other):
#jupman-strip
if isinstance(other, ComplexNumber):
return ComplexNumber(self.real + other.real,self.imaginary + other.imaginary)
Expand All @@ -64,21 +64,21 @@ def __add__(self, other):
else:
return NotImplemented
#/jupman-strip

def __radd__(self, other):
# subtitute this with more precise code using the properties of the object
return NotImplemented

def __radd__(self, other):

#jupman-strip
if (type(other) is int or type(other) is float):
return ComplexNumber(self.real + other, self.imaginary)
else:
return NotImplemented
#/jupman-strip

def __mul__(self, other):
# subtitute this with more precise code using the properties of the object
return NotImplemented

def __mul__(self, other):

#jupman-strip
if isinstance(other, ComplexNumber):
Expand All @@ -89,14 +89,16 @@ def __mul__(self, other):
else:
return NotImplemented
#/jupman-strip

def __rmul__(self, other):
# subtitute this with more precise code using the properties of the object
return NotImplemented

def __rmul__(self, other):

#jupman-strip
if (type(other) is int or type(other) is float):
return ComplexNumber(self.real * other, self.imaginary * other)
else:
return NotImplemented
#/jupman-strip
#/jupman-strip
# subtitute this with more precise code using the properties of the object
return NotImplemented
19 changes: 18 additions & 1 deletion exercises/oop/ComplexNumber_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_02_add_numbers(self):
self.assertEquals(ComplexNumber(1,2) + ComplexNumber(3,4), ComplexNumber(4,6));

class RaddTest(unittest.TestCase):
def test_01_add_scalar_right(self):
def test_01_add_scalar_right(self):
self.assertEquals(ComplexNumber(1,2) + 3, ComplexNumber(4,2));

def test_02_add_scalar_left(self):
Expand All @@ -85,3 +85,20 @@ def test_02_add_scalar_left(self):
def test_03_add_negative(self):
self.assertEquals(ComplexNumber(-1,0) + ComplexNumber(0,-1), ComplexNumber(-1,-1));

class MulTest(unittest.TestCase):

def test_01_mul_by_zero(self):
self.assertEquals(ComplexNumber(0,0) * ComplexNumber(1,2), ComplexNumber(0,0));

def test_02_mul_just_real(self):
self.assertEquals(ComplexNumber(1,0) * ComplexNumber(2,0), ComplexNumber(2,0));

def test_03_mul_just_imaginary(self):
self.assertEquals(ComplexNumber(0,1) * ComplexNumber(0,2), ComplexNumber(-2,0));

def test_04_mul_scalar_right(self):
self.assertEquals(ComplexNumber(1,2) * 3, ComplexNumber(3,6));

def test_05_mul_scalar_left(self):
self.assertEquals(3 * ComplexNumber(1,2), ComplexNumber(3,6));

33 changes: 29 additions & 4 deletions exercises/oop/oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7fa9bc769320>\n"
"<__main__.ComplexNumber object at 0x7f03c052d2e8>\n"
]
}
],
Expand Down Expand Up @@ -796,7 +796,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7fa9bc769320>\n"
"<__main__.ComplexNumber object at 0x7f03c052d2e8>\n"
]
}
],
Expand Down Expand Up @@ -1085,7 +1085,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7fa9bc769860>\n"
"<__main__.ComplexNumber object at 0x7f03c052d860>\n"
]
}
],
Expand Down Expand Up @@ -1160,7 +1160,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7fa9bc7698d0>\n"
"<__main__.ComplexNumber object at 0x7f03c052d978>\n"
]
}
],
Expand Down Expand Up @@ -1708,6 +1708,31 @@
" self.assertEquals(3 * ComplexNumber(1,2), ComplexNumber(3,6)); \n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
".................\n",
"----------------------------------------------------------------------\n",
"Ran 17 tests in 0.013s\n",
"\n",
"OK\n"
]
}
],
"source": [
"# ignore this\n",
"import ComplexNumber_test\n",
"jupman.run(ComplexNumber_test)"
]
}
],
"metadata": {
Expand Down
7 changes: 0 additions & 7 deletions exercises/relmath/relmath.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,6 @@
"source": [
"link_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 3c67de4

Please sign in to comment.